zoukankan      html  css  js  c++  java
  • 集合到文件(数据排序改进版)

    package com.io.liushuaishuai;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Comparator;
    import java.util.Scanner;
    import java.util.TreeSet;
    
    public class ArrayListToTxDemo01 {
        public static void main(String[] args) throws IOException {
            TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
                @Override
                public int compare(Student s1, Student s2) {
                    int num = s2.sum() - s1.sum();
                    int num2 = num == 0 ? s2.getChinese() - s1.getChinese() : num;
                    int num3 = num2 == 0 ? s2.getMath() - s1.getMath() : num2;
                    int num4 = num3 == 0 ? s2.getName().compareTo(s1.getName()) : num3;
                    return num4;
    
                }
            });
    
            int i;
            for (i = 0; i < 5; i++) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入第" + (i + 1) + "个学生的信息");
                System.out.println("请输入姓名");
                String name = sc.nextLine();
                System.out.println("请输入语文成绩");
                int chinese = sc.nextInt();
                System.out.println("请输入数学成绩");
                int math = sc.nextInt();
                System.out.println("请输入英语成绩");
                int english = sc.nextInt();
                Student s = new Student(name, chinese, math, english);
                ts.add(s);
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter("myIOstream\fos.txt"));
            for (Student s : ts) {
                StringBuilder sb = new StringBuilder();
                StringBuilder str = sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish()).append(",").append(s.sum());
                String s1 = str.toString();
                bw.write(s1);
                bw.newLine();
                bw.flush();
            }
    
            bw.close();
    
    
        }
    }
    
  • 相关阅读:
    Spring中的AOP
    P2782 友好城市
    1576 最长严格上升子序列
    1058 合唱队形 2004年NOIP全国联赛提高组
    5294 挖地雷
    1643 线段覆盖 3
    4768 跳石头
    1026 逃跑的拉尔夫
    2727:仙岛求药
    codevs 4888 零件分组
  • 原文地址:https://www.cnblogs.com/lsswudi/p/11428731.html
Copyright © 2011-2022 走看看