• 【TreeSet:键盘录入5个学生信息,按照总分从高到低输出到控制台】


    package com.yjf.esupplier.common.test;
    
    import java.util.Comparator;
    import java.util.Scanner;
    import java.util.TreeSet;
    
    /**
     * @author shusheng
     * @description 键盘录入5个学生信息,按照总分从高到低输出到控制台
     * @Email shusheng@yiji.com
     * @date 2018/12/17 15:36
     */
    public class TreeSetDemo3 {
    
        public static void main(String[] args) {
    
            // 创建一个TreeSet集合
            TreeSet<Student3> ts = new TreeSet<Student3>(new Comparator<Student3>() {
                @Override
                public int compare(Student3 s1, Student3 s2) {
                    // 总分从高到低
                    int num = s2.getSum() - s1.getSum();
                    // 总分相同的不一定语文相同
                    int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
                    // 总分相同的不一定数序相同
                    int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;
                    // 总分相同的不一定英语相同
                    int num4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3;
                    // 姓名还不一定相同呢
                    int num5 = num4 == 0 ? s1.getName().compareTo(s2.getName()) : num4;
                    return num5;
                }
            });
    
            System.out.println("学生信息录入开始");
    
            // 键盘录入5个学生信息
            for (int x = 1; x <= 5; x++) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入第" + x + "个学生的姓名:");
                String name = sc.nextLine();
                System.out.println("请输入第" + x + "个学生的语文成绩:");
                String chineseString = sc.nextLine();
                System.out.println("请输入第" + x + "个学生的数学成绩:");
                String mathString = sc.nextLine();
                System.out.println("请输入第" + x + "个学生的英语成绩:");
                String englishString = sc.nextLine();
    
                // 把数据封装到学生对象中
                Student3 s = new Student3();
                s.setName(name);
                s.setChinese(Integer.parseInt(chineseString));
                s.setMath(Integer.parseInt(mathString));
                s.setEnglish(Integer.parseInt(englishString));
                // 把学生对象添加到集合
                ts.add(s);
            }
    
            System.out.println("学习信息从高到低排序如下:");
            System.out.println("姓名	语文成绩	数学成绩	英语成绩");
            // 遍历集合
            for (Student3 s : ts) {
                System.out.println(s.getName() + "	" + s.getChinese() + "	"
                        + s.getMath() + "	" + s.getEnglish());
            }
        }
    }
    
    class Student3 {
    
        // 姓名
        private String name;
        // 语文成绩
        private int chinese;
        // 数学成绩
        private int math;
        // 英语成绩
        private int english;
    
        public Student3(String name, int chinese, int math, int english) {
            super();
            this.name = name;
            this.chinese = chinese;
            this.math = math;
            this.english = english;
        }
    
        public Student3() {
            super();
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getChinese() {
            return chinese;
        }
    
        public void setChinese(int chinese) {
            this.chinese = chinese;
        }
    
        public int getMath() {
            return math;
        }
    
        public void setMath(int math) {
            this.math = math;
        }
    
        public int getEnglish() {
            return english;
        }
    
        public void setEnglish(int english) {
            this.english = english;
        }
    
        public int getSum() {
            return this.chinese + this.math + this.english;
        }
    }
    终身学习者
  • 相关阅读:
    二分查找算法
    java多线程模拟停车位问题
    ECShop模板原理
    linux 常用命令 自己 积累
    ubuntu忘记root密码解决
    centos下一键安装lamp环境,快捷,方便
    腾讯php部分面试题答案
    PHP面试题集
    php笔试题
    流行的php面试题及答案
  • 原文地址:https://www.cnblogs.com/zuixinxian/p/10340938.html
Copyright © 2020-2023  润新知