日期:2014-05-20  浏览次数:20711 次

随机生成5个数字,然后对其排序
随机生成5个数字,然后对其排序,请用java写,谢谢

------解决方案--------------------
Java code
public class Sort {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int[] numArr = new int[5];
        Random r = new Random();
        System.out.println("排序前的数据为:");
        for(int i=0; i<5; i++) {
            numArr[i] = r.nextInt();
            System.out.println(numArr[i]);
        }
        

        //冒泡排序
         for(int i=0; i<numArr.length; i++) {  
            for(int j=i+1; j<numArr.length; j++) {  
                if(numArr[i] < numArr[j]) {//降序排列  
                    int temp = numArr[i];  
                    numArr[i] = numArr[j];  
                    numArr[j] = temp;  
                }  
            }  
        }  
         
         System.out.println("排序后的数据为:");
         for(int i=0; i<5; i++){
             System.out.println(numArr[i]);
         }
    }

}