简体   繁体   中英

C# - Finding maximum of 3 numbers using if / else statements

I'm completely new to C# - just trying out some simple stuff. I want to find the maximum of three numbers, using if/else statements. I tried writing this method:

public int Maximum(int num1, int num2, int num3)
    {
        if (num2 < num1 > num3)
        {
            return num1;
        }

        else if (num1 < num2 > num3)
        {
            return num2;
        }

        else if (num1 < num3 > num2)
        {
            return num3;
        }
    }

but I've gotten a bit confused.

Q1 - By saying "int Maximum", do I still need to declare each parameter as being an int (or the same the other way round)? Or does everything need its data type declared?

Q2 - Keeps telling me that "Operator '>' cannot be applied to operands of type bool and int"? I'm confused as to what I should do here. Have I done something wrong or is there an alternative?

Thanks for any help.

Regarding Q1

C# is a strongly typed language. This means that you must explicitly tell the compiler the types of your parameters, variables, members, and even your methods return value.

This is similar to other strongly typed languages, such as Java or C++.

PS: Newest C# keywords (such as var or dynamic ) try to move C# from a strongly typed environment to an hybrid one.

Regarding Q2

num < num2 > num3 is not a valid condition expression.

num < num2 yields a bool , so the following expression num < num2 > num3 would be equivalent to bool > num3 , which is invalid, since you can't compare a bool and an int like this.

What you're looking for is the Conditional AND (&&) operator:

if (num < num2 && num2 > num3)
{
    return num2;
}

Alternatively, you can also use nested if statements to achieve the same behaviour:

if (num < num2)
{
    if (num2 > num3)
    {
        return num2;
    }
}

Regarding your code

The code you have provided has another problem.

What would happen if none of the three conditions are met? What would the method return?

Of course, you know that it's not possible for the three conditions to never be met, but the compiler doesn't know this .

You have to either return a dummy value, or remove your last else if :

public int Maximum(int num1, int num2, int num3)
{
    if (num2 < num1 && num1 > num3)
    {
        return num1;
    }

    else if (num1 < num2 && num2 > num3)
    {
        return num2;
    }

    // If we reached this point, we know that num3 is the biggest of them all
    else
    {
        return num3;
    }
}

The statement inside the brackets () must result in true or false. The way it is written will not work as you are not comparing two values. Instead you should write:

public int Maximum(int num1, int num2, int num3)
{
    if ((num2 < num1) && (num1 > num3))
    {
        return num1;
    }

    else if ((num1 < num2) && (num2 > num3))
    {
        return num2;
    }

    else
    {
        return num3;
    }
}

I think the basic answer would be like this for you(a new learner):

 int num1 = 1, num2 = 2, num3 = 3;
 int max;
 if (num1 >= num2)
 {
    if (num3 >= num1)
    {
       max = num3;
    }
    else
    {
       max = num1;
    }
 }
 else
 {
    if (num3 >= num2)
    {
       max = num3;
    }
    else
    {
       max = num2;
    }
 }

A1: yes, you have to declare the types of all parameters. The parameter type is nothing to do with your return type.

A2: you expect a bool value from your if statement but (n2 < n1 > n3) means (n2 < n1) > n3 -> bool > int. You cannot compare a boolean value with an integer.

If you really want to do it in a if else way,

if(num1 > num2)
{
    if(num1 > num3 )
        return num1;
    else 
        return num3;
}
else
{
    if(num2 > num3)
        return num2;
    else
        return num3;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM