• Java基础知识强化之IO流笔记51:IO流练习之 键盘录入学生信息按照总分排序写入文本文件中的案例


    1.  键盘录入学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分排序写入文本文件中

    分析:
        A:创建学生类
        B:创建集合对象
            TreeSet<Student>
            C:键盘录入学生信息存储到集合
           D:遍历集合,把数据写到文本文件
     
    2. 代码示例:
    (1)Student.java:
     1 package cn.itcast_06;
     2 
     3 public class Student {
     4     // 姓名
     5     private String name;
     6     // 语文成绩
     7     private int chinese;
     8     // 数学成绩
     9     private int math;
    10     // 英语成绩
    11     private int english;
    12 
    13     public Student() {
    14         super();
    15     }
    16 
    17     public Student(String name, int chinese, int math, int english) {
    18         super();
    19         this.name = name;
    20         this.chinese = chinese;
    21         this.math = math;
    22         this.english = english;
    23     }
    24 
    25     public String getName() {
    26         return name;
    27     }
    28 
    29     public void setName(String name) {
    30         this.name = name;
    31     }
    32 
    33     public int getChinese() {
    34         return chinese;
    35     }
    36 
    37     public void setChinese(int chinese) {
    38         this.chinese = chinese;
    39     }
    40 
    41     public int getMath() {
    42         return math;
    43     }
    44 
    45     public void setMath(int math) {
    46         this.math = math;
    47     }
    48 
    49     public int getEnglish() {
    50         return english;
    51     }
    52 
    53     public void setEnglish(int english) {
    54         this.english = english;
    55     }
    56 
    57     public int getSum() { // 获得总分
    58         return this.chinese + this.math + this.english;
    59     }
    60 }

    (2)StudentDemo.java:

     1 package cn.itcast_06;
     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 /*
    11  * 键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
    12  * 
    13  * 分析:
    14  *         A:创建学生类
    15  *         B:创建集合对象
    16  *             TreeSet<Student>
    17  *         C:键盘录入学生信息存储到集合
    18  *         D:遍历集合,把数据写到文本文件
    19  */
    20 public class StudentDemo {
    21     public static void main(String[] args) throws IOException {
    22         // 创建集合对象
    23         TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
    24             @Override
    25             public int compare(Student s1, Student s2) {
    26                 int num = s2.getSum() - s1.getSum(); //总分排序是由高到低
    27                 int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
    28                 int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;
    29                 int num4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3;
    30                 int num5 = num4 == 0 ? s1.getName().compareTo(s2.getName())
    31                         : num4;
    32                 return num5;
    33             }
    34         });
    35 
    36         // 键盘录入学生信息存储到集合
    37         for (int x = 1; x <= 5; x++) {
    38             Scanner sc = new Scanner(System.in);
    39             System.out.println("请录入第" + x + "个的学习信息");
    40             System.out.println("姓名:");
    41             String name = sc.nextLine();
    42             System.out.println("语文成绩:");
    43             int chinese = sc.nextInt();
    44             System.out.println("数学成绩:");
    45             int math = sc.nextInt();
    46             System.out.println("英语成绩:");
    47             int english = sc.nextInt();
    48 
    49             // 创建学生对象
    50             Student s = new Student();
    51             s.setName(name);
    52             s.setChinese(chinese);
    53             s.setMath(math);
    54             s.setEnglish(english);
    55 
    56             // 把学生信息添加到集合
    57             ts.add(s);
    58         }
    59 
    60         // 遍历集合,把数据写到文本文件
    61         BufferedWriter bw = new BufferedWriter(new FileWriter("students.txt"));
    62         bw.write("学生信息如下:");
    63         bw.newLine();
    64         bw.flush();
    65         bw.write("姓名,语文成绩,数学成绩,英语成绩");
    66         bw.newLine();
    67         bw.flush();
    68         for (Student s : ts) {
    69             StringBuilder sb = new StringBuilder();
    70             sb.append(s.getName()).append(",").append(s.getChinese())
    71                     .append(",").append(s.getMath()).append(",")
    72                     .append(s.getEnglish());
    73             bw.write(sb.toString());
    74             bw.newLine();
    75             bw.flush();
    76         }
    77         // 释放资源
    78         bw.close();
    79         System.out.println("学习信息存储完毕");
    80     }
    81 }

    运行效果,如下:

  • 相关阅读:
    安卓学习39
    安卓学习38
    Python+Selenium学习--打印当前页面的title及url
    Python+Selenium学习--访问连接
    Python+Selenium学习--浏览器设置
    Python+Selenium学习--启动及关闭浏览器
    Go语言学习笔记(十八)之文件读写
    Go语言学习笔记(十七)之命令行参数
    Go语言学习笔记(十六)之格式化输入输出
    Go语言学习笔记(十五)之异常处理
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4870294.html
Copyright © 2020-2023  润新知