2
I need to put five pesos in order from the smallest to the largest. Defining the numbers instead for example: int vet[] = {3,4,2,8,7,1}; it runs right, but I need to do it this way: int vet[] = new int [5]; that way he’s only getting 3 numbers like this showing in the image. follows the code:
int vet[] = new int [5];
int aux;
boolean controle;
for(int i = 0; i < vet.length; i++ ) {
vet[i] = Integer.parseInt(JOptionPane.showInputDialog("Digite o peso: "));
controle = true;
for(int j = 0; j < (vet.length -1); j++) {
if(vet[j] > vet[j+1]) {
aux = vet[j];
vet[j] = vet[j + 1];
vet[j + 1] = aux;
controle = false;
}
}
if(controle) {
break;
}
}
for(int i = 0; i < vet.length; i++) {
System.out.println(vet[i] + "");
}
It would not be better to divide this into two parts, where the first reads the values and places them in the array and the second sorts them?
– Victor Stafusa