题解 | #成绩排序#

成绩排序

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

import java.util.Scanner;

import java.util.Arrays;
import java.util.Comparator;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = in.nextInt();
        int sortType = in.nextInt();
        in.nextLine();
        StuScore[] param = new StuScore[count];
        int index=0;
        // 注意 hasNext 和 hasNextLine 的区别
        while (index<count) { // 注意 while 处理多个 case
            String line = in.nextLine();
            String[] pair = line.split(" ");
            param[index] = new StuScore(pair[0],Integer.parseInt(pair[1]));
            index++;
        }
        sortStu(param,sortType);
        Arrays.stream(param).forEach(i->System.out.println(i.name+" "+i.score ));
    }

    public static void sortStu(StuScore[] param, int sortType) {
        Arrays.sort(param, new Comparator<StuScore>() {
            public int compare(StuScore a, StuScore b) {
                if (sortType == 1) {
                    return Integer.compare(a.score, b.score);
                } else {
                    return -Integer.compare(a.score, b.score);
                }
            }
        });

    }
    public static class StuScore {
        private String name;
        private int score;
        public StuScore(String name,int score){
            this.name=name;
            this.score = score;
        }
    }
}

全部评论

相关推荐

冰皮月饼_FLORRIEEE:你是准备投产品嘛?可以重新整理一下实习的bulletpoint,侧重描述你的工作所带来的结果收益,不要只写泛泛的内容(比如改写通过xx数据分析,提升xx),产品的价值并不在处理和分析数据的过程
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务