需求说明:正确使用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");
}
}
}