gpt4 book ai didi

c - 使用嵌套 if 查找四个数字中最大的一个程序

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

先生/女士,
我不知道我做错了什么。从逻辑上看,一切对我来说都是正确的。大括号似乎也放置在正确的位置。那么为什么我会收到以下错误消息?

32 8 [Error] expected constructor, destructor, or type conversion before '(' token
33 2 [Error] expected unqualified-id before 'return'
34 1 [Error] expected declaration before '}' token

我对这个特定程序做错了什么?而且,我希望使用相同的陈述进行更正,而不是任何其他陈述。

请帮忙!

程序:

#include <stdio.h>
main()
{
int a, b, c, d, big;
printf("\n Enter value to a, b, c, d: ");
scanf("%d %d %d %d", &a, &b, &c, &d);
if (a >b)
{
if(a>c)
{
if(a>d)
big = a;
else
big=d;
}
else
{
if(c>d)
big = c;
else
big = d;
}
else
if(b>c)
{
if(b>d)
big = b;
else
big = d;
}
}
printf("\n The biggest number is %d", big);
return 0;
}

最佳答案

您可以使用三元运算符来解决您的问题。因此,代码简化为

int main() {
int a, b, c, d;
printf("\n Enter value to a, b, c, d: ");
scanf("%d %d %d %d", &a, &b, &c, &d);

const int big1 = (a > b) ? a : b;
const int big2 = (c > d) ? c : d;
int big = (big1 > big2) ? big1 : big2;

printf("\n The biggest number is %d", big);
}

关于c - 使用嵌套 if 查找四个数字中最大的一个程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58634089/

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