Doesn’t show the right way out

Asked

Viewed 149 times

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 TRIANGLE

6 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) and if(b > c)). Then the next if also fails (if (a >= c + b)), for 6 is not greater than 8 + 10. Both if'the following ones also fail, because 6 squared is not greater than nor equal to 8 squared plus 10 squared. It will only enter the if following (acutangulo). The problem is in causing a be the hypotenuse (i.e., the logic of the first 2 if's that is wrong)

  • Thanks for the tip, I’ll review the logic.

  • 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.

1 answer

0

I think I found the solution, my idea was to sort the sides in a vector, since the library Arrays has the method sort(), it sorts the numbers of the vector in descending order.

For the vector to be ordered in ascending order just multiply the array by -1, follow the code:

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();

//nomeia o vetor
//exemplo 1, 2, 3
double lados[] = {a,b,c};

//multiplica os valores por -1
//exemplo -1, -2, -3
for(int i=0;i<lados.length;i++) {
    lados[i]=-lados[i];            
}

//ordena os dados em ordem decrescente
//exemplo -3, -2, -1
Arrays.sort(lados); 

//multiplica os valores por -1 denovo
//exemplo 3, 2, 1
for(int i=0; i<3; i++){
    lados[i] =-lados[i];
}        
if(lados[0]>= lados[1]+lados[2]){
   System.out.println("NAO FORMA TRIANGULO");

}else if((lados[0]*lados[0]) == (lados[1]*lados[1])+(lados[2]*lados[2])){
    System.out.println("TRIANGULO RETANGULO");

}else if((lados[0]*lados[0])>(lados[1]*lados[1])+(lados[2]*lados[2])){
    System.out.println("TRIANGULO OBTUSANGULO");

}else if((lados[0]*lados[0])<(lados[1]*lados[1])+(lados[2]*lados[2])){
    System.out.println("TRIANGULO ACUTANGULO"); 

}

if(lados[0] >= lados[1]+lados[2]){

}else if( lados[0] == lados[1] && lados[0] == lados[2]){
    System.out.println("TRIANGULO EQUILATERO");

}else if(lados[0] == lados[1] || lados[1] == lados[2] || lados[2] == lados[0]){
    System.out.println("TRIANGULO ISOSCELES");
}else{
    System.out.println("");
}
  • Since you only have three values, maybe it’s simpler to do if (a < b) { troca os valores de a e b } and then if (a < c) { troca os valores de a e c} (the exchange is as you were already doing). With this you already guarantee that a will be greater than b and c (and you don’t even have to order b and c, for whatever the order of these, in your case).

Browser other questions tagged

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