Identifying triangles in Java

Asked

Viewed 547 times

0

I’m doing java exercise, but I can’t find a logic for this: Receive 6 numbers representing measures a,b,c,d,e,f and relate how many and which triangles can be formed using these measures.(To form a triangle the sum of the shorter sides must be greater than the measure of the longer side) Output example [abc], [Abd]... I can make logic with 3 values only

System.out.print("Informe a primeira medida: ");
int a = sc.nextInt();
System.out.print("Informe a segunda medida: ");
int b = sc.nextInt();
System.out.print("Informe a terceira medida: ");
int c = sc.nextInt();
System.out.print("Informe a quarta medida: ");
int d = sc.nextInt();
System.out.print("Informe a quinta medida: ");
int e = sc.nextInt();
System.out.print("Informe a sexta medida: ");
int f = sc.nextInt();

if((a+b>c) && (a+c>b) && (b+c>a)){
    if(a == b && a == c){
        System.out.println("Triangulo equilatero.");
    }
    else if((a == b) || (a == c) || (b == c)){
        System.out.println("Triangulo isosceles.");
    }
    else{
        System.out.println("Triangulo escaleno.");
    }
    System.out.println("Medidas usadas: [abc]");
}

else{
    System.out.println("Nao forma triangulo.");
}

2 answers

0

What you want to do is a permutation, and for that you will have q work with arrays, this link has a little about permutation https://www.devmedia.com.br/permutacoes-de-objetos-um-algoritmo-recursivo-em-java/27512

On the site has this code, you can use the same logic and implement with your

public class Permutacoes {

//numero da permutacao atual
private static int cont=0; 

//armazena a permutacao corrente
private static char[] p;    


/**
 * metodo principal: recebe o vetor cujos elementos que serao permutados
 * @param vet
 */
public static void permuta(char [] vet) {

    p = new char[vet.length];
    permuta(vet,0);
}


/**
 * método recursivo que implementa as permutacoes
 * @param vet
 * @param n
 */
private static void permuta(char []vet, int n) {

    if (n==vet.length) {
        cont++;
        imprime();

    } else {

        for (int i=0; i < vet.length; i++) {

            boolean achou = false;

            for (int j = 0; j < n; j++) {

                if (p[j]==vet[i]) achou = true;
            }

            if (!achou) {

                p[n] = vet[i];
                permuta(vet,n+1);
            }

        } //--for

    } //--if/else

} //--permuta


/** imprime a permutacao corrente */
private static void imprime() {

    System.out.println();
    System.out.print("(" + cont + ") : ");
    for (int i=0; i < p.length; i++) System.out.print(p[i] + " ");

} //--imprime


/** metodo principal para teste da classe */
public static void main(String[] args) {

    char v[] = {'A','B','C', 'D'};
    Permutacoes.permuta(v);
}

}

-2

package fundamentos;

import java.util.Scanner;

public class IdentificandoTriangulo {
    
    public static void main(String[] args) {
        
        Scanner entrada = new Scanner(System.in);
        
        double x,y,z;
        
        System.out.println("Digite a medida do lado x: ");
        x=entrada.nextDouble();
        System.out.println("Digite a medida do lado y: ");
        y=entrada.nextDouble();     
        System.out.println("Digite a medida do lado z: ");
        z=entrada.nextDouble();
        

          if (x<=0 || y<=0 || z<=0) {
              System.out.println("Lados nulos ou negativos nao sao aceitos.");
           }

           if (x>=y+z || y>=z+x || z>=x+y) {
              System.out.println("Triangulo inexistente.");
              System.exit(3);
           }

           if (x==y && y==z)
              System.out.println("Triangulo equilatero.");

           else if (x==y || y==z || z==x)
              System.out.println("Triangulo isosceles.");

           else
              System.out.println("Triangulo escaleno.");

           System.exit(0);
           
           entrada.close();
           
    }

    
    
    
}

Browser other questions tagged

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