要求:
1.创建学生类 并生成总成绩方法
package SetChengJi;
public class XueSheng {
String name;
int chinese;
int math;
int english;
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 XueSheng(String name, int chinese, int math, int english) {
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public XueSheng() {
super();
}
public int getsum(){
return chinese+math+english;
}
}
2.因TreeSet集合不能存放重复数据,所有用Comparator方法完善功能
package SetChengJi;
import java.util.Comparator;
public class XueShengCompare implements Comparator {
@Override
public int compare(XueSheng x1, XueSheng x2) {
//主要条件:总分
int sum1 = x2.getsum() - x1.getsum();
//次要条件①:总分+语文成绩
int sum2 = sum10 ? x2.getChinese()-x1.getChinese() : sum1;
//次要条件②:总分+语文成绩+数学成绩
int sum3 = sum20 ? x2.getMath()-x1.getMath() : sum2;
//次要条件③:总分+语文成绩+数学成绩+英语成绩
int sum4 = sum30 ? x2.getEnglish()-x1.getEnglish() : sum3;
//次要条件④:总分+语文成绩+数学成绩+英语成绩+姓名
int sum5 = sum40 ? x2.getName().compareTo(x1.getName()) : sum4;
return sum5;
}
}
3.测试类 存放数据 调动方法
package SetChengJi;
import java.util.Scanner;
import java.util.TreeSet;
public class Test {
public static void main(String[] args) {
set.add(xue);
}
for (XueSheng xueSheng : set) {
System.out.println("姓名:"+xueSheng.getName()+" 语文成绩"+xueSheng.getChinese()+" 数学成绩"+xueSheng.getMath()
+" 英语成绩"+xueSheng.getEnglish()+"总分:"+xueSheng.getsum());
}
}
}