0
I believe I may be the result of my ordination that is influencing the output, eg if it was 6 8 10 should show RECTANGULAR TRIANGLE but is showing ACUTANGULO TRIANGLE.
The code I made at the end of the statement
Read 3 decimal numbers A, B and C and ordinances in descending order, from mode that side A represents the largest of the 3 sides. Next, determine the type of triangle these three sides form, based on the following cases, always writing an appropriate message:
if A B+C, display message: NO TRIANGLE
if A 2 = B 2 + C 2, display the message: RECTANGULAR TRIANGLE
if A 2 > B 2 + C 2, display message: OBTUSE TRIANGLE
if A 2 < B 2 + C 2, display message: ACUTANGULO TRIANGLE
if the three sides are equal, display the message: TRIANGLE EQUILATERAL
if only two sides are equal, display the message: ISOSCELE TRIANGLE
Ex Inputs (A, B, C) Expected output 7 5 7
ACUTANGULO ISOSCELE TRIANGLE 6 6 10 OBTUSANGULE TRIANGLE ISOSCELE TRIANGLE6 6 ACUTANGULO TRIANGLE EQUILATERAL TRIANGLE
5 7 2 NO TRIANGLE SHAPE
6 8 10 RECTANGULAR TRIANGLE
Code
public class Ex18 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a,b,c,maior;
System.out.printf("Digite o valor de A: ");
a=sc.nextDouble();
System.out.printf("Digite o valor de B: ");
b=sc.nextDouble();
System.out.printf("Digite o valor de C: ");
c=sc.nextDouble();
if(a > b){
maior = a;
a = b;
b = maior;
}if(b > c){
maior = b;
b = c;
c = maior;
}
if(a>= c+b){
System.out.println("NAO FORMA TRIANGULO");
}else if((a*a) == (b*b)+(c*c)){
System.out.println("TRIANGULO RETANGULO");
}else if((a*a)>(b*b)+(c*c)){
System.out.println("TRIANGULO OBTUSANGULO");
}else if((a*a)<(b*b)+(c*c)){
System.out.println("TRIANGULO ACUTANGULO");
}
if(a >= b+c){
System.out.println("NAO FORMA TRIANGULO");
}else if( a == b && a == c){
System.out.println("TRIANGULO EQUILATERO");
}else if(a == b || b == c || c == a){
System.out.println("TRIANGULO ISOSCELES");
}else{
System.out.println("");
}
}
}
If a=6, b=8 and c=10, it does not enter the first 2
if
's (if(a > b)
andif(b > c)
). Then the nextif
also fails (if (a >= c + b)
), for 6 is not greater than 8 + 10. Bothif
'the following ones also fail, because 6 squared is not greater than nor equal to 8 squared plus 10 squared. It will only enter theif
following (acutangulo). The problem is in causinga
be the hypotenuse (i.e., the logic of the first 2if
's that is wrong)– hkotsubo
Thanks for the tip, I’ll review the logic.
– Andre Viana
I believe you are calculating the values with incorrect ordering. This calculation "(aa) == (bb)+(c*c)" is the pitagoras theorem, so "a" will have to be the highest side (hypotenuse) and "b" and "c" will be the catects. The correct ordering would be decreasing: a=10, b=8 and c=6.
– Keoma