package com.heima.test;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
public class Test12 {
/**录入5名学生成绩 按总分排序
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileWriter file = new FileWriter("stu.txt");
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int num = o2.getSum() - o1.getSum();
return num == 0? 1: num;
}
});
Scanner sc = new Scanner(System.in);
System.out.println("输入5个学生的成绩(格式:名字,语文,数学,英语): ");
while(ts.size() < 5){
try{
String line = sc.nextLine();
String arr[] = line.split(",");
int chinese = Integer.parseInt(arr[1]);
int math = Integer.parseInt(arr[2]);
int english = Integer.parseInt(arr[3]);
ts.add(new Student(arr[0],chinese,math,english));
}catch (Exception e) {
// TODO: handle exception
System.out.println("输入格式有误!请重输:");
}
}
System.out.println(ts);
for (Student student : ts) {
file.write(student.toString() + "\n");
}
file.close();
}
}
录入5名学生成绩 按总分排序
最新推荐文章于 2023-10-28 19:56:18 发布