angie angie - 2 years ago 70
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

have been
declared as
, so the return type is

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download