Method that returns greater value of two numbers
Asked Answered
S

6

25

So I have this code

  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
        {
            // ??????
        }
    }

is there a way to make GetMax return a string with error message or something when first == second.

Suazo answered 28/9, 2013 at 18:40 Comment(1)
Already part of the BCL: Math.MaxSuperorganic
H
16
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!!!");
    }
}

but really I would simply do:

public static int GetMax(int first, int second)
{
    return first > second ? first : second;
}
Hydro answered 28/9, 2013 at 18:43 Comment(0)
C
46

You can use the built in Math.Max Method

Conner answered 5/10, 2016 at 23:14 Comment(2)
OP wants to return a string/error if the two values are equal.Jacelynjacenta
In Unity this requires using System;Scilicet
H
16
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!!!");
    }
}

but really I would simply do:

public static int GetMax(int first, int second)
{
    return first > second ? first : second;
}
Hydro answered 28/9, 2013 at 18:43 Comment(0)
P
4

Since you are returning greater number, as both are same, you can return any number

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

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

You can further simplify it to

public static int GetMax(int first, int second)
{
  return first >second ? first : second; // It will take care of all the 3 scenarios
}
Poche answered 28/9, 2013 at 18:43 Comment(2)
Best and most logical solution.Bolin
OP wants to return a string/error if the two values are equal. I think most people here are just reading the title, which is different to what the question actually is.Jacelynjacenta
C
1

If possible to use the List type, we can make use of the built in methods Max() and Min() to identify the largest and smallest numbers within a large set of values.

List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(30);
numbers.Add(30);
..

int maxItem = numbers.Max();
int minItem = numbers.Min();
Calcium answered 19/1, 2015 at 12:37 Comment(0)
I
0
    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);
    }
Impose answered 1/10, 2020 at 18:30 Comment(0)
N
0

Since you want to display the output like this

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

I'd have the GetMax function return a string:

public static string GetMaxString(int first, int second)
{
    return (first > second) ? first.ToString() : (second > first) ? second.ToString() : "The two values are equal";
}
Neysa answered 19/1, 2024 at 17:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.