利用treeset从键盘获取学生成绩并排序打印

main函数 


class hello {
	public static void main(String[] args) throws ParseException {
	
		Scanner sc = new Scanner(System.in);
		
		TreeSet<Score> ts = new TreeSet<>(new Comparator<Score>() {
			@Override
			public int compare(Score s1, Score s2) {
				// TODO Auto-generated method stub
				int num = s2.getResult() - s1.getResult();
				return num == 0 ? 1 : num;
			}
		});
		while(ts.size()<3) {
			String s = sc.nextLine();
			String [] sum = s.split(",");
			int chinese = Integer.parseInt(sum[1]);
			int math = Integer.parseInt(sum[2]);
			int english = Integer.parseInt(sum[3]);
			ts.add(new Score(sum[0],chinese,math,english));
		}
		System.out.println(ts);

Score类

 

package heima2;

public class Score {
	private String name;
	private int chinese,math,english;
	private int result = chinese + math + english;
	public Score(String name,int chinese,int math,int english) {
		this.name = name;
		this.chinese = chinese;
		this.math = math;
		this.english = english;
		this.result = math + chinese + english;
	}
	
	public Score() {
		// TODO Auto-generated constructor stub
	}

	public int getResult() {
		return result;
	}
	public void setResult(int result) {
		this.result = result;
	}
	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;
	}
	@Override
	public String toString() {
		return "name=" + name + ", chinese=" + chinese + ", math=" + math + ", english=" + english + ",RESULT="+result;
	}
	
}

运行结果:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值