逻辑错误,需要帮助

Logic error, assistance required

本文关键字:帮助 错误      更新时间:2023-10-16

>我在这个应用程序上遇到了一个逻辑错误。这是一个单词混乱的应用程序,显示一个混乱的单词,并询问玩家是否愿意在他们猜对后再次玩。

当我告诉应用程序我不想再玩时,它无论如何都会继续执行序列。我有一种感觉,它对我的嵌套很糟糕。

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
    enum fields {WORD, HINT, NUM_FIELDS};
    const int NUM_WORDS = 5;
    const string WORDS[NUM_WORDS][NUM_FIELDS] =
    {
        {"wall", "Are you banging your head against something?"},
        {"jumble", "Its what this game is all about."},
        {"glasses", "You might need these to read this text."},
        {"labored", "Going slowly, is it?"},
        {"persistent", "Keep at it."},
    };
    srand(static_cast<unsigned int>(time(0)));
    cout << "ttWelcome to Word Jumble!nn";
    cout << "Unscramble the the letters to make the word!n";
    cout << "Enter 'hint' for a hintn";
    cout << "Enter 'quit' to quit the gamenn";

    const int MAX_LEVEL = NUM_WORDS - 1;
    int totalScore = 0;
    for (int level = 0; level <= MAX_LEVEL; ++level)
    {
        string theWord = WORDS[level][WORD]; // Word to guess
        string theHint = WORDS[level][HINT]; // Word hint
        char playAgain;
        string jumble = theWord; //Jumbled version of the word
        int length = jumble.size();
        int score = jumble.size() * 10;
        for (int i = 0; i < length; ++i)
        {
            int index1 = (rand() % length);
            int index2 = (rand() % length);
            char temp = jumble[index1];
            jumble[index1] = jumble[index2];
            jumble[index2] = temp;
        }
        cout << jumble << endl;
        string guess;
        cout << "nYour Guess: ";
        cin >> guess;

        while ((guess != theWord) && (guess != "quit"))
        {
            if (guess == "hint")
            {
                cout << theHint;
                score = score / 2;
            }
            else
            {
                cout << "nnSorry thats not it.nn";
            }
            cout << "nnYour Guess: nn";
            cin >> guess;
        }
        if (guess == theWord)
        {
            cout << "Thats it! You guessed it!tYou scored: " << score << "nn";
            cout << "Would you like to play again? (y/n): ";
            cin >> playAgain;
            if (playAgain = 'y')
            {
                continue;
            }
            else if (playAgain = 'n')
            {
                cout << "Your total score is: " << totalScore << endl;
                break;
            }
        }

        else if (guess == "quit")
        {
            if (totalScore > 0)
            {
                cout << "Your total score is: " << totalScore << endl;
            }
            break;
        }
    }
    cout << "nGoodbye.";
    return 0;
}

playAgain'y''n'进行比较时,你只有一个等号,导致第一个('y')总是执行而不是实际的选择,因为'y'的值不是0。

要解决此问题,它们应该是:

if (playAgain == 'y') //note ==
{
    continue;
}
else if (playAgain == 'n') //note ==
{
    cout << "Your total score is: " << totalScore << endl;
    break;
}

此外,如果您打开了警告,任何理智(更现代)的编译器都应该警告您。一定要打开它们并注意它们。

我认为您将需要==用于您的PlayAgain问题。我经常犯错误。