IO流的练习4 —— 键盘录入学生成绩信息,进行排序后存入文本中

需求:

  键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件

分析:
  A:创建学生类
  B:创建集合对象
      TreeSet<Student>
  C:键盘录入学生信息存储到集合
  D:遍历集合,把数据写到文本文件

首先创建个学生类

 1 package zl_Test;
 2 /**
 3  * 这是个记录学生成绩类
 4  * @author LZL
 5  *
 6  */
 7 public class Student {
 8     private String name;
 9     private int chinese;
10     private int math;
11     private int english;
12     
13     
14     public Student() {
15         super();
16         // TODO Auto-generated constructor stub
17     }
18 
19 
20     public Student(String name, int chinese, int math, int english) {
21         super();
22         this.name = name;
23         this.chinese = chinese;
24         this.math = math;
25         this.english = english;
26     }
27 
28 
29     public String getName() {
30         return name;
31     }
32 
33 
34     public void setName(String name) {
35         this.name = name;
36     }
37 
38 
39     public int getChinese() {
40         return chinese;
41     }
42 
43 
44     public void setChinese(int chinese) {
45         this.chinese = chinese;
46     }
47 
48 
49     public int getMath() {
50         return math;
51     }
52 
53 
54     public void setMath(int math) {
55         this.math = math;
56     }
57 
58 
59     public int getEnglish() {
60         return english;
61     }
62 
63 
64     public void setEnglish(int english) {
65         this.english = english;
66     }
67     
68     public int getsum() {
69          return this.chinese + this.english + this.math;
70         
71 
72     }
73     
74 }
View Code

具体实现类:

 1 package zl_Test;
 2 
 3 import java.io.BufferedWriter;
 4 import java.io.FileWriter;
 5 import java.io.IOException;
 6 import java.util.Comparator;
 7 import java.util.Scanner;
 8 import java.util.TreeSet;
 9 
10 public class StudentDemo {
11 
12     public static void main(String[] args) throws IOException {
13         // 创建集合对象
14         TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
15             // 比较器
16             public int compare(Student s1, Student s2) {
17 
18                 int num1 = s2.getsum() - s1.getsum();
19                 int num2 = num1 == 0 ? s2.getChinese() - s1.getChinese() : num1;
20                 int num3 = num2 == 0 ? s2.getMath() - s1.getMath() : num2;
21                 int num4 = num3 == 0 ? s2.getName().compareTo(s1.getName())
22                         : num3;
23                 return num4;
24 
25             }
26         });
27 
28         // 键盘录入学生信息
29         for (int x = 0; x < 5; x++) {
30             Scanner sc = new Scanner(System.in);
31 
32             System.out.println("请输入学生姓名:");
33             String name = sc.nextLine();
34             System.out.println("请输入语文成绩");
35             int chinese = sc.nextInt();
36             System.out.println("请输入数学成绩");
37             int math = sc.nextInt();
38             System.out.println("请输入英语成绩");
39             int english = sc.nextInt();
40 
41             // 创建学生对象,调用学生类
42             Student s = new Student();
43             s.setName(name);
44             s.setChinese(chinese);
45             s.setMath(math);
46             s.setEnglish(english);
47 
48             // 把信息添加到集合中
49             ts.add(s);
50         }
51 
52         // 文本打开能看得懂的信息,用缓冲字符流
53         // 创建缓冲字符输出流对象
54         BufferedWriter bw = new BufferedWriter(new FileWriter("student.txt"));
55         // 先写文本里面的内容
56         bw.write("学生成绩记录");
57         bw.newLine();
58         bw.flush();
59         bw.write("语文成绩" + "	" + "数学成绩" + "	" + "英语成绩");
60         bw.newLine();
61         bw.flush();
62 
63         // 遍历集合,把得到的数据添加到文件中
64         for (Student student : ts) {
65             // 对得到的信息进行拼接
66             StringBuilder sb = new StringBuilder();
67             sb.append(student.getName() + "		")
68                     .append(student.getChinese() + "		")
69                     .append(student.getMath() + "		")
70                     .append(student.getEnglish());
71             bw.write(sb.toString());
72             bw.newLine();
73         }
74         bw.close();
75         System.out.println("学生成绩存储完毕");
76     }
77 }
何事都只需坚持.. 难? 维熟尔。 LZL的自学历程...只需坚持
原文地址:https://www.cnblogs.com/LZL-student/p/5927791.html