题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

import java.util.*;


public class Main {
    public static int ORDER = 1;
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Integer count =  Integer.parseInt(in.nextLine());
        Integer orderInput = Integer.parseInt(in.nextLine());
        if(orderInput.intValue() == 0) ORDER = -1;
        Set<Student> set = new TreeSet<Student>();
        while (count-- > 0) { 
            String[] studentData = in.nextLine().split(" ");
            set.add(new Student(studentData[0], studentData[1]));
        }
        
        set.forEach(st -> System.out.println(st));
    }

}
class Student implements Comparable<Student> {
    String name;
    int score;
    public Student() {}
    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }
    public Student(String name, String score) {
        this.name = name;
        try {
            this.score = Integer.parseInt(score);
        } catch (Exception e) {
            this.score = 0;
        }
    }

    public String getName() {
        return this.name;
    }
    public int getScore() {
        return this.score;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setScore(int score) {
        this.score = score;
    }
    @Override
    public int compareTo(Student o) {
        return (this.score - o.getScore()) * Main.ORDER;
    }
    @Override
    public String toString(){
        return this.name + " " + this.score;
    }
}

全部评论

相关推荐

10-10 17:46
门头沟学院 Java
廖依然:所以能分我一个offer吗?求求了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务