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();
    
    
        }
    }
    
  • 相关阅读:
    Ubuntu apt-get update 失败
    Ubuntu无法访问windows分区
    Python实现使用tkinter弹出输入框输入数字, 具有确定输入和清除功能
    如何更改监控器的默认计数器
    健壮的 Java 基准测试
    从虚拟机视角谈 Java 应用性能优化
    LoadRunner如何调用外部函数
    git安装与上传
    Loadrunner安装与破解【转】
    性能测试方法【转】
  • 原文地址:https://www.cnblogs.com/lsswudi/p/11428731.html
Copyright © 2011-2022 走看看