案例:集合到文件(数据排序改进版)

1.需求和思路

 

package Demo2;


/**
 * @auther 黄磊
 * @2021/8/23 11:14
 **/
public class Student {
    private String name;
    private int Chinese;
    private int Math;
    private int English;

    public Student() {
    }

    public Student(String name, int chinese, int math, int english) {
        this.name = name;
        Chinese = chinese;
        Math = math;
        English = english;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChinese() {
        return Chinese;
    }

    public void setChinese(int chinese) {
        Chinese = chinese;
    }

    public int getMath() {
        return Math;
    }

    public void setMath(int math) {
        Math = math;
    }

    public int getEnglish() {
        return English;
    }

    public void setEnglish(int english) {
        English = english;
    }
	//注意此时的this关键字即:输入的内容
    public int getSum() {
        return this.Chinese + this.Math + this.English;
    }
}

package Demo2;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

/**
 * @auther 黄磊
 * @2021/8/23 11:17
 **/
public class TreeSetToFile {
    public static void main(String[] args) throws IOException {
        TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int i = s2.getSum() - s1.getSum();
                int i1 = i == 0 ? s2.getChinese() - s1.getChinese() : i;
                int i2 = i1 == 0 ? s2.getMath() - s1.getMath() : i1;
                int i3 = i2 == 0 ? s2.getName().compareTo(s1.getName()) : i2;
                return i3;
            }
        });
        Scanner in = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("你输入的是第" + (i + 1) + "个学生的信息");
            System.out.println("请输入该学生的姓名");
            String Name = in.next();
            System.out.println("请输入该学生的语文成绩");
            int Chinese = in.nextInt();
            System.out.println("请输入该学生的数学成绩");
            int Math = in.nextInt();
            System.out.println("请输入该学生的英语成绩");
            int English = in.nextInt();
            Student s = new Student();
            s.setName(Name);
            s.setChinese(Chinese);
            s.setMath(Math);
            s.setEnglish(English);
            treeSet.add(s);
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter("Previous\\12.txt"));
        ;//StringBuilder的位置非常重要,只有一个字符构造器,输出的文件如下
        //方式,99,99,99
        //方式,99,99,99维度,90,88,99
        //方式,99,99,99维度,90,88,99百度12,90,89,88
//        StringBuilder sb = new StringBuilder();
        for (Student s : treeSet) {
            StringBuilder sb = new StringBuilder();
            sb.append(s.getName()).append(",").append(s.getChinese()).append(",")
                    .append(s.getMath()).append(",").append(s.getEnglish());
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}

结果

创建文件,写入内容

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值