-3

有谁知道这段代码有什么问题。它提出了上面的错误。我正在尝试创建一个生成“随机”字母的程序,并为用户提供最多 6 次猜测该字母的机会。

if ( Play != "y" || Play != "Y" )

编辑:完整代码

// This function displays game instructions, and returns nothing.
void Instructions();

// This function plays one game, and returns "W" if the player wins
// or "L" if the player runs out of guesses. 
char Play();

//this function prompts the player to make a guess and returns that guess
char getLetter();

//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
char guess();

// This function returns a random letter between "A" and "Z 
char getAnswer();

int CompareLetters(char guess, char answer);

int main()
{ 

    char answer;


    //1. Greet the user and ask if they would like to play a guessing game.
    printf("\nHello there!\nWould like to play a guessing game?Enter Y or N: \n");
    scanf(" %c", &answer);

    if(answer == 'y' || answer == 'Y')
        Instructions();
    {
        printf("\nYou entered Y.  Let's  play!\n");

        do{

        }while (answer == 'y' || answer == 'Y');
    }
    printf("\nMaybe next time.\n");
    printf("\nGoodBye for now.\n");
    return -1;
} 


void Instructions()
{
    printf("I have a capital letter in mind. You have 6 chances to guess which letter I am \nthinking. I will let you know if you are too high or too low.\n");
    printf("After each guess, you will be informed if your guess is too high or too low.\nGood luck!\n");

}

int PlayGuess(char answer)
{
    int NumGuesses=0; int WinOrLose=0;

    while (NumGuesses < MAX_GUESSES && WinOrLose==0);
    {

        //6. If the player guesses wrong for a 6th time, console them and let the program end with a return code of 1.  
        char guess;
        guess = getLetter();
        CompareLetters(guess,answer);
        if(CompareLetters(guess,answer)==1)
        {
            WinOrLose = 1;
        }
        else
        {
            NumGuesses++;
        }
    }
    if (WinOrLose==1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

//3. Generate a "random" character between 'A' and 'Z'.  This will be the value the player will try to guess. 
char getLetter()
{
    char guess=0;
    printf("Please enter your letter guess:", guess);
    scanf(" %c",&guess);
    return guess;
}

int CompareLetters(char guess, char answer)
{
    if(guess == answer)
    {
        return 1;
    }
    else
    {
        ////5. If the player does not guess the right answer, display whether the guess is "too high" or "too low". 
        if (guess < answer)
        {
            printf("Your guess is too low.");
        }
        else
        {
            printf("Your guess is too high.");
        }

        {
            printf("\nDo you want to play again? Y or N: \n");

        }
        if ( Play != 'y' &&  Play != 'Y' )
            printf("Thanks for playing.Goodbye!/n");
}
4

3 回答 3

3

Play似乎是 (single) char,而不是char数组。所以你应该使用'y'而不是"y".

if ( Play != 'y' || Play != 'Y' )

这里似乎也存在逻辑错误-大概您打算使用&&而不是||

if ( Play != 'y' && Play != 'Y' )

编辑:您现在已经添加了其余的源代码。这里有很多问题,但我会列出一些让你开始。

  • 您的调用Instructions()不在大括号内,因此Instructions()将有条件地调用,其余代码将无条件地执行。

    if(answer == 'y' || answer == 'Y')
        Instructions();
    {
        printf("\nYou entered Y.  Let's  play!\n");
    

    这应该是:

    if(answer == 'y' || answer == 'Y')
    {
        printf("\nYou entered Y.  Let's  play!\n");
        Instructions();
    
  • 你的do {} while陈述是空的。这也是一个无限循环,因为你的scanffor 答案在循环之外。

        do{
    
        }while (answer == 'y' || answer == 'Y');
    

    您应该移动循环scanf 内部,并添加对Play().

  • Play与in进行比较的 if 语句CompareLetters既不正确(没有调用变量Play),又出现在错误的地方(CompareLetters不应该对此负责。)

    你应该把它移到上面main,更新和比较answer我上面提到的变量。

于 2013-10-13T19:28:23.827 回答
1

在 C 中,双引号中的字符序列表示一个字符串,在内存中是一个const char *. 您的变量Play包含一个char,而不是一个char*. 告诉编译器使用char常量的方法是在字符周围使用单引号,如下所示:

Play != 'Y'
于 2013-10-13T19:31:43.280 回答
0

该变量Play正在使用 char,因此请尝试使用''. 也""用于 char* 文字。您可以这样尝试:

if ( Play != 'y' || Play != 'Y' )

代替

 if ( Play != "y" || Play != "Y" )
于 2013-10-13T19:29:58.193 回答