2、Arrays類排序陣列
需求說明:正確使用Arrays的sort()方法為陣列進行排序,效果如下圖所示。降序排列的寫法可以參考for(int i=scores.length-1;i>=0;i–){}
package homework;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
public class 作業2Arrays類排序陣列 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("請輸入5名學員的成績:");
Scanner scanner=new Scanner(System.in);
int xxx[]=new int[5];
for (int i = 0; i < xxx.length; i++) {
int scores = scanner.nextInt();
xxx[i]=scores;
}
Arrays.sort(xxx);
System.out.println("學員成績按升序排列:" );
for (int i = 0; i < xxx.length; i++) {
System.out.print(xxx[i]+"\t");
}
System.out.println();
System.out.println("學員成績按降序排列:");
for (int i = xxx.length-1; i>=0; i--) {
System.out.print(xxx[i]+"\t");
}
}
}