packagecom.lianxi1;importjava.io.BufferedWriter;importjava.io.FileWriter;importjava.io.IOException;importjava.util.Comparator;importjava.util.Scanner;importjava.util.TreeMap;importjava.util.TreeSet;public classStudnentDemo {public static voidmain(String[] args) {
TreeSet treeSet = new TreeSet<>(new Comparator() {
@Overridepublic intcompare(Student s1, Student s2) {int num1=s2.getSun()-s1.getSun();int num2=num1==0?s2.getChinese()-s1.getChinese():num1;int num3=num2==0?s2.getMath()-s1.getMath():num2;int num4=num3==0?s2.getEnglish()-s1.getEnglish():num3;int num5=num4==0?s2.getName().compareTo(s1.getName()):num4;returnnum5;
}
});
Scanner s=newScanner(System.in);for(int i=1;i<=5;i++){
System.out.println("请输入第"+i+"个学生的姓名:");
String name=s.nextLine();
System.out.println("请输入第"+i+"个学生的语文成绩:");
String chinese=s.nextLine();
System.out.println("请输入第"+i+"个学生的数学成绩:");
String math=s.nextLine();
System.out.println("请输入第"+i+"个学生的英语成绩");
String english=s.nextLine();
Student stu=newStudent(name,Integer.parseInt(chinese),Integer.parseInt(math),Integer.parseInt(english));
treeSet.add(stu);
}
BufferedWriter bw=null;try{
bw= new BufferedWriter(new FileWriter("stuInfo.txt"));
bw.write("学生成绩如下:");
bw.newLine();
bw.write("姓名,语文,数学,英语 ");
bw.newLine();
bw.flush();for(Student stu : treeSet) {
StringBuilder sb=newStringBuilder();
sb.append(stu.getName()).append(",").append(stu.getChinese()).append(",").append(stu.getMath())
.append(",").append(stu.getEnglish());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
System.out.println("学生信息录入完毕");
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}finally{if(bw!=null){try{
bw.close();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}