gpt4 book ai didi

C# 最大的 5 个数字只有 If/else

转载 作者:太空宇宙 更新时间:2023-11-03 18:30:43 25 4
gpt4 key购买 nike

在做一些 C# 练习时卡在了这个上。

//通过仅使用五个 if 语句找到五个数字中最大的一个。

我不知道怎么做,所以我在网上查找并设置了它。

    Console.Write("Type number 1:");    double a = double.Parse(Console.ReadLine());
Console.Write("Type number 2:"); double b = double.Parse(Console.ReadLine());
Console.Write("Type number 3:"); double c = double.Parse(Console.ReadLine());
Console.Write("Type number 4:"); double d = double.Parse(Console.ReadLine());
Console.Write("Type number 5:"); double e = double.Parse(Console.ReadLine());

double max;
if (a > b && a > c && a > d && a > e)
{
max = a;
Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
}
else if (b > a && b > c && b > d && b > e)
{
max = b;
Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
}
else if (c > a && c > b && c > d && c > e)
{
max = c;
Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
}
else if (d > a && d > b && d > c && d > e)
{
max = d;
Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
}
else
{
max = e;
Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
}

使用练习示例,它返回正确的数据。例如

5   2   2   4   1   return: 5

但经过进一步测试后我发现这是错误的:

-1  -2  -2  -1  -15 return: -15

要么我的一年级数学技能有误,要么 -15 不大于 -1。我知道错误在哪里,但我不知道如何实际修复它。

最佳答案

即使只有 4 个 if 也很容易。

double max = a;
if (b > max) max = b;
if (c > max) max = c;
if (d > max) max = d;
if (e > max) max = e;

Console.WriteLine("max is " + max);

关于C# 最大的 5 个数字只有 If/else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22861588/

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