【发布时间】:2018-04-06 18:47:26
【问题描述】:
我正在尝试执行一个 do-while 循环,其中用户输入一个值,1 或 0,如果它不是 1 或 0,则循环将要求他们再次执行此操作,直到他们输入 1 或 0 . 但是,在运行时,当我输入 1 或 0 时,它会继续打印 if 语句中的内容
您的回答不正确,它必须是 1 或 0
即使我已经进入 1/0,也永远停留在这个循环中。它是否存储 enter 键?我缺少什么以及如何解决这个问题?
int validateresultmanual(char* word, char* suggestion)
{
int choice;
int result;
if(suggestion == NULL)
{
result = FALSE;
}
else
{
do
{
printf("Enter 0 for yes or 1 for no: Do you want to change %s to %s?\n", word, suggestion);
scanf("%c", &choice);
if(choice != 0 || choice != 1)
{
printf("You have given an incorrect response, it must either be 1 or 0\n");
}
else if(choice == 0)
{
printf ("\n yaaaaaaaaaaaaa \n");
result = TRUE;
}
else if (choice == 1)
{
result = FALSE;
}
} while (choice != 0 || choice !=1 );
}
return result;
}
【问题讨论】:
-
提示:想一个数字
choice,choice != 0和choice != 1的计算结果都是false;这是您需要输入的数字才能结束循环。 -
是的,它会读入回车键。
-
超级骗子。很多很多类似的Q:(
标签: c if-statement scanf user-input do-while