根据分数对学生进行排名

时间:2019-03-17 07:01:19

标签: java comparator

我想问一下如何通过对它应用比较器来达到以下结果:

输入:

4 //此班级列表中的学生人数

学生1200 /// [姓名] [分数]

学生2 200

学生3 100

Student4 300

输出:

Student1 2 /// [姓名] [排名]

Student2 2

Student3 4

学生4 1

*获得相同分数的学生将处于同一排名

`

public class Ranking {
    private void run() {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        Student [] studentArray = new Student [number];

    for(int i=0;i<number;i++){
        String name = sc.next();
        int score = sc.nextInt();
        studentArray [i] = new Student(name, score, 0);
    }

    getRanks(number, studentArray);

    for(Student s: studentArray){
        System.out.println(s);
    }
}

public void getRanks(int number, Student [] studentArray){
    int ranking = 1;
    studentArray[0].setRank(ranking);
    for(int i = 1; i<number; i++){
        if(studentArray[i].getScore() != studentArray[i-1].getScore()){
            ranking ++;
            studentArray[i].setRank(ranking);
        }
    }
}

`

2 个答案:

答案 0 :(得分:1)

为什么不将数组转换为列表然后运行

Collections.sort(studentList, new Comparator<Student>() {
    public int compare(Student a, Student b) {
        return a.getScore() - b.getScore();
    }
});

https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)

此后,该列表中任何学生的索引将确定其排名。

您可以通过遍历列表来清洁等级,并将等级直接关联到每个学生,如果最后的分数等于当前分数,则不递增等级。

int lastScore = -1;
int rank = studentList.size() + 1;
for(Student s : studentList) {
    if(s.getScore() > lastScore) {
        rank—;
    }
    s.setRank(rank);
    lastScore = s.getScore();
}

更新: 要根据列表中学生的索引设置排名,请执行以下操作:

int lastScore = -1;
int rank = studentList.size() + 1;
int displayRank = rank;
for(Student s : studentList) {
    rank—;
    if(s.getScore() > lastScore) {
        displayRank = rank;
    }
    s.setRank(displayRank);
    lastScore = s.getScore();
}

答案 1 :(得分:1)

public class Ranking {
    private void run() {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        Student [] studentArray = new Student [number];

    for(int i=0;i<number;i++){
        String name = sc.next();
        int score = sc.nextInt();
        studentArray [i] = new Student(name, score, 0);
    }

    getRanks(number, studentArray);

    for(Student s: studentArray){
        System.out.println(s);
    }
}

public void getRanks(int number, Student [] studentArray){
    List<Student> studentList = Arrays.asList(studentArray);

    Comparator<Student> comparator = new Comparator<Student>() {
        public int compare(Student student1, Student student2) {
           // for descending order
            return student2.getScore() - student1.getScore(); 
        }
    };
    Collections.sort(studentList, comparator);

    int ranking = 1, displayRanking = 1;
    studentList.get(0).setRank(displayRanking);
    for(int i = 1; i<number; i++){
        ranking++;
        if(studentList.get(i).getScore() > studentList.get(i - 1).getScore()){
            displayRanking = ranking;
        }
        studentList.get(i).setRank(displayRanking);
    }
    for(int i = 0; i<number; i++){
        int index = sortedList.indexOf(studentArray[i]);
        studentArray[i].setRank(sortedList.get(index).getRank);
    }
}