Tentando Resolver Questão Java

3 respostas
programaçãojava
G

Tô tentando fazer um código no qual o usuário digite 5 nomes, e o programa deverá apresentar na tela Quantas vezes o primeiro nome digitado aparece repetido, Quantos caracteres possui o maior nome digitado e Quantos caracteres possui o menor nome digitado . Só não consegui fazer aparecer na tela quantos caracteres possui o menor nome digitado. Alguém sabe me dizer qual o meu erro ??

import java.util.Scanner;

public class Questao{
public static void main(String [] args){

Scanner ler;
    int cont=1;
    String n1;
    String n2;
    String n3;
    String n4;
    String n5;
  
    ler = new Scanner(System.in);
    
    System.out.print("Digite primeiro nome: ");
    n1 = ler.nextLine();
    
    System.out.print("Digite o segundo nome: ");
    n2 = ler.nextLine();

    System.out.print("Digite o terceiro nome: ");
    n3 = ler.nextLine();

    System.out.print("Digite o quarto nome: ");
    n4 = ler.nextLine();
    
    System.out.print("Digite o Quinto nome: ");
    n5 = ler.nextLine();
    
    if(n1.equals(n2)){
    cont++;
        
    }
    if(n1.equals(n3)){
    cont++;
        
    }
     if(n1.equals(n4)){
    cont++;
        
    }
    if(n1.equals(n5)){
    cont++;
        
    }
    System.out.println("O primeiro nome digitado repete " +cont);
    
 //Maior Nome Digitado   
 if(n1.length() > n2.length() && n1.length() > n3.length() && n1.length() > n4.length() && n1.length() > n5.length()){
   System.out.println("O maior nome digitado possui "+n1.length()+ " caracteres"); 
 }
  if(n2.length() > n1.length() && n2.length() > n3.length() && n2.length() > n4.length() && n2.length() > n5.length()){
   System.out.println("O maior nome digitado possui "+n2.length()+ " caracteres"); 
 }
  if(n3.length() > n1.length() && n3.length() > n2.length() && n3.length() > n4.length() && n3.length() > n5.length()){
   System.out.println("O maior nome digitado possui "+n3.length()+ " caracteres"); 
 }
  if(n4.length() > n1.length() && n4.length() > n2.length() && n4.length() > n3.length() && n4.length() > n5.length()){
   System.out.println("O maior nome digitado possui "+n4.length()+ " caracteres"); 
 }
  if(n5.length() > n1.length() && n5.length() > n2.length() && n5.length() > n3.length() && n5.length() > n4.length()){
   System.out.println("O maior nome digitado possui "+n5.length()+ " caracteres"); 
 }

  // Menor Nome Digitado
  if(n1.length() < n2.length() && n1.length() < n3.length() && n1.length() < n4.length() && n1.length() < n5.length()){
   System.out.println("O menor nome digitado possui "+n1.length()+ " caracteres"); 
 }
  if(n2.length() < n1.length() && n2.length() < n3.length() && n2.length() < n4.length() && n2.length() < n5.length()){
   System.out.println("O menor nome digitado possui "+n2.length()+ " caracteres"); 
 }
  if(n3.length() < n1.length() && n3.length() < n2.length() && n3.length() < n4.length() && n3.length() < n5.length()){
   System.out.println("O menor nome digitado possui "+n3.length()+ " caracteres"); 
 }
  if(n4.length() < n1.length() && n4.length() < n2.length() && n4.length() < n3.length() && n4.length() < n5.length()){
   System.out.println("O menor nome digitado possui "+n4.length()+ " caracteres"); 
 }
  if(n5.length() < n1.length() && n5.length() < n2.length() && n5.length() < n3.length() && n5.length() < n4.length()){
   System.out.println("O menor nome digitado possui "+n5.length()+ " caracteres"); 
 }
  
    
}

}

3 Respostas

rcarvalhoalencar

Cara, aqui no meu computador apareceu.

run:
Digite primeiro nome: rafael
Digite o segundo nome: rafael
Digite o terceiro nome: joana
Digite o quarto nome: mike
Digite o Quinto nome: leandro
O primeiro nome digitado repete 2
O maior nome digitado possui 7 caracteres
O menor nome digitado possui 4 caracteres
BUILD SUCCESSFUL (total time: 23 seconds)

G

O problema é que quando digito mais de 3 palavras iguais não aparece o menor nome digitado. E se colocar todos os nomes iguais, não aparece nem o menor e nem o maior nome digitado.

I

Quando os dados são homogêneos (de mesmo tipo) e guardam uma relação (tem a ver), se pode usar arrays. Assim essas variáveis:

podem ser escritas assim;

String[] nomes = new String[5];

Exemplo:

import java.util.Scanner;

public class Questao {
	public static void main(String[] args){
		int contador = 0;
		int numerador = 1;
		String menorNome;
		String maiorNome;
		String[] nomes;
		Scanner leitor = new Scanner(System.in);
		
		System.out.print("Quantidade de nomes a serem lidos: ");
		nomes = new String[leitor.nextInt()];
		leitor.nextLine(); //Para 'fechar a linha'
		
		System.out.println("\nInforme os nomes");
		
		for(int i = 0; i < nomes.length; i++){
			System.out.print("Digite o " + (numerador++) + "º nome: ");
			nomes[i] = leitor.nextLine();
		}
		
		for(int i = 1; i < nomes.length; i++){
			if(nomes[0].equals(nomes[i]))
				contador++;
		}
		
		System.out.println("\nO primeiro nome se repete " + contador + " vezes!");
		
		menorNome = nomes[0];
		maiorNome = nomes[1];
		for(int i = 0; i < nomes.length; i++){
			if(nomes[i].length() > maiorNome.length())
				maiorNome = nomes[i];
			
			if(nomes[i].length() < menorNome.length())
				menorNome = nomes[i];
		}
		
		System.out.println("\nMaior nome: " + maiorNome + " com " + maiorNome.length() 
			+  " caracteres!");
		System.out.println("\nMenor nome: " + menorNome + " com " + menorNome.length() 
			+  " caracteres!");
	}
}

Rodando…

image

Criado 3 de maio de 2020
Ultima resposta 3 de mai. de 2020
Respostas 3
Participantes 3