Alguem sabe o erro

2 respostas
marcelo_mococa

To tentando ordenar uma array, mas naum ta dando certo.
Alguem me ajude a achar o erro…

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class teste {

	public static void main(String[] args) throws NumberFormatException, IOException {

		int[] array  = new int[3];
		int aux;
		
		BufferedReader leitor = new BufferedReader(new InputStreamReader (System.in));
		
		for (int i=0; i<array.length; i++){
			System.out.println("Digite o valor para a posicao " + i);
			array[i] = Integer.parseInt(leitor.readLine());
		}
		
		for (int i=0; i < array.length-1; i++) { 
			for(int j=i+1; j < array.length; j++ ) { 
				if ( array[i] < array[j] ) { 
					aux = array[i]; 
					array[i] = array[j]; 
					array[j] = array[i];
					} 
				} 
			}
		for (int i=0; i<array.length; i++){
			System.out.println(array[i]);
		}

	}

}

2 Respostas

rodrigo_gomes

Olá,

nao ta dando certo como?

_Renatu
for (int i=0; i < array.length-1; i++) { 
 			for(int j=i+1; j < array.length; j++ ) { 
 				if ( array[i] < array[j] ) { 
 					aux = array[i]; 
 					array[i] = array[j]; 
 					array[j] = array[i];
 					} 
 				} 
 			}

da uma olhada nesse seu codigo…
primeiro… vc quer ordenar em ordem crescente ou decrescente??
se for crescente… troque o array[i] < array[j] para array[i]>array[j]

outra coisa… vc nao achou estranho vc guardar o valor de array[i] numa variavel auxiliar e nao usar???
entao provavelmente seu codigo vai ter q fazer:

aux = array[i];
array[i]=array[j];
array[j]=aux;

ve se com essas modificacoes fica tudo certo…

Criado 13 de agosto de 2005
Ultima resposta 13 de ago. de 2005
Respostas 2
Participantes 3