简体   繁体   English

C编程问题

[英]C programming issue

I've created a simple guessing game. 我创建了一个简单的猜谜游戏。 Pick a number between 0 and 100, depending on the number you enter the program will output hot, Warm, Cold or correct!. 选择一个0到100之间的数字,具体取决于您输入的数字,程序将输出热,暖,冷或正确! The number I've chosen to be the correct answer is 51. My program compiles and runs but always outputs correct! 我选择的正确答案是51。我的程序可以编译并运行,但始终输出正确! for every value I enter. 输入的每个值。 Thanks for helping if you decide to. 如果您愿意的话,感谢您的帮助。

    int main(void)

{
    int number, answer;
    answer = 51;

    printf("Enter a number between 0 and 100:\n");
    scanf("%d", &number);

    if ((number > 51) && (number <= 56) || (number < 51) && (number >= 46))
    {
            printf("Hot\n");
    }     
    else if ((number > 56) && (number <= 66) || (number < 46) && (number >= 36))
    {
            printf("Warm\n");
    }
    else if ((number > 66) && (number <= 100) || (number <36) && (number >= 0))
    {
            printf("Cold\n");
    }
    else if ((number > 100) || (number < 0))
    {
            printf("Error number has to be between 0 and 100 - re run\n");
    } 
    else (number = 51);
    {
            printf("Correct!\n");
    } 
    system ("pause");
    return 0;    
}

This little snippet: 这个小片段:

else (number = 51);
{
    printf("Correct!\n");
}

is actually equivalent to: 实际上等效于:

else
    (number = 51);
//=======================
{
    printf("Correct!\n");
}

The first part of that is the else clause which, if activated, will set number to 51 . 它的第一部分是else子句,如果激活该子句,它将number设置为51

The second part is totally disconnected from the if statement section and is a standalone block that will execute no matter what. 第二部分与if语句部分完全断开 ,并且是一个独立的块,无论如何执行。

If you must put the condition there for readability (and it would be == rather than = ), you could do: 如果必须将条件放在此处以提高可读性(并且将是==而不是= ),则可以执行以下操作:

else // (number == 51)
{
    printf("Correct!\n");
}

As an aside, you could make your code shorter and more readable by starting at the specific and moving to the general, something like (just replacing the if statement): 顺便说一句,您可以通过从特定内容开始并转移到一般内容,从而使代码更短,更易读,例如(仅替换if语句):

if (number == 51)
    printf ("Correct!\n");
else if ((number >= 46) && (number <= 56))
    printf ("Hot\n");
else if ((number >= 36) && (number <= 66))
    printf ("Warm\n");
else if ((number >= 0) && (number <= 100))
    printf ("Cold\n");
else
    printf ("Error,  number has to be between 0 and 100 - re run\n");
}

You don't need to ever check the two ranges (above and below) for "temperature" since the inner ranges have already been checked. 您无需检查“温度”的两个范围(上下),因为已经检查了内部范围。

else (number = 51);
{
   printf("Correct!\n");
} 

can be re-written as 可以重写为

else 
{
   number = 51;
}
{
   printf("Correct!\n");
} 

So irrespective of what your number is the printf("Correct!\\n"); 因此,无论您的电话号码是什么, printf("Correct!\\n"); gets executed. 被执行。

It can be fixed like 它可以像

else
{
   printf("Correct!\n");
} 

remove the condition from the else statement. 从else语句中删除条件。

int main(void)

{
    int number, answer;
    answer = 51;

printf("Enter a number between 0 and 100:\n");
scanf("%d", &number);

if ((number > 51) && (number <= 56) || (number < 51) && (number >= 46))
{
        printf("Hot\n");
}     
else if ((number > 56) && (number <= 66) || (number < 46) && (number >= 36))
{
        printf("Warm\n");
}
else if ((number > 66) && (number <= 100) || (number <36) && (number >= 0))
{
        printf("Cold\n");
}
else if ((number > 100) || (number < 0))
{
        printf("Error number has to be between 0 and 100 - re run\n");
} 
else
{
        printf("Correct!\n");
}
return 0;    

} }

your code says: if none of the if statements is satisfied, assign 51 to number. 您的代码说:如果所有if语句都不满足,则为number分配51。 and then always print "Correct!\\n". 然后始终打印“正确!\\ n”。

In the last else statement, you should either use else if (number == 51) { ... } or 在最后的else语句中,您应该使用else if(number == 51){...}或

else 
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM