简体   繁体   中英

if else if not outputing else if statement java

I have problem with else if part of the code. First "if" works fine, third (else) works fine, but I have problem with "else if" part of the code, where program don't execute this statement.

if ((a+c)>=(b+d)) {
    if ((a==b) && (b==c) && (c==d)) {
        System.out.println("square");
    } else if ( (a!=b) && (c!=d)  && ( a== c) && (b == d)  ) {
        System.out.println("rectangle");
    } else {
        System.out.println("four different sides");
    }
} else
    System.out.println("ERROR");

So, when I enter values a=c and b=d, I get "ERROR" instead of "rectangle.

What did I do wrong?

Thanks!

You have this condition at the top: 'if ((a+c)>=(b+d))'. Maybe you are entering a = c and b = d but it may does not meet this condition '((a+c) >= (b + d)' which is the very first one. Due to which it will go towards else part of first if statement. For more details share your values which you are entering maybe I can elaborate it a bit more.

If you are entering values a = c and b = d then you have to also check that a or c values are greater then b and d. In your case, a and c values are smaller then b and d so first if statement doesn't meet the condition.

You can try this for understanding.

public static void main(String[] args) {
        int a = 4,b =2 ,c =4 ,d =2;
        if ((a+c)>=(b+d))
        {
            if ((a==b) && (b==c) && (c==d))
            {

                System.out.println("square");

            }
            else if ( (a!=b) && (c!=d)  && ( a== c) && (b == d)  )
            {
                System.out.println("rectangle");
            }

            else
            {

                System.out.println("four different sides");
            }

        }
        else{
            System.out.println("ERROR");
        }
    }

Just modify your code to this :

if ((a + c) >= (b + d)) {
        if ((a == b) && (b == c) && (c == d)) {
            System.out.println("square");
        } else if ((a != b) && (c != d) && (a == c) && (b == d)) {
            System.out.println("rectangle");
        } else {
            System.out.println("four different sides");
        }
    } else if ((a != b) && (c != d) && (a == c) && (b == d)) {
        System.out.println("rectangle");
    } else if (a != b && b != c && c != d && a != c) {
        System.out.println("four different sides");
    } else
        System.out.println("ERROR");

Job Done :)

Your first condition if ((a+c)>=(b+d)) is not correct for rectangle condition because for rectangle both opposite side are equal.If you sum (a+c) it looks like two small side value is never equal to two big side (b+d) value.Similarly if you change a,c as big side and b,d as small side of rectangle condition will be same as before.So you can change like bellow

Sample input for square int a=2,b=2,c=2,d=2;

Sample input for rectangle int a=2,b=4,c=2,d=4;

Sample input a=10,b=4,c=6,d=9

if ((a==c)||(b==d))
        {
            if ((a==b) && (b==c) && (c==d))
            {
                System.out.println("square");
            }
            else if ( (a!=b) && (c!=d)  && ( a== c) && (b == d))
            {
                System.out.println("rectangle");
            }
            else 
            {           
                System.out.println("Two sides are similar but not square or rectangle");
            }

        } else
        System.out.println("Foure side are different");

You can try this solution for checking square, rectangle and four sides are different for all sample input

 if(a>0 && b>0 && c>0 && d>0)
  {
    if ((a==c) ||(b==d))
    {
        if ((a==b) && (b==c) && (c==d))
        {
            System.out.println("square");
        }
        else if ( (a!=b) && (c!=d)  && ( a== c) && (b == d))
        {
            System.out.println("rectangle");
        }
        else{
            System.out.println("Two side are same but not square or rectangle");
        }
    }  
    else 
       System.out.println("four different sides");

 } else 
    System.out.println("Error");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM