package com.itheima.eaxm;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
public class Result {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请输入 姓名,语文,数学,英语");
TreeSet<Student> studentSet = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
// TODO Auto-generated method stub
int num = s2.getSum()-s1.getSum();
return num==0 ? 1 : num;
}
});
while(studentSet.size()<5){
int china = 0;
int math =0;
int english =0 ;
try{
String line = sc.nextLine();
String[] arr = line.split(",");
china = Integer.parseInt(arr[1]);
math = Integer.parseInt(arr[2]);
english = Integer.parseInt(arr[3]);
int sum = china+math+english;
studentSet.add(new Student(arr[0],china,math,english, sum));
}catch (Exception e) {
// TODO: handle exception
throw new RuntimeException("录入格式或字符串错误");
}
}
System.out.println();
BufferedWriter bw = new BufferedWriter(new FileWriter("stu.txt"));
for(Student stu : studentSet){
bw.write(stu.toString());
bw.newLine();
}
bw.close();
}
} |