angie angie - 6 months ago 9
Java Question

What is wrong with the method below?

Below is the code for a java method, but it won't compile. I'm using Eclipse and whenever I try to compile my code it says:


This method must return a result of type
int
.


a,b,c
have been
declared as
int
, so the return type is
int
.

public static int f(int a, int b, int c) {
if ((a < b) && (b < c))
return a;
else if ((a >= b) && (b >= c))
return b;
else if ((a == b) || (b == c) || (a == c))
return c;
}

Answer

Your if conditions are not exhaustive. If a > b and b < c, none of the conditions will match, and your function will not return anything, which is why Eclipse is complaining.

Add a default return statement at the end, either without a condition or in an else block.