显示学生姓名及成绩降序排列
代码如下:
package Day05;
import java.util.Scanner;
public class TestStudent {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter student number: ");
int stuNum = Integer.parseInt(sc.nextLine());
Student[] stu = new Student[stuNum];
for (int i = 0; i < stu.length; i++) {
System.out.println("Enter student " + i + "'s name: ");
String name = sc.nextLine();
System.out.println("Enter student " + i + "'s score: ");
double score = Double.parseDouble(sc.nextLine());
stu[i] = new Student(name, score);
}
arrangeStudent(stu);
printStudent(stu);
}
private static void printStudent(Student[] stu) {
for (int i = 0; i < stu.length; i++) {
System.out.println(stu[i].toString());
}
}
private static void arrangeStudent(Student[] stu) {
for (int i = 0; i < stu.length; i++) {
for (int j = i + 1; j < stu.length; j++) {
if (stu[i].getScore() < stu[j].getScore()) {
Student tempStu = null;
tempStu = stu[i];
stu[i] = stu[j];
stu[j] = tempStu;
}
}
}
}
}