if和if else语句是否有效,但不是

时间:2014-03-11 07:31:54

标签: c if-statement

我正在制作一个数字猜谜游戏程序,并且在我的其他声明中遇到了一些麻烦。在尝试猜测数字的主块中,if和if else语句有效,但else语句什么都不做。我试图使其在0 <&lt; 0&lt; 0&gt;范围之外的数字处。数字&lt; 100触发else语句。

此外,如果输入“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;

}

5 个答案:

答案 0 :(得分:2)

如果您使用不带括号的if else if,请确保它不会是不明显的。

当你这样做时:

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

如何知道else if是对应第一个还是第二个if?这就是为什么每个人都对你大喊大叫。

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

答案 1 :(得分:1)

更正和简化版本:

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

你的程序中的逻辑是错误的,如果输入低于0或高于100,如果数字低于,等于或高于测试输入,则混合测试。 / p>

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

此外,要猜测的数字(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;
}

答案 2 :(得分:0)

你的嵌套错了。为每个ifelse添加括号以使代码正常工作(quickfix),并使用适当的缩进使其对人类可读(如果您愿意)。

以下是事情可能出错的一个例子(伪代码):

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

您期望什么是?

的最终价值

无论如何,你的:

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)

您的代码将有效。

答案 3 :(得分:0)

提供消息else的{​​{1}}语句被视为内部"Invalid input: Please enter an integer between 1 and 100:\n"语句的else部分。因此,只有当if-else-if执行才输入if-else-if语句时,该语句才会执行。所以正确使用{}来正确组合if-else语句。

答案 4 :(得分:0)

希望这会有所帮助

#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;
}