#//设计一个类Student,该类包姓名、学号和成绩。设计一个方法按照成绩从高到低的顺序输出姓名、学号和成绩信息。
public class Student {
private String name;//姓名
private int id;//学号
private double score;//成绩
public Student(String name, int id, double result) {//构造方法
this.name = name;
this.id = id;
this.score = result;
}
//生成 set get 方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getResult() {
return score;
}
public void setResult(double score) {
this.score = score;
}
public static void sort(Student[] stus) {//数组方法
Student stu;
for (int i = 0; i < stus.length - 1; i++) {//冒泡比较
for (int j = 0; j < stus.length - i - 1; j++) {
if (stus[j].score < stus[j + 1].score) {
stu = stus[j];
stus[j] = stus[j + 1];
stus[j + 1] = stu;
}
}
}
}
public void show() {
System.out.println(id + name + score);
}
public static void main(String[] args) {
Student[] stus = new Student[3];
stus[0] = new Student("路飞", 1, 100);
stus[1] = new Student("山治", 3, 80);
stus[2] = new Student("索龙", 2, 99);
Student.sort(stus);
for (Student stu : stus) {
stu.show();
}
}
}