Skip to content
Advertisement

JAVA program that checks if a triangle is scalene, isosceles, equilateral, or not a triangle

I am trying to write java program to see if a triangle is scalene, isosceles, equilateral or not a triangle. With the integers I used it is supposed to be not a triangle (1, 1, 30). But I keep getting scalene and not a triangle together. Any help is appreciated! Thank you!

public class Tri {

    static void checkTriangle(int x, int y, int z) 
    { 

        // Check for equilateral triangle 
        if (x == y && y == z ) 
            System.out.println("Equilateral Triangle"); 

        // Check for isoceles triangle 
        else if (x == y || y == z || z == x ) 
            System.out.println("Isoceles Triangle"); 

        // Check for scalene triangle
        else if (x != y || y!= z || z != x)
            System.out.println("Scalene Triangle");
        {
            // Check for not a triangle 
            if (x + y < z || x + z < y || y + z > x) 
                System.out.println("Not a triangle");

        }
    } 

    public static void main(String[] args) {
        { 

            int x = 1, y = 1, z = 30; 

            checkTriangle(x, y, z); 
        } 
    } 
}

Advertisement

Answer

You should check for not a triangle condition first. As below:

static void checkTriangle(int x, int y, int z) 
{ 

    // Check for not a triangle 
    if (x + y < z || x + z < y || y + z > x) {
        System.out.println("Not a triangle");
    } else {

    // Check for equilateral triangle 
    if (x == y && y == z ) 
        System.out.println("Equilateral Triangle"); 

    // Check for isoceles triangle 
    else if (x == y || y == z || z == x ) 
        System.out.println("Isoceles Triangle"); 

    // Check for scalene triangle
    else if (x != y || y!= z || z != x)
        System.out.println("Scalene Triangle");
    }
} 

public static void main(String[] args) {
    { 

        int x = 1, y = 1, z = 30; 

        checkTriangle(x, y, z); 
    } 
} 
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement