【问题标题】:If and if else statements working, but not elseif 和 if else 语句有效,但不是 else
【发布时间】:2014-03-11 07:31:53
【问题描述】:

我正在制作一个猜数字游戏程序,但我的 else 语句遇到了一些问题。在试图猜测数字的主块中,if 和 if else 语句有效,但 else 语句不执行任何操作。我试图让它在范围之外的数字 0

此外,如果输入“1”,我会尝试让游戏重演,但无论输入什么,程序都会崩溃。

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    /*Number guessing game: The number that needs to be guessed is 52*/
    int guess;
    int attempt = 6;
    int playAgain = 1;

    printf("Guess the integer that I am thinking of between 1 and 100\n");
    printf("I will tell you if you guess too high or too low\n");
    printf("You have 6 attempts\n");
    printf("What is your first guess?\n");
    if (playAgain == 1)
    {
        while (attempt > 0)
        {

            scanf_s("%d", &guess);

            if (guess > 0)
            {
                if (guess < 52)
                {
                    attempt--;
                    printf("Sorry! Too low! You have %d more tries:\n", attempt);
                }
            }
            else if (guess <100)
            {
                if (guess > 52)
                {
                    attempt--;
                    printf("Sorry! Too high! You have %d more tries:\n", attempt);
                }
            }
            else if (guess == 52)
            {
                printf("Correct! You win!\n");
                attempt = 0;
            }

            else
            {
                printf("Invalid input: Please enter an integer between 1 and 100:\n");
            }
        }

        printf("Enter '1' to play again or anything else to terminate\n");
        scanf_s("%d", playAgain);
        attempt = 6;
    }

    else
        {
            printf("Thanks for playing!\n");
        }
    return 0;

}

【问题讨论】:

  • 为了您自己的理智,请使用一致的编码风格并始终以相同的方式缩进。如果您使用 tab 键缩进,请确保代码编辑器正在插入 空格
  • 另外,在你学习的时候,为 every if/else 使用大括号。这(加上这样的缩进)应该有助于澄清代码的含义。
  • 你创建了另外两个,但只有一个可以使用开关盒或类似的东西
  • 如果您在执行上述两个建议后仍有问题 - 请告诉我们。这很可能足以让您找出问题所在。

标签: c if-statement


【解决方案1】:

当您使用不带括号的if else if 时,请确保它不会有歧义。

当你这样做时:

if (true)
   if (true)
   {
   }
else if (false)
{
}

如何知道else if对应的是第一个还是第二个if?这就是为什么每个人都喊你放括号的原因。

if (true)
{
   if (true)
   {
   }
}
else if (false)
{
}

【讨论】:

    【解决方案2】:

    更正和简化的版本:

    您的程序崩溃了,因为您忘记了 & 登录scanf("%d", &amp;playAgain);

    你的程序中的逻辑是错误的,如果数字小于、等于或大于输入,则将测试与输入小于 0 或大于 100 的测试混合。

    在这个更正的版本中,“无效输入”问题与实际的“数字猜测”问题分开。

    此外,要猜测的数字 (52) 不再是硬编码的,而是使用变量 numbertobeguessed。稍后您应该增强程序以便生成随机数。

    int main(void)
    {
      /*Number guessing game: The number that needs to be guessed is 52*/
      int numbertobeguessed = 52 ;
      int guess;
      int attempt = 6;
      int playAgain = 1;
    
      printf("Guess the integer that I am thinking of between 1 and 100\n");
      printf("I will tell you if you guess too high or too low\n");
      printf("You have 6 attempts\n");
      printf("What is your first guess?\n");
    
      if (playAgain == 1)
      {
        while (attempt > 0)
        {
          scanf_s("%d", &guess);
    
          if (guess < 0 || guess > 100)
          {
            printf("Invalid input: Please enter an integer between 1 and 100:\n");
          }
          else
          {
            if (guess < numbertobeguessed)
            {
              attempt--;
              printf("Sorry! Too low! You have %d more tries:\n", attempt);
            }
            else if (guess > numbertobeguessed)
            {
              attempt--;
              printf("Sorry! Too high! You have %d more tries:\n", attempt);
            }
            else
            {
              printf("Correct! You win!\n");
              attempt = 0;
            }
          }
        }
    
        printf("Enter '1' to play again or anything else to terminate\n");
        scanf_s("%d", &playAgain);
        attempt = 6;
      }
      else
      {
        printf("Thanks for playing!\n");
      }
    
      return 0;
    }
    

    【讨论】:

      【解决方案3】:

      给出消息"Invalid input: Please enter an integer between 1 and 100:\n"else 语句被认为是最内层if-else-if 语句的else 部分。因此,只有在0 &lt; guess &lt; 100 时,才会执行该语句,因为执行进入该if-else-if 语句。因此,请正确使用 {} 来正确组合 if-else 语句。

      【讨论】:

        【解决方案4】:

        你的嵌套是错误的。为每个 ifelse 加上括号以使您的代码工作(快速修复),并使用适当的缩进使其对人类可读(如果您愿意)。

        这是一个错误的例子(伪代码):

        a = 4
        if (a > 0)
            if (a < 3)
                a = 2
        else
            a = 3
        

        你期望 a 的最终值是多少?

        不管怎样,你的:

        if (guess > 0)
        if (guess < 52)
        

        应该变成这样:

        if (guess > 0 && guess < 52)
        

        和你的:

        else if (guess <100) // this is where the problems start
        if (guess > 52)
        

        应该变成:

        else if (guess < 100 && guess > 52)
        

        你的代码就可以工作了。

        【讨论】:

          【解决方案5】:

          希望对你有帮助

          #include <stdio.h>
          #include <stdlib.h>
          
          int main()
          {
             int guess; //variable to hold the number from player
             int attempts = 6;
             char play = 'y';
          
             do
             {
          
              while(attempts)
             { 
               printf("\nEnter you guess: "); scanf("%d", &guess);
          
               attempts--;
          
               if(guess>0 && guess <100 )
               {
                  if(guess>52)
                    printf("\nThat's not it. Try something lower than that.");
                  else if(guess<52)
                    printf("\nThat's not the number. Try something higher than that.");
                  else if(guess==52)
                  { printf("\nYou got it!. You won the game."); 
                    attempts = 0; //we're setting it to zero; we don't want the loop to run anymore
                  }
               }
               else 
                 printf("\nNumber enter is not in range!");
             }//end of while loop
          
               printf("\nDo you want to play again? (y/n): "); scanf("%c", &play);
             }while(play=='y' || play=='Y'); //run till player enter 'Y'
             return 0;
          }
          

          【讨论】:

            最近更新 更多