大侠(JAVA高手)帮忙解一道编程题目!跪谢···

2024-11-07 22:47:57
推荐回答(1个)
回答(1):

你要是不涉及到数据库和WEB应用,只要求按成绩高低排名并输出到控制台的话,那就简单了,如下可参考:
import java.util.*;
public class T1109 {
public static void main(String[] args) {
Setstudents = new TreeSet();
students.add(new Student(1,"学生1",65));
students.add(new Student(2,"学生2",85));
students.add(new Student(3,"学生3",56));
students.add(new Student(4,"学生4",98));
students.add(new Student(5,"学生5",33));
students.add(new Student(6,"学生6",74));
students.add(new Student(7,"学生7",45));
students.add(new Student(8,"庆枣学生8",88));
students.add(new Student(9,"学生9",72));
System.out.println("学号"+"\t"+"姓名"+"兆答\t"+"成绩"+"\t"+"名次誉猜拆");
int index = 1;
for(Student s : students){
System.out.println(s.getNumber()+"\t"+s.getName()+"\t"+s.getScore()+"\t"+index);
index++;
}
}
}
class Student implements Comparable {
private Integer number;
private String name;
private int score;
public Student(int number, String name, int score) {
this.number = number;
this.name = name;
this.score = score;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public int compareTo(Student o) {
if (score > o.score)
return -1;
else if (score < o.score)
return 1;
else {
return number.compareTo(o.number);
}
}
}