所以我有这样的代码和平:
int choice_dig;
char choice_dup;
printf("Welcome to the mystery number game.\n");
printf("How many digits do you want to use (3 to 5)?");
scanf("%d", &choice_dig);
while (choice_dig<3 || choice_dig>5)
{
printf("\nPlease choose a number between 3 and 5.\t");
scanf("%d",&choice_dig);
}
printf("\nDo you want to allow duplicate digits (y or n)?");
scanf(" %c", &choice_dup);
while (choice_dup != 'y' || choice_dup != 'n')
{
printf("\nInvalid entry. Please choose y for yes and n for no.\t");
choice_dup = getchar();
getchar();
}
choice_dup 已经在 main 的开头被分配为 char var。所以当我运行这个时一切都很好。但即使我按 y 或 n,它也无法识别它,并且循环永远不会结束。无论我输入什么。有人可以帮助我并向我解释哪里出了问题吗?
最佳答案
循环将永远运行,因为 while (choice_dup != 'y' || choice_dup != 'n')
的计算结果始终为 true
。
您可能想要:while (choice_dup != 'y' && choice_dup != 'n')
关于c - 我的 while 循环与 char 不起作用。请提供一些帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26872002/