Greatest of three numbers using switch case

YogeshAgar picture YogeshAgar · Jul 14, 2012 · Viewed 9.3k times · Source

I want to find out the greatest number, out of three given numbers, using switch-case(without using if) I answered the question using this program, which works:

class GreatestNoSwitch{
    public int main(int a, int b, int c){
        int d = (int)Math.floor(a/b);
        int max = 0;
        switch(d){
            case 0:
                max = b;
                break;
            default:
                max = a;
        }

        d = (int)Math.floor(max/c);

        switch(d){
            case 0:
                max = c;
        }
        return max;
    }
}

Does anyone have any simpler answer?

Answer

Chris Gessler picture Chris Gessler · Jul 14, 2012

It's kinda stupid, but here you go.

switch(1)
{
    default:
        return Math.max(a, Math.max(b, c));
}