选择你自己的冒险



因此,我将老板室作为"选择你自己的冒险计划"的一部分,而我在使if语句正常工作方面遇到了问题。当键入1或2时,打印语句不会打印。

#include <stdio.h>
#include <stdbool.h>
int main()
{
int answer;
bool repeat= true;
printf("nYou have entered the boss room!!nWould you like to turn back and allow humanity to be e[1;31mDESTROYED e[0mn ttor ne[1;31mFIGHTe[0m the boss and attempt to save the world?n1.Turn backn2.Fightn");
while(repeat) 
{   
if(scanf("%d",&answer) != 1 || scanf("%d",&answer) != 2)
{
printf("Invalid selection. Please selection an action to take.n");
printf("1.Turn backn2.Fightn");
} 
else if(scanf("%d",&answer) == 1) //add string option
{
printf("nYou have choosen to turn back and as a result the world has been destroyed and humanity has perished!!nntte[1;31mGAME OVERe[0mnn");
repeat= false;
}
else if(scanf("%d",&answer) == 2) //add string option
{
printf("nYou have chosen to fight the boss. The fate of humanity will now be decided. Good luck.n");    
repeat= false;
}  
}   
return 0;
}

scanf的返回值是它成功找到的参数数,因此您的测试是不正确的。您需要检查scanf是否返回1,这意味着它成功解码了一个整数,然后测试answer的值是否为1或2。

但是,如果用户键入的内容不是整数,比如1.1I like pie,您仍然会遇到问题。然后输入缓冲区将仍然包含原始输入,并且您的代码将永远循环。在这种情况下,在循环中再次调用scanf之前,您需要清除输入缓冲区。正是scanf(以及其他(的弱点让人们不喜欢它

相反,您可以使用类似fgets的函数将整个输入读取为字符串,从而每次清除所有输入,然后使用sscanf或类似函数进一步解析该字符串。通过这种方式,您可以保持相同的循环和测试结构,而不必担心不一致的输入会使循环永远迭代。

我已经重写了您的代码,显示了通过fgets管理输入的一种方法。

#include <stdio.h>
#include <stdbool.h>
int main()
{
char line[80];
int answer;
printf("nYou have entered the boss room!!nWould you like to turn back and allow humanity to be e[1;31mDESTROYED e[0mn ttor ne[1;31mFIGHTe[0m the boss and attempt to save the world?n1.Turn backn2.Fightn");
while(fgets(line, 80, stdin) != NULL) 
{
if(sscanf(line,"%d",&answer) != 1 || (answer != 1 && answer != 2))
{
printf("Invalid selection. Please selection an action to take.n");
printf("1.Turn backn2.Fightn");
}
else if(answer == 1) //add string option
{
printf("nYou have choosen to turn back and as a result the world has been destroyed and humanity has perished!!nntte[1;31mGAME OVERe[0mnn");
break;
}
else // answer == 2  //add string option
{
printf("nYou have chosen to fight the boss. The fate of humanity will now be decided. Good luck.n");    
break;
}  
}   
return 0;
}
  • 每次调用scanf()时都会读取输入
  • scanf()的返回值不是读取的值,而是输入的数量(读取了多少数据(

不应该像这样多次调用scanf(),而应该读取一次并将该值用于条件。

还要注意,条件answer != 1 || answer != 2将始终为真,因为没有数字同时等于1和2。应该是answer != 1 && answer != 2

#include <stdio.h>
#include <stdbool.h>
int main()
{
int answer;
bool repeat= true;
printf("nYou have entered the boss room!!nWould you like to turn back and allow humanity to be e[1;31mDESTROYED e[0mn ttor ne[1;31mFIGHTe[0m the boss and attempt to save the world?n1.Turn backn2.Fightn");
while(repeat) 
{
if(scanf("%d",&answer) != 1) // read a value
{
puts("read error");
return 1;
}
if(answer != 1 && answer != 2)
{
printf("Invalid selection. Please selection an action to take.n");
printf("1.Turn backn2.Fightn");
} 
else if(answer == 1) //add string option
{
printf("nYou have choosen to turn back and as a result the world has been destroyed and humanity has perished!!nntte[1;31mGAME OVERe[0mnn");
repeat= false;
}
else if(answer == 2) //add string option
{
printf("nYou have chosen to fight the boss. The fate of humanity will now be decided. Good luck.n");    
repeat= false;
}  
}
return 0;
}

最新更新