看完视频觉得自己写得是一坨屎 - -。
- //package day21_IOStream;
- /*
- 有五个学生,每个学生有3门课的成绩,
- 从键盘输入以上数据(包括姓名,三门课成绩),
- 输入的格式:如:zhagnsan,30,40,60计算出总成绩,
- 并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stud.txt"中。
- 思路:
- 1.创建学生对象Student
- 2.建立键盘录入流 ,对录入数据进行缓存
- 3.创建文件写入流
- */
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Set;
- import java.util.TreeSet;
- public class StudentInfoTest {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- Set<Student> stus = StudentInfoTool.getStudents();
- StudentInfoTool.writeToFile(stus);
- }
- }
- /* 学生工具类 */
- class StudentInfoTool {
- public static Set<Student> getStudents() throws IOException {
- Set<Student> stuSet = new TreeSet<Student>();
- BufferedReader bufr = new BufferedReader(new InputStreamReader(
- System.in));
- String line = null;
- while ((line = bufr.readLine()) != null) {
- if (line.equals("over"))
- break;
- String[] strs = parseLine(line);
- /* 格式不正确 */
- if (strs == null) {
- System.out.println("请输入正确成绩:如zhagnsan,30,40,60");
- continue;
- }
- Student stu = new Student(strs[0], Integer.parseInt(strs[1]),
- Integer.parseInt(strs[2]), Integer.parseInt(strs[2]));
- stuSet.add(stu);
- }
- return stuSet;
- }
- public static void writeToFile(Set<Student> stus) throws IOException {
- File file = new File("stu.txt");
- BufferedWriter bufw = new BufferedWriter(new FileWriter(file, true));
- for (Student s : stus) {
- bufw.write(s + "\t");
- bufw.write(s.getSum() + "");
- bufw.newLine();
- bufw.flush();
- }
- bufw.close();
- }
- private static String[] parseLine(String line) {
- String[] strArray = line.split(",");
- if (strArray.length == 1) {
- strArray = line.split(",");
- if (strArray.length == 1)
- return null;
- }
- if (strArray.length == 4 && strArray[0] instanceof String) {
- for (int i = 1; i < 4; i++) {
- try {
- Integer.parseInt(strArray[i]);
- } catch (NumberFormatException e) {
- return null;
- }
- }
- return strArray;
- }
- return null;
- }
- }
- /* 创建学生对象 */
- class Student implements Comparable<Student> {
- private String name;
- /* 三门成绩及总分 */
- private int cr1, cr2, cr3, sum;
- Student(String name, int cr1, int cr2, int cr3) {
- this.name = name;
- this.cr1 = cr1;
- this.cr2 = cr2;
- this.cr3 = cr3;
- sum = cr1 + cr2 + cr3;
- }
- /* 排序 */
- public int compareTo(Student s) {
- int num = new Integer(s.sum).compareTo(sum);
- if (num == 0)
- return name.compareTo(s.name);
- return num;
- }
- /* 获取总成绩 */
- public int getSum() {
- return sum;
- }
- /* 获取姓名 */
- public String getName() {
- return this.name;
- }
- public boolean equals(Object obj) {
- if (!(obj instanceof Student))
- return false;
- Student stu = (Student) obj;
- return name.equals(stu.name) && cr1 == stu.cr1 && cr2 == stu.cr2
- && cr3 == stu.cr3;
- }
- public int hashCode() {
- return name.hashCode() + sum * 42;
- }
- public String toString() {
- return "student[" + name + ", " + cr1 + ", " + cr2 + ", " + cr3 + "]";
- }
- }
复制代码 |