1 import java.io.FileReader;
2 import java.io.FileWriter;
3 import java.io.IOException;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.LinkedList;
7 import java.util.Scanner;
8
9 public class Student
10 {
11 double chinese;
12 double math;
13 double english;
14 double sum;
15 String sname;
16
17 public Student ( double chinese, double math, double english, double sum, String sname )
18 {
19 this.chinese = chinese;
20 this.math = math;
21 this.english = english;
22 this.sum = sum;
23 this.sname = sname;
24 }
25
26 @Override
27 public String toString ()
28 {
29 return String.format ("%s\t\t%2$.1f\t\t\t%3$.1f\t\t\t%4$.1f\t\t\t%5$.1f", sname, chinese, math, english, sum);
30 }
31
32 public static void main ( String[] args )
33 {
34 Scanner scanner = new Scanner (System.in);
35 LinkedList<Student> list = new LinkedList<Student> ();
36 System.out.println ("从键盘输入学生的信息,输入格式为:name,30,30,30(姓名,<a href="https://www.baidu.com/s?wd=%E4%B8%89%E9%97%A8&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YvuhnkuWN-njmvnyPBPWnv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1DLPWT3PWR3rjRLPjfdrHRvP0" target="_blank" class="baidu-highlight">三门</a>课成绩)<直接回车结束>");
37 while (scanner.hasNextLine ())
38 {
39 String line = scanner.nextLine ().trim ();
40 if ("".equals (line))
41 {
42 break;
43 }
44 String[] info = line.split ("\\,");
45 String name = info[0];
46 double chinese = 0;
47 double math = 0;
48 double english = 0;
49 double sum = 0;
50 try
51 {
52 chinese = Double.parseDouble (info[1]);
53 math = Double.parseDouble (info[2]);
54 english = Double.parseDouble (info[3]);
55 sum = chinese + math + english;
56 }
57 catch (Exception e)
58 {
59 System.out.println ("格式不正确,重写输入:");
60 continue;
61 }
62 Student student = new Student (chinese, math, english, sum, name);
63 list.add (student);
64 }
65 scanner.close ();
66 Collections.sort (list, new Comparator<Student> ()
67 {
68 @Override
69 public int compare ( Student o1, Student o2 )
70 {
71 if (o1.sum > o2.sum)
72 {
73 return -1;
74 }
75 else if (o1.sum < o2.sum)
76 {
77 return 1;
78 }
79 else
80 {
81 return 0;
82 }
83 }
84 });
85 try
86 {
87 String file = "stu.txt";
88 String line = System.getProperty ("line.separator");
89 FileWriter fw = new FileWriter (file, true);
90 FileReader fr = new FileReader (file);
91 if (fr.read () == -1)
92 {
93 fw.write ("姓名\t\t语文\t\t数学\t\t<a href="https://www.baidu.com/s?wd=%E8%8B%B1%E8%AF%AD&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YvuhnkuWN-njmvnyPBPWnv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1DLPWT3PWR3rjRLPjfdrHRvP0" target="_blank" class="baidu-highlight">英语</a>\t\t总分" + line);
94 }
95 fr.close ();
96 for ( Student student : list )
97 {
98 fw.write (student.toString () + line);
99 fw.flush ();
100 }
101 fw.close ();
102 System.out.println ("加入完毕.");
103 }
104 catch (IOException e)
105 {}
106 }
107 }