KolibreathMe 2017-02-15 12:45 采纳率: 0%
浏览 2299

PAT 返回非零 不知道哪里有问题 本地样例可以运行

题目:
读入n名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。

输入格式:每个测试输入包含1个测试用例,格式为

第1行:正整数n
第2行:第1个学生的姓名 学号 成绩
第3行:第2个学生的姓名 学号 成绩
... ... ...
第n+1行:第n个学生的姓名 学号 成绩
其中姓名和学号均为不超过10个字符的字符串,成绩为0到100之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的。
输出格式:对每个测试用例输出2行,第1行是成绩最高学生的姓名和学号,第2行是成绩最低学生的姓名和学号,字符串间有1空格。

 import java.util.*;

/**
 * Created by kolibreath on 2017/2/15.
 */
public class Main implements Comparator {

    private int flag =0;
    public static void main(String args[]){
        List<Student> list = new ArrayList<Student>();
        List<String> list1 = new ArrayList<String>();
        Scanner sc1 = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);
        int total = sc1.nextInt();
        for (int i=0;i<total;i++){
            String studentinfo = sc2.nextLine();
            String infos[]  = studentinfo.split(" ");
            Student student= new Student(infos[0],infos[1],infos[2]);
            list.add(student);
        }
        //sort中第二个参数放一个实现了compare接口的类
        Main main = new Main();
        Collections.sort(list,main);
        Student student1 = list.get(total-1);
        Student student2 = list.get(0);
        list1.add(student2.getName()+" "+student2.getMajor());
        list1.add(student1.getName()+" "+student1.getMajor());
        for (int i=0;i<list1.size();i++){
        System.out.println(list1.get(i));
        }

    }

    @Override
    public int compare(Object o1, Object o2) {
        Student student1 = (Student)o1;
        Student student2 = (Student)o2;
        int score1 = Integer.parseInt(student1.getScore());
        int score2 = Integer.parseInt(student2.getScore());

        //compareto比较的是String类型 返回值有0,1,-1
        if (score1>score2){
            flag = -1;
        }
        if (score1==score2){
            flag = 0;
        }
        if (score1<score2){
            flag = 1;
        }
        return flag;
    }

    static class Student{
        private String name;
        private String major;
        private String score;
        public Student(String name, String major, String score){
            this.name = name;
            this.major= major;
            this.score=score;
        }
        public String getName(){
            return name;
        }
        public String getMajor(){
            return major;
        }
        public String getScore(){
            return score;
        }
    }
}


  • 写回答

1条回答

  • threenewbee 2017-02-15 14:58
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3