【问题标题】:C# biggest of 5 numbers only with If/elseC# 最大的 5 个数字,只有 If/else
【发布时间】:2014-04-04 11:29:28
【问题描述】:

在做一些 C# 练习时遇到了这个问题。

// 仅使用 5 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。我知道错误在哪里,但我不知道如何真正解决它。

【问题讨论】:

  • 我认为你应该使用>= 而不是>。否则你有false -1 > -1
  • 而且你总是在你的Console.WriteLine 中使用a, b, c, max。是否有任何特殊原因将您的 string 解析为 double 而不是 int
  • 是的,这似乎有效。我会做更多的测试。
  • 如果您已完成学业,请使用double max = new[] { a, b, c, d, e }.Max();

标签: c# if-statement compare


【解决方案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);

【讨论】:

    【解决方案2】:

    你也应该处理相等的情况,否则没有'if'语句将匹配并且你最终会进入最后一个设置max=e的'else'块,这是错误的。 做:

                  if (a >= b && a >= c && a >= d && a >= e)
            {
                max = a;
    
            }
            else if (b >= a && b >= c && b >= d && b >= e)
            {
                max = b;
    
            }
            else if (c >= a && c >= b && c >= d && c >= e)
            {
                max = c;
    
            }
            else if (d >= a && d >= b && d >= c && d >= e)
            {
                max = d;
    
            }
            else
            {
                max = e;
    
            }
            Console.WriteLine(max);
    

    【讨论】:

      【解决方案3】:

      首先,存在作弊的可能性,即

        if (a > b)
          max = a;
        else
          max = b;
      

      等于(三元运算符

        max = a > b ? a : b;  
      

      of (while 循环仿真)

        max = b;
      
        while (a > b) {
          max = a;
      
          break;
        }
      

      可能的诚实解决方案很简单,只需要 4 个ifs:

        ...
        Double max = a;
      
        if (max < b)
          max = b;
      
        if (max < c)
          max = c;
      
        if (max < d)
          max = d;
      
        if (max < e)
          max = e;
      
        Console.WriteLine("The biggest number from {0}, {1}, {2}, {3}, {4} is {5}.", a, b, c, d, e, max);
      

      【讨论】:

        【解决方案4】:

        我找到了更聪明的解决方案

            Console.Write("Type number 1:");    float a = float.Parse(Console.ReadLine());
            Console.Write("Type number 2:");    float b = float.Parse(Console.ReadLine());
            Console.Write("Type number 3:");    float c = float.Parse(Console.ReadLine());
            Console.Write("Type number 4:");    float d = float.Parse(Console.ReadLine());
            Console.Write("Type number 5:");    float e = float.Parse(Console.ReadLine());
            float max = a;
        
            if (a < b)
            {
                max = b;
            }
            if (max < c)
            {
                max = c;
            }
            if (max < d)
            {
                max = d;
            }
            if (max < e)
            {
                max = e;
            }
            Console.WriteLine(max);
        

        工作正常。

        【讨论】:

        • 返回 15 Type number 1:2 Type number 2:15 Type number 3:6 Type number 4:15 Type number 5:9 15 Press any key to continue . . .
        【解决方案5】:

        我知道这是一篇旧帖子,但我只想添加我自己的解决方案。

        int large = 0;
                for (int i = 0; i < 5; i++)
                {
                    Console.Write("Enter interger :");
                    int num = int.Parse(Console.ReadLine());
                    if (num > large)
                    {
                        large = num;
                    }
        
                }
                Console.WriteLine(large);
        

        【讨论】:

        • 你没有真正回答这个问题。他要求“仅使用五个 if 语句来找出五个数字中最大的一个。”
        • @yakobom,最佳答案已被勾选,并且我知道这是一个五个 if 语句解决方案,我只是对提供一个简单的解决方案感兴趣,即使我知道它不能使用。问题是三年前问过,我只是在添加替代品
        • 作为新用户,您应该意识到回答问题的重要性。您发布的内容是针对不同问题的替代方法 - 没有用户指定的限制。
        • 作为新用户,您应该意识到回答问题的重要性。您发布的内容是针对不同问题的替代方法 - 没有用户指定的限制。
        猜你喜欢
        • 1970-01-01
        • 2022-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-29
        相关资源
        最近更新 更多