Last active
May 10, 2016 14:14
-
-
Save igordcsouza/3c7682fc48cceb8b14e3f3d3a32572d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ordenanomeidade; | |
import java.util.Scanner; | |
public class OrdenaNomeIdade { | |
private static final int TAMANHO = 10; | |
public static void main(String[] args) { | |
String nome[] = new String[TAMANHO]; | |
int idade[] = new int[TAMANHO]; | |
int menu = 0; | |
Scanner teclado = new Scanner(System.in); | |
// String[] nome = {"b","c","a","e","f","h","d","i","j","g"}; | |
// int[] idade = {10,1,42,12,42,44,1,2,9,100}; | |
for (int i= 0; i < 4; i++){ | |
System.out.println("Digite nome: "); | |
nome[i] = teclado.next(); | |
System.out.println("Digite a idade: "); | |
idade[i] = teclado.nextInt(); | |
} | |
while(menu != 3){ | |
System.out.println("Menu"); | |
System.out.println("1 - Ordenar por nome"); | |
System.out.println("2 - Ordenar por idade"); | |
System.out.println("3 - Sair"); | |
menu = teclado.nextInt(); | |
switch (menu) { | |
case 1: | |
ordena_nome(nome,idade); | |
break; | |
case 2: | |
ordena_idade(nome,idade); | |
break; | |
case 3: | |
break; | |
default: | |
System.out.println("Numero invalido!"); | |
break; | |
}; | |
} | |
} | |
public static void ordena_nome(String nome[], int idade[]){ | |
int aux_idade = 0; | |
String aux_nome; | |
for (int i= 0; i < TAMANHO; i++){ | |
for (int j = 0; j < (TAMANHO - 1); j++) { | |
int r = nome[j].compareToIgnoreCase(nome[j+1]); | |
if (r > 0) { | |
aux_idade =idade[j]; | |
idade[j] = idade[j+1]; | |
idade[j+1] = aux_idade; | |
aux_nome = nome[j]; | |
nome[j] = nome[j+1]; | |
nome[j+1] = aux_nome; | |
} | |
} | |
} | |
imprimir(nome,idade); | |
} | |
public static void ordena_idade(String nome[], int idade[]){ | |
int aux_idade = 0; | |
String aux_nome; | |
for (int i=0; i < TAMANHO; i++){ | |
for (int j = 0; j < (TAMANHO - 1); j++) { | |
if(idade[j] > idade[j + 1]){ | |
aux_idade =idade[j]; | |
idade[j] = idade[j+1]; | |
idade[j+1] = aux_idade; | |
aux_nome = nome[j]; | |
nome[j] = nome[j+1]; | |
nome[j+1] = aux_nome; | |
} | |
} | |
} | |
imprimir(nome,idade); | |
} | |
public static void imprimir(String nome[], int idade[]){ | |
for (int i=0; i< TAMANHO; i++){ | |
System.out.println("Nome = " + nome[i] + " ; Idade = " + idade[i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment