C语言 C 中的是/否循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18627163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yes/No loop in C
提问by user2232926
I just don't understand why this Yes/No loop will not work. Any suggestions? Given the input is "Y". I just want it to run the loop, then ask for Y or N again. If Y, print success, if N, print a good bye statement. What's the reason?
我只是不明白为什么这个是/否循环不起作用。有什么建议?鉴于输入是“Y”。我只希望它运行循环,然后再次请求 Y 或 N。如果是,则打印成功,如果是 N,则打印再见语句。什么原因?
int main(){
char answer;
printf("\nWould you like to play? Enter Y or N: \n", answer);
scanf("%c", &answer);
printf("\n answer is %c");
while (answer == 'Y'){
printf("Success!");
printf("\nDo you want to play again? Y or N: \n");
scanf("%c", &answer);
}
printf("GoodBye!");
return 0;
}
回答by Yu Hao
Change the second scanfto:
将第二个更改scanf为:
scanf(" %c", &answer);
// ^
The problem is, when you enter Y and press ENTER, the new line is still in the input buffer, adding a space before %ccould consume it.
问题是,当您输入 Y 并按 ENTER 时,新行仍在输入缓冲区中,在%c可以使用它之前添加一个空格。
回答by Keith Nicholas
fixed the various issues
修复了各种问题
#include <stdio.h>
int main(){
char answer;
printf("\nWould you like to play? Enter Y or N: \n");
scanf(" %c", &answer);
printf("\n answer is %c\n", answer);
while (answer == 'Y'){
printf("Success!");
printf("\nDo you want to play again? Y or N: \n");
scanf(" %c", &answer);
printf("\n answer is %c\n", answer);
}
printf("GoodBye!");
return 0;
}
回答by Jonathan Leffler
You can reduce the repetition in your code a bit, and check the result of scanf()(as you should) by writing:
您可以稍微减少代码中的重复,并scanf()通过编写以下内容来检查(如您应该的那样)的结果:
int main(void)
{
char answer;
printf("Would you like to play? Enter Y or N: ");
while (scanf(" %c", &answer) == 1 && answer == 'Y')
{
printf("Answer is %c\n", answer);
printf("Success!\n");
printf("Do you want to play again? Y or N: ");
}
printf("GoodBye!\n");
return 0;
}
The first printf()lost the unused argument answer; the second printf()collected the necessary second argument, answer. Except for prompts, it is generally best to end printing operations with a newline (rather than use a newline at the start). Prompts will generally be flushed by the C library before the input from stdinis read, so you don't need the newline at the end of those.
第一个printf()丢失了未使用的参数answer;第二个printf()收集了必要的第二个参数,answer。除提示外,通常最好以换行符结束打印操作(而不是在开始时使用换行符)。提示通常会在stdin读取输入之前由 C 库刷新,因此您不需要在这些末尾添加换行符。
Since printf()returns the number of characters it prints, you can use it in conditions too:
由于printf()返回它打印的字符数,您也可以在条件中使用它:
int main(void)
{
char answer;
printf("Would you like to play? Enter Y or N: ");
while (scanf(" %c", &answer) == 1 &&
printf("Answer is %c\n", answer) > 0 &&
answer == 'Y')
{
printf("Success!\n");
printf("Do you want to play again? Y or N: ");
}
printf("GoodBye!\n");
return 0;
}
This always echoes the answer, even if the answer was not Yand the loop exits.
这总是回应答案,即使答案不是Y并且循环退出。

