/**
* 有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
* 输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中。
* 要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter("stu.txt"));
System.out.println("请输入学生成绩格式是:姓名,语文成绩,数学成绩,英语成绩");
//创建TreeSet集合对象,在TreeSet的构造函数中传入比较器,按照总分比较
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s2.getSum() - s1.getSum();
return num == 0 ? 2 : num;
}
});
//录入五个学生,所以以集合中的学生个数为判断条件,如果size是小于5就进行存储
while(ts.size() < 5) {
//将录入的字符串切割,用逗号切割,会返回一个字符串数组,将字符串数组中从二个元素转换成int数,
String line = bd.readLine();
String[] arr = line.split(",");
int chinese = Integer.parseInt(arr[1]);
int math = Integer.parseInt(arr[2]);
int english = Integer.parseInt(arr[3]);
//将转换后的结果封装成Student对象,将Student添加到TreeSet集合中
ts.add(new Student(arr[0], chinese, math, english));
}
//遍历TreeSet集合打印每一个Student对象
for (Student s : ts) {
System.out.println(s);
}
}
}
class Student {
private String name;
private int chinese;
private int math;
private int english;
private int sum;
public Student() {
super();
}
public Student(String name, int chinese, int math, int english) {
super();
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
this.sum = this.chinese + this.math + this.english;
}
public int getSum() {
return sum;
}
public String toString() {
return name + "," + chinese + "," + math + "," + english + "," + sum;
}
/**
* 有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
* 输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中。
* 要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
*
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new FileWriter("stu.txt"));
//System.out.println("请输入学生成绩格式是:姓名,语文成绩,数学成绩,英语成绩");
// 创建TreeSet集合对象,在TreeSet的构造函数中传入比较器,按照总分比较
TreeSet<Student> ts = new TreeSet<Student>();
// 录入五个学生,所以以集合中的学生个数为判断条件,如果size是小于5就进行存储
while (ts.size() < 5)
{
// 将录入的字符串切割,用逗号切割,会返回一个字符串数组,将字符串数组中从二个元素转换成int数,
String line = bd.readLine();
String[] arr = line.split(",");
int chinese = Integer.parseInt(arr[1]);
int math = Integer.parseInt(arr[2]);
int english = Integer.parseInt(arr[3]);
// 将转换后的结果封装成Student对象,将Student添加到TreeSet集合中
ts.add(new Student(arr[0], chinese, math, english));
}
// 遍历TreeSet集合打印每一个Student对象
for (Student s : ts)
{
//System.out.println(s);
bw.write(s.toString());
bw.newLine();
}
bd.close();
bw.close();
}
}
class Student implements Comparable
{
private String name;
private int chinese;
private int math;
private int english;
private int sum;
public Student(String name, int chinese, int math, int english)
{
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
sum = chinese + math + english;
}
public int getSum()
{
return sum;
}
public String toString()
{
return name + "," + chinese + "," + math + "," + english + "," + sum;
}
public int compareTo(Object obj)
{
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s = (Student)obj;