gpt4 book ai didi

c - C 中 do while 循环条件错误

转载 作者:行者123 更新时间:2023-11-30 17:10:33 24 4
gpt4 key购买 nike

在尝试这部分程序时,我在 Xcode 中收到“代码将永远不会被执行”警告。该警告出现在 do while 循环的 while 部分的条件中。如果未输入列表中显示的数字之一,则应该打印下面的字符串。有什么建议吗?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main()
{

int input;

do
{
printf("Welcome to the Arithmetic Quiz Program!\n\n
Please choose one of the following by entering the correspoding number:\n\n
1. Give me an addition problem.\n
2. Give me a subtraction problem.\n
3. Give me a multiplication problem.\n
4. Quit.\n\n");

scanf(" %i", &input);

} while ( input!=1 || input!=2 || input!=3 || input!=4 );

return 0;

}

最佳答案

If your do while loop condition has all the conditions possible then it would never evaluate to false to break the loop

OR 条件只需要一个条件即可计算为 True,因此如果用户输入为 1

Input!=1 //evaluates to false
Input!=2 //becomes True

等等

使用 && 运算符,这将检查如果您的输入不是 1,2,3,4 则会中断

关于c - C 中 do while 循环条件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32815564/

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