使用TreeSet集合存储学生姓名成绩,并按照总成绩从高到低排序。

  1. 学生基本信息类
package StudentScoreDemo;

public class Student {
    //学生姓名
    private String name;

    //语文成绩
    private int chinese;

    //数学成绩
    private int math;

    //英语成绩
    private int english;

    //无参构造
    public Student() {
        // TODO Auto-generated constructor stub
    }

    //带参构造
    public Student(String name, int chinese, int math, int english) {
        super();
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
    }

    //getXxx() setXxx()方法
    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;
    }

}






















2.存储学生信息,并排序

package StudentScoreDemo;

import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        //创建一个TreeSet集合
        TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                //总分从高到低
                int num = s2.getSum() - s1.getSum();
                //从低到高的排序比较器设置
                //int num = s1.getSum() - s2.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;
            }
        });

        //键盘录入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();

            //把数据封装到学生对象中
            Student s = new Student();
            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(Student s : ts){
            System.out.println(s.getName() + "\t" + s.getChinese()
                    + "\t" + s.getMath() + "\t" + s.getEnglish() + "\t" + s.getSum());
        }
    }
}














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值