C If和If-else语句起作用,但不是else

C If和If-else语句起作用,但不是else,c,if-statement,C,If Statement,我正在做一个数字猜测游戏程序,我的else语句有一些问题。在试图猜测数字的主块中,if和if-else语句起作用,而else语句不起作用。我试图使范围00) { 如果(猜测0) if(a0) 如果(猜测0&&guess0) if(a0) 如果(猜测0&&guess0) if(a0) 如果(猜测0&&guess0) if(a0) 如果(猜测0&&guess

我正在做一个数字猜测游戏程序,我的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;

}
#包括
#包括
内部主(空)
{
/*猜数字游戏:需要猜的数字是52*/
智力猜测;
int尝试=6;
int=1;
printf(“猜一猜我想到的1到100之间的整数”\n);
printf(“如果您猜得太高或太低,我会告诉您\n”);
printf(“您有6次尝试\n”);
printf(“您的第一个猜测是什么?\n”);
如果(再次播放==1)
{
同时(尝试次数>0)
{
scanf_s(“%d”和“猜测”);
如果(猜测>0)
{
如果(猜测<52)
{
企图--;
printf(“对不起!太低了!您还有%d次尝试:\n”,尝试次数);
}
}
否则如果(猜测52)
{
企图--;
printf(“对不起!太高了!您还有%d次尝试:\n”,尝试);
}
}
否则如果(猜测=52)
{
printf(“正确!您赢了!\n”);
尝试=0;
}
其他的
{
printf(“无效输入:请输入一个介于1和100之间的整数:\n”);
}
}
printf(“输入'1'以再次播放,或输入任何其他内容以终止\n”);
scanf_s(“%d”,再次播放);
尝试=6;
}
其他的
{
printf(“感谢您的播放!\n”);
}
返回0;
}

您的嵌套错误。为每个
if
else
放上括号以使代码正常工作(quickfix),并使用适当的缩进使其可读(如果您愿意)

下面是一个如何出错的示例(伪代码):

a=4
如果(a>0)
if(a<3)
a=2
其他的
a=3
你期望一个公司的最终价值是什么

无论如何,你的:

if (guess > 0)
if (guess < 52)
if(猜测>0)
如果(猜测<52)
应该是这样的:

if (guess > 0 && guess < 52)
if(猜测>0&&guess<52)
你的:

else if (guess <100) // this is where the problems start
if (guess > 52)
else if(猜测52)
应成为:

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

您的代码将正常工作。

您的嵌套错误。为每个
if
else
放上括号以使代码正常工作(quickfix),并使用适当的缩进使其可读(如果您愿意)

下面是一个如何出错的示例(伪代码):

a=4
如果(a>0)
if(a<3)
a=2
其他的
a=3
你期望一个公司的最终价值是什么

无论如何,你的:

if (guess > 0)
if (guess < 52)
if(猜测>0)
如果(猜测<52)
应该是这样的:

if (guess > 0 && guess < 52)
if(猜测>0&&guess<52)
你的:

else if (guess <100) // this is where the problems start
if (guess > 52)
else if(猜测52)
应成为:

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

您的代码将正常工作。

您的嵌套错误。为每个
if
else
放上括号以使代码正常工作(quickfix),并使用适当的缩进使其可读(如果您愿意)

下面是一个如何出错的示例(伪代码):

a=4
如果(a>0)
if(a<3)
a=2
其他的
a=3
你期望一个公司的最终价值是什么

无论如何,你的:

if (guess > 0)
if (guess < 52)
if(猜测>0)
如果(猜测<52)
应该是这样的:

if (guess > 0 && guess < 52)
if(猜测>0&&guess<52)
你的:

else if (guess <100) // this is where the problems start
if (guess > 52)
else if(猜测52)
应成为:

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

您的代码将正常工作。

您的嵌套错误。为每个
if
else
放上括号以使代码正常工作(quickfix),并使用适当的缩进使其可读(如果您愿意)

下面是一个如何出错的示例(伪代码):

a=4
如果(a>0)
if(a<3)
a=2
其他的
a=3
你期望一个公司的最终价值是什么

无论如何,你的:

if (guess > 0)
if (guess < 52)
if(猜测>0)
如果(猜测<52)
应该是这样的:

if (guess > 0 && guess < 52)
if(猜测>0&&guess<52)
你的:

else if (guess <100) // this is where the problems start
if (guess > 52)
else if(猜测52)
应成为:

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

您的代码将正常工作。

当您使用
if
或者如果
没有括号,请确保它不能含糊不清

当您这样做时:

if (true)
   if (true)
   {
   }
else if (false)
{
}
如何知道else if
是否对应于第一个或第二个
if
?这就是为什么每个人都大声叫你放括号

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

当您使用不带括号的
if
else if
时,请确保它不能模棱两可

当您这样做时:

if (true)
   if (true)
   {
   }
else if (false)
{
}
如何知道else if
是否对应于第一个或第二个
if
?这就是为什么每个人都大声叫你放括号

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

当您使用不带括号的
if
else if
时,请确保它不能模棱两可

当您这样做时:

if (true)
   if (true)
   {
   }
else if (false)
{
}
如何知道else if是否对应于第一个或第二个
if
?这就是为什么每个人都大声叫你放括号

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

当您使用不带括号的
if
else if
时,请确保它不能模棱两可

当您这样做时:

if (true)
   if (true)
   {
   }
else if (false)
{
}
如何知道else if是否对应于第一个或第二个
if
?这就是为什么每个人都大声叫你放括号

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

给出消息“无效输入:请输入一个介于1和100之间的整数:\n”的
else
语句被视为内部mo的
else
部分