版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40567229/article/details/85172575
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;
}
}
运行结果: