gpt4 book ai didi

比较 3 个数字时出现 C 错误(其中 1 个为负数)

转载 作者:行者123 更新时间:2023-11-30 18:36:45 24 4
gpt4 key购买 nike

如果有人能帮助我编写代码,那就太好了。根据任务,我有几个数字(至少 1 个负数),如果我得到 3 个数字 > 0 或 = 0,则答案为 0。Cod 应该找到最接近 0 的数字。这是我的鳕鱼,但它不起作用。

#include <stdio.h>
#include <conio.h>
int main()
{
int uno, dos, tres; //numbers


printf("enter your numbers");
scanf_s("%d %d %d", &uno, &dos, &tres);
if (uno > 0 || uno == 0 && dos > 0 || dos == 0 && tres > 0 || tres == 0)
printf("There is no negative numbers ...");
else if (uno == 0 || uno > 0 && dos > 0 || dos == 0 && tres < 0)
printf("Your number is %d", &tres);
else if (uno > 0 || uno == 0 && dos < 0 && tres < 0)
if (dos < tres || tres == dos)
printf("Your number is %d", &tres);
else
printf("Your number is %d", &dos);
else if (uno < 0 && dos < 0 && tres < 0)
if (uno < dos && uno < tres)
if (dos < tres || tres == dos)
printf("Your number is %d", &tres);
else printf("Your number is %d", dos);
else if (dos < uno && dos < tres)
if (uno < tres|| uno==tres)
printf("Your number is %d",& tres);
else printf("Your number is %d", &uno);
else if (tres < uno && tres < dos)
if (uno < dos || uno==dos)
printf("Your number is %d",&dos);
else printf("Your number id %d",&uno);

_getch();
return 0;
}

感谢任何帮助。

最佳答案

逻辑或 (||) 的优先级高于逻辑与 (&&),因此您应该添加一些括号。例如。

if (uno > 0 || uno == 0 && dos > 0 || dos == 0 && tres > 0 || tres == 0)

应该变成

if ((uno > 0 || uno == 0) && (dos > 0 || dos == 0) && (tres > 0 || tres == 0))

关于比较 3 个数字时出现 C 错误(其中 1 个为负数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40295898/

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