【发布时间】:2025-11-23 20:25:01
【问题描述】:
我正在编写一段代码,在我的代码的一部分中,我使用了 C 语言的 switch 语句。如果我按 n,它会正确退出,如果我按 y,它将无限循环默认语句,直到我按 control c。我在这里做错了什么。一直在更改 while 语句,但找不到正确的。
int main()
{
char ans;
printf("DO you want to continue?");
scanf("%c", &ans);
do
{
switch(ans)
{
case 'y':
some stuff...
printf("DO you want to continue?");
scanf("%c", &ans);
break;
case'n':
printf("BYE");
break;
default:
printf("error, you must enter y or n");
continue;
}
}
while (ans!='n');
return 0;
}
【问题讨论】:
-
在我看来像 C ...
-
如果他/她没有输入“y”或“n”并且您打印错误消息,您不想让用户再次尝试输入字符吗?跨度>
标签: c loops switch-statement scanf