欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

计算语文和数学成绩的总和java实现

程序员文章站 2022-05-29 21:09:25
...

计算语文和数学成绩的总和java实现
Student

public class Student {
    private  String name;
    private  int yuwen;
    private int math;
    private int sum;

    public Student() {
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", yuwen=" + yuwen +
                ", math=" + math +
                ", sum=" + sum +
                '}';
    }

    public Student(String name, int yuwen, int math) {
        this.name = name;
        this.yuwen = yuwen;
        this.math = math;
        this.sum=yuwen+math;
    }

    public String getName() {
        return name;
    }

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

    public int getYuwen() {
        return yuwen;
    }

    public void setYuwen(int yuwen) {
        this.yuwen = yuwen;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getSum() {
        return sum;
    }

    public void setSum(int sum) {
        this.sum = sum;
    }
}
public class Sorce {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("输入路径"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("输出路径"));
        List<Student> list = new ArrayList<Student>();
        String line="";
        while ((line=br.readLine())!=null){
            String[] split = line.split(",");
            if (split.length>=3){
                Student student = new Student(split[0],Integer.valueOf(split[1]),Integer.valueOf(split[2]));
                list.add(student);
            }
        }
        Collections.sort(list, new Comparator<Student>() {
            public int compare(Student o1, Student o2) {
                return o1.getSum()>o2.getSum()?1:-1;
            }
        });
        for (Student s:list ) {
            bw.write(s.toString());
            bw.newLine();
        }
        bw.close();
        br.close();
    }
}