题解 | #成绩排序#

成绩排序

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int num = sc.nextInt();

        ArrayList<Student> list = new ArrayList<>();

        int type = sc.nextInt();

        for (int i = 0; i < num; i++) {
            String name = sc.next();
            int score = sc.nextInt();
            list.add(new Student(name, score));
        }

        Comparator<Student> comparator = type == 1 ?
        new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return ((Student) o1).score - ((Student) o2).score;
            }
        } : new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return ((Student) o2).score - ((Student) o1).score;
            }
        };

        list.sort(comparator);

        for (Student student : list) {
            System.out.println(student.name + " " + student.score);
        }
    }
}

class Student {
    String name;
    Integer score;

    public Student() {
    }

    public Student(String name, Integer score) {
        this.name = name;
        this.score = score;
    }


}

成员变量最好还是私有化

全部评论

相关推荐

2024-12-27 23:45
已编辑
三江学院 Java
程序员牛肉:死局。学历+无实习+项目比较简单一点。基本就代表失业了。 尤其是项目,功能点实在是太假了。而且提问点也很少。第一个项目中的使用jwt和threadlocal也可以作为亮点写出来嘛?第二个项目中的“后端使用restful风格”,“前端采用vue.JS”,“使用redis”也可以作为亮点嘛? 项目实在是太简单了,基本就是1+1=2的水平。而你目标投递的肯定也是小厂,可小厂哪里有什么培养制度,由于成本的问题,人家更希望你来能直接干活,所以你投小厂也很难投。基本就是死局,也不一定非要走后端这条路。可以再学一学后端之后走测试或者前端。 除此之外,不要相信任何付费改简历的。你这份简历没有改的必要了,先沉淀沉淀
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务