-1
public class NastingIfElse {

    public static void main(String[] args) {

        int a = 998 , b = 857 , c = 241 , d = 153;
        int result;

        if ( a > b ) {
            if ( a > c ) {
                if ( a > d ) {
                    result = a;
                }else {
                    result = d;
                }
            }
        }
        if ( b > c ) {
            if ( b > d ) {
                result = b;
            }else {
                result = d;
            }
        }
        if ( c > a ) {
            if ( c > d ) {
                result = c;
            }else {
                result = d;
            }
        }
        if ( d > a ) {
            if ( d > c ) {
                if ( d > b ) {
                    result = d;
                }else {
                    result = b;
                }
            }
        }



        System.out.println("Biggest number of three is " + result);


    }

}

I want to do this code and find out the biggest number from this 4 numbers. but have some problems with this "nesting if program". so I need to find out some way to run this particular program for finding the highest number from these numbers by only using "nested if".

so help me in this program.

lurker
  • 56,987
  • 9
  • 69
  • 103
Om Patel
  • 3
  • 1
  • 2

2 Answers2

0

if it is some kind of practice which must be done by nested if and elses, try this:

    int a = 998 , b = 857 , c = 241 , d = 153;
    int result;

    if ( a > b ) {
        if ( a > c ) {
            if ( a > d ) {
                result = a;
            }
            else {
                result = d;
            }
        }
        else if ( c > d ) {
            result = c;
        }
        else {
            result = d;
        }
    }
    else if ( b > c ) {
        if ( b > d ) {
            result = b;
        }
        else {
            result = d;
        }
    }
    else if ( c > d ) {
        result = c;
    }
    else {
        result = d;
    }

otherwise there are really better options in java, with supporting more count of numbers.

Majid Roustaei
  • 1,556
  • 1
  • 20
  • 39
0
int a = 545, b = 24, c = 75454, d = 68;
int result = 0;
if (a > b) {
    if (a > c && a > d) {
        result = a;
    } else {
        if (c > d) {
            result = c;
        } else {
            result = d;
        }
    }
} else {
    if (b > c && b > d) {
        result = b;
    } else {
        if (c > d) {
            result = c;
        } else {
            result = d;
        }
    }
}

System.out.println(result);

The code I made is super short and easy to implement

David Buck
  • 3,752
  • 35
  • 31
  • 35
Vyom Yadav
  • 145
  • 13
  • 1
    I missed the second part of your first question. Basically, the better your answer ( good content, well formatted so it's easy to read), the more likely it is to attract upvotes. The opposite is also true. If you haven't already, take a look at [answer] – David Buck Oct 10 '20 at 10:46