首页 > 其他 > 详细

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

时间:2019-01-31 11:37:00      阅读:224      评论:0      收藏:0      [点我收藏+]
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("姓名\t语文成绩\t数学成绩\t英语成绩");
        // 遍历集合
        for (Student3 s : ts) {
            System.out.println(s.getName() + "\t" + s.getChinese() + "\t"
                    + s.getMath() + "\t" + 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;
    }
}

 

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

原文:https://www.cnblogs.com/zuixinxian/p/10340938.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!