數組排序Arrays.sort()

需要import java.util.Arrays
然後將需要排序的數組傳入Arrays.sort(scores)
結果會按升序排

import java.util.Arrays;
public class HelloWorld {

    //完成 main 方法
    public static void main(String[] args) {
        int[] scores = {89 , -23 , 64 , 91 , 119 , 52 , 73};
        HelloWorld hello = new HelloWorld();
        hello.rank(scores);


    }

    //定義方法完成成績排序並輸出前三名的功能
    public void rank(int[] scores){
        int j = 0;
        Arrays.sort(scores);
        for(int i=scores.length-1; j<3; i--){
            if((scores[i] >0) && (scores[i] <=100)){
                System.out.println(scores[i]);
                j++;
            }
        }
    }


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