自从发布此书以来,我已经休息了一段时间,并阅读了我正在研究的C编程书籍(哈佛cs50书籍)的一半。我现在应该可以解决此问题,但无法解决。
无论输入什么整数,程序都会连续循环运行;无限地打印“对您有好处...”。
示例代码:
//example 3 version2 from chapter 11, beginner programming in c
#include <cs50.h>
#include <stdio.h>
int main ()
{
int prefer;
printf("On a scale from 1 to 10, how happy are you?\n");
scanf(" %d", &prefer);
while(prefer >= 1 || prefer <= 10)
//goal is for program to run while entered int "prefer" is between 1 - 10
if (prefer > 10)
{
printf("Oh really, now? Can't follow simple directions, can you?\n");
printf("want to try that again? 1 through 10...?\n");
scanf(" %d", &prefer);
}
else if (prefer >= 8)
{
printf("Good for you!\n");
}
else if (prefer <= 5)
{
printf("Cheer up : )\n");
}
else if (prefer <= 3)
{
printf("Cheer up, Buttercup!\n");
}
else
{
printf("Get in the RIVER with that attitude!\n");
}
return 0;
}
答案 0 :(得分:1)
运算符<
和&&
是二进制运算符。当我们使用它们时,它将比较左侧和右侧的值。上面的那段看起来像这样。
while(prefer <= 10 && prefer > 0);