import java.io.*;
import java.util.*;

class Q9 {
    public static void main(String args[]) {
        int a,b,c;
        System.out.println("\n\t Enter the value of A,B & C ");
        Scanner sc = new Scanner(System.in);
        a = sc.nextInt();b = sc.nextInt(); c = sc.nextInt();
        System.out.println("\nLargest = "+func(a,b,c));
    }

    public static int func(int a,int b,int c) {
        if((a>=b)&&(a>=c)) {
            return(a);
        }
        if((b>=a)&&(b>=c)) {
            return(b);
        }
        if((c>=b)&&(c>=a)) {
            return(c);
        }
    } 
}

Although logically there should be a guarantee here that your function will reach a return statement, the java compiler doesn't seem to see it that way. I think there are two possible solutions:

1) Add an extra return statement that just returns some arbitrary value to appease the compiler.
2) Rewrite your logic as an if-else chain, like so:

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

This makes it much easier for the compiler to be able to tell that your function is guaranteed to return a value.

what happen if none of these three conditions are true?

at that time you have to put one default return statement at the end of function.

So write return 0 or any other int as last line of func(if none of these condition satisfied)

that can indeed be a sollution if, and only, if you know that 0 can never be a valid outcome of the method, but in this method, I can't see any reason why any return value would be invalid (depending on the numbers you enter)

another sollution would be to throw an Exception if none of these if's return true.

so, going back to your original method, you can also change it like this:

public static int func(int a, int b, int c) throws Exception {
if ((a >= b) && (a >= c)) {
    return a;
} else if ((b >= a) && (b >= c)) {
    return b;
} else if ((c >= b) && (c >= a)) {
    return c;
}
throw new Exception("no match found");
}

or like this:

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

logic dictates the last option, since you'll always return one of either three. if you throw an exception, you'll still have to handle that never-occurring Exception where you call the method, which isn't exactly good practice. you'll write lines of code you don't need, and you'll force other developers who might need to use your method to handle exceptions they'll never encounter.

To throw the spanner in the works a bit ... perhaps this isn't the best way to find the largest number. Usually you assign the first number largest, and then compare it to subsequent inputs. If the next number is larger than the current one, then that becomes largest and so on until no more numbers are entered. {as taught in many texts :D}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.