在找到答案之后,我得到了两次问题:“太棒了!..like to play (Y or no);.Even if n.Problemıy orıto to.(顺便说一句,ı我为我糟糕的英语道歉)。”
我已尝试更改该语句的位置(如果(回答..)在while或结尾行的第一行,或像things.Alsoı那样的smth,已经尝试将break放在某个地方(ı认为问题不会涉及到这个问题,但ı尝试过)。
int main() {
srand(time(NULL));
int guess,number;
char answer;
number=1+(rand()%1000);
printf("I have a number between 1 and 1000\n");
printf("Could you guess my number?\n");
printf("Please type your guess\n");
while(1) {
scanf("%d",&guess);
if(guess==number) {
printf("Excellent!You guessed the number!Would you like to play again(y or n)");
scanf("%c",&answer);
if(answer=='y') {
number=1+(rand()%1000);
printf("I have a number between 1 and 1000 \n");
printf("Could you guess my number? \n");
printf("Please type your guess \n");
}
else if(answer=='n') {
return 1;
}
}
else if(guess>number){
printf("Too high.Try again.\n");
}
else if(guess<number){
printf("Too low.Try again.\n");
}
}
}发布于 2019-02-01 05:17:55
在您的代码中,将执行以下两个从控制台获取输入的语句:
scanf("%d",&guess);
...
scanf("%c",&answer);当执行scanf("%d",&guess);时,它将从标准输入中读取一个数字,但它会将最后一个新行(您必须在缓冲的标准输入中输入以完成输入)留在缓冲区中。随后的scanf("%c",&answer);将立即将这一新行读入到answer中,而不会让用户有机会输入任何其他字母。
写...
scanf(" %c",&answer); 使得在读入“实际”字符之前,任何空白都被消耗。
https://stackoverflow.com/questions/54469299
复制相似问题