java中關於冒泡排序

package com.yun.java.six;

import java.util.Scanner;

public class P5 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /*
         * created by sun 17/6/2
         * 冒泡排序輸出成績
         */
        System.out.println("請輸入十個數字:");
        Scanner input=new Scanner(System.in);
        int[] scores=new int[10];//定義數組接收輸入的成績
        for (int i = 0; i <scores.length; i++) {
            System.out.println("請輸入第"+(i+1)+"個人的成績");
            scores[i]=input.nextInt();
        }
        //冒泡排序重新排列成績
        System.out.println("排序後的成績爲:");
        for (int i = 0; i < scores.length-1; i++) {
            for (int j = 0; j < scores.length-1-i; j++) {
                if (scores[j]>scores[j+1]) {
                    int num=scores[j];
                    scores[j]=scores[j+1];
                    scores[j+1]=num;
                }
            }
        }
        for (int i = 0; i < scores.length; i++) {
            System.out.print(scores[i]+" ");
        }
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章