将学生成绩从集合存储至文件

将学生成绩从集合存储至文件,第1张

学生成绩从集合存储至文件
  public static void main(String[] args) throws IOException {
        TreeSet ts = new TreeSet(new Comparator() {
            @Override
            public int compare(Outer o1, Outer o2) {
                int num = o1.getSum() - o2.getSum();
                return num;
            }
        });
        BufferedWriter bw = new BufferedWriter(new FileWriter("fos"));

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要输入学生人数");
        int num = sc.nextInt();
        sc.nextLine();

        for (int i = 0; i < num; i++) {
            System.out.println("学生姓名");
            String name = sc.nextLine();

            System.out.println("数学成绩");
            int math = sc.nextInt();
            sc.nextLine();

            System.out.println("语文成绩");
            int chinese = sc.nextInt();
            sc.nextLine();

            Outer o = new Outer(name, math, chinese);
            ts.add(o);
        }

        StringBuilder sb = new StringBuilder();
        for (Outer o : ts) {
            sb.append(o.getName()).append(",").append(o.getMath()).append(",").append(o.getChinese());
            bw.write(sb.toString());
            bw.newline();
            bw.flush();
        }
        System.out.println(sb.toString());
        bw.close();
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5661278.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存