gpt4 book ai didi

c - 我的 while 循环与 char 不起作用。请提供一些帮助

转载 作者:行者123 更新时间:2023-11-30 18:40:13 25 4
gpt4 key购买 nike

所以我有这样的代码和平:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com