Problem identifying triangle in Java

Asked

Viewed 50 times

-1

Well, I’m still much new in the world of programming and I’m trying to make a simple code where it says whether the triangle is isosceles, scalene or equilateral depending on the measurement of the typed sides. However, if I type all the equal measures, instead of just saying that the triangle is equilateral (all sides are equal), it also says that it is isosceles (only two sides are equal).

Example:

Digite o valor do primeiro lado:
10
Digite o valor do segundo lado:
10
Digite o valor do terceiro lado:
10
O triângulo é equilátero.
O triângulo é isósceles.
BUILD SUCCESSFUL (total time: 7 seconds)

That is the code:

int lado1, lado2, lado3;
    Scanner leia = new Scanner(System.in);
    System.out.println("Digite o valor do primeiro lado:");
    lado1 = leia.nextInt();
    System.out.println("Digite o valor do segundo lado:");
    lado2 = leia.nextInt();
    System.out.println("Digite o valor do terceiro lado:");
    lado3 = leia.nextInt(); 
    
    if ((lado1 == lado2) && (lado2 == lado3) && (lado3 == lado1)) {
        System.out.println("O triângulo é equilátero.");
    }
    
    if ((lado1 != lado2) && (lado2 != lado3) && (lado3 != lado1)) { //O != significa diferente
        System.out.println("O triângulo é escaleno.");
    }
    
    if ((lado1 == lado2) || (lado2 == lado3) || (lado3 == lado1)) {
        System.out.println("O triângulo é isósceles.");
    }

Can anyone tell me the problem?

1 answer

0


You can do it this way:

if (x == y && x == z){
    System.out.println("Três lados iguais . Trata-se de um Triangulo Equilatero");
}else if(x == y || x == z || y==z ){
    System.out.println("Dois lados iguais . Trata-se de um Triangulo Isosceles");
    }else
    System.out.println("Três lados diferentes.");

simpler and achieves the goal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.