dev*_*dev 1 c# if-statement compare
虽然做一些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);
}
Run Code Online (Sandbox Code Playgroud)
使用练习示例,它返回正确的数据.例如
5 2 2 4 1 return: 5
Run Code Online (Sandbox Code Playgroud)
但在进一步测试后,我最终认为这是错误的:
-1 -2 -2 -1 -15 return: -15
Run Code Online (Sandbox Code Playgroud)
要么我的一年级数学技能错误,要么-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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10016 次 |
| 最近记录: |