【问题标题】:Method that returns greater value of two numbers返回两个数字中较大值的方法
【发布时间】:2013-10-04 21:51:03
【问题描述】:

所以我有这个代码

  static void Main(string[] args)
    {
        Console.Write("First Number = ");
        int first = int.Parse(Console.ReadLine());

        Console.Write("Second Number = ");
        int second = int.Parse(Console.ReadLine());

        Console.WriteLine("Greatest of two: " + GetMax(first, second));
    }

    public static int GetMax(int first, int second)
    {
        if (first > second)
        {
            return first;
        }

        else if (first < second)
        {
            return second;
        }
        else
        {
            // ??????
        }
    }

有没有办法让 GetMax 在 first == second 时返回带有错误消息或其他内容的字符串。

【问题讨论】:

  • 已经是 BCL 的一部分:Math.Max

标签: c# methods


【解决方案1】:
static void Main(string[] args)
{
    Console.Write("First Number = ");
    int first = int.Parse(Console.ReadLine());

    Console.Write("Second Number = ");
    int second = int.Parse(Console.ReadLine());

    Console.WriteLine("Greatest of two: " + GetMax(first, second));
}

public static int GetMax(int first, int second)
{
    if (first > second)
    {
        return first;
    }

    else if (first < second)
    {
        return second;
    }
    else
    {
        throw new Exception("Oh no! Don't do that! Don't do that!!!");
    }
}

但我真的会这样做:

public static int GetMax(int first, int second)
{
    return first > second ? first : second;
}

【讨论】:

    【解决方案2】:

    由于您返回更大的数字,因为两者相同,您可以返回任何数字

    public static int GetMax(int first, int second)
    {
        if (first > second)
        {
            return first;
        }
    
        else if (first < second)
        {
            return second;
        }
        else
        {
            return second;
        }
    }
    

    你可以进一步简化为

    public static int GetMax(int first, int second)
    {
      return first >second ? first : second; // It will take care of all the 3 scenarios
    }
    

    【讨论】:

    • 最佳和最合乎逻辑的解决方案。
    • 如果两个值相等,OP 想要返回一个字符串/错误。我想这里的大多数人只是在阅读标题,这与实际问题不同。
    【解决方案3】:

    如果可以使用 List 类型,我们可以利用内置方法 Max() 和 Min() 来识别大量值中的最大和最小数字。

    List<int> numbers = new List<int>();
    numbers.Add(10);
    numbers.Add(30);
    numbers.Add(30);
    ..
    
    int maxItem = numbers.Max();
    int minItem = numbers.Min();
    

    【讨论】:

      【解决方案4】:

      你可以使用内置的Math.Max Method

      【讨论】:

      • 如果两个值相等,OP 想要返回一个字符串/错误。
      【解决方案5】:
          static void Main(string[] args)
          {
              Console.Write("First Number: ");
              int number1 = int.Parse(Console.ReadLine());
      
              Console.Write("Second Number: ");
              int number2 = int.Parse(Console.ReadLine());
      
              var max = (number1 > number2) ? number1 : number2;
              Console.WriteLine("Greatest Number: " + max);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-23
        • 1970-01-01
        • 2022-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多