Does not show the correct output

0

I believe that I can be the result of my ordering that is influencing the output, eg if it was 6 8 10 it should show TRIANGLE RETURN but it is showing ACTUALLY TRIANGLE

The code I made is at the end of the statement

  

Read 3 decimal numbers A, B, and C, and order them in descending order from   so that side A represents the largest of the 3 sides. Then determine   the type of triangle that these three sides form, based on the   following cases, always writing an appropriate message:

     

If A ≥ B + C, display the message: DO NOT TRIANGLE

     

If A 2 = B 2 + C 2, display the message: TRIANGLE RETANGLE

     

if A 2 > B 2 + C 2, present the message: TRIANGULO OBTUSANGULO

     

if A 2 < B 2 + C 2, display the message: TRIANGLE ACUTANGULO

     

If all three sides are the same, display the message: TRIANGLE   EQUILATERO

     

If only two sides are the same, display the message:   TRIANGULO ISOSCELES

     

Ex-Entries (A, B, C) Expected Exit 7 5 7
  ACTUALLY TRIANGLE                                   TRIANGULO ISOSCELES 6 6 10 TRIANGULO OBTUSANGULO                                   TRIANGULO ISOSCELES

     

6 6 6 ACTUALLY TRIANGLE                                   TRIANGLE EQUILATERAL

     

5 7 2 DO NOT FORM TRIANGLE

     

6 8 10 TRIANGLE RETANGULO

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


       }

    }
    
asked by anonymous 15.09.2018 / 21:38

1 answer

0

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

In order for the vector to be ordered in ascending order, simply 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("");
}
    
16.09.2018 / 17:09