有人知道这个代码怎么改吗?

package Week008;

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 Test {
public static void main(String[] args) throws IOException {

TreeSet set= new TreeSet<>(new Comparator() {

public int compare (Student o1,Student o2){
int num=o1.getTotalScore()-o2.getTotalScore() ;
int num2=num==0?o1. getName ( ).compareTo(o2.getName()) : num;
return num2;
}
});
Scanner sc=new Scanner (System.in);
for (int i=1;i<=3;i++) {
Student student=new Student();
System.out.println("请输入第”+i+“个学生的姓名");
String name=sc.nextLine();
student.setHame(name);
System.out.println("请输入第"+i+"个学生的语文成绩");
int chineseScore=Integer.parseInt(sc.nextLine());
student.set (chineseScore);
System.out.println("请输入第"+i+"个学生的教学成绩");
int mathScore=Integer.parseInt(sc. nextLine());
student.setMathScore(mathScore);
System.out.println("请输入第"+i+"个学生的英语成绩");
int englishScore=Integer.parseInt(sc.nextLine());
student. setEnglishScore(englishScore);
//再把学对象存到集合中
set.add(student);
}
BufferedWriter out = new BufferedWriter(new FileWriter("studentScore. txt", true));
out.write("序号\t\t"+"姓名\t\t"+"语文成绩\t"+"数学成绩\t"+"英语成绩\t"+"总分");
out.newLine();
out. flush();
//遍历集合把数据存到文本文件中去
int index=1 ;
for (Student student : set){
out.write((index++)+"\t\t " +student.getName()+"\t\t"+student.getChineseScore()+"\t\t"+ student.getMathScore()+"\t\t"+student.getEnglishScore()+"\t\t" +student. getTotalScore());
out.newLine();out. flush();
out.close();
}

}
}

大概看了一下,foreach循环那里流关闭的位置不对,把它挪出来。还有些小问题,比如TreeSet泛型你没写,还有上面你都有了setChineseSoore(int chineseScore)方法,为什么还写个set(int chineseScore2) 方法,而这个方法里压根没存到语文成绩,改一下就对了

public class M {
    public static void main(String[] args) throws IOException {
        TreeSet<Student> set = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int num = o1.getTotalScore() - o2.getTotalScore();
                int num2 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;
                return num2;
            }

        });
        BufferedWriter out = new BufferedWriter(new FileWriter("studentScore. txt", true));
        Scanner sc = new Scanner(System.in);
        for (int i = 1; i <= 3; i++) {
            Student student = new Student();
            System.out.println("请输入第" + i + "个学生的姓名");
            String name = sc.nextLine();
            student.setHame(name);
            System.out.println("请输入第" + i + "个学生的语文成绩");
            int chineseScore = Integer.parseInt(sc.nextLine());
            student.setChineseSoore(chineseScore);
            System.out.println("请输入第" + i + "个学生的教学成绩");
            int mathScore = Integer.parseInt(sc.nextLine());
            student.setMathScore(mathScore);
            System.out.println("请输入第" + i + "个学生的英语成绩");
            int englishScore = Integer.parseInt(sc.nextLine());
            student.setEnglishScore(englishScore);
            // 再把学对象存到集合中
            set.add(student);
        }
        out.write("序号\t\t" + "姓名\t\t" + "语文成绩\t" + "数学成绩\t" + "英语成绩\t" + "总分");
        out.newLine();
        out.flush();
        // 遍历集合把数据存到文本文件中去
        int index = 1;
        for (Student student : set) {
            out.write((index++) + "\t\t " + student.getName() + "\t\t" + student.getChineseScore() + "\t\t"
                    + student.getMathScore() + "\t\t" + student.getEnglishScore() + "\t\t" + student.getTotalScore());
            out.newLine();
            out.flush();
        }
        out.close();

    }

}

class Student {
    private String name;
    private int chineseScore;
    private int mathScore;
    private int englishScore;
    public int[] set;

    public String getName() {
        return name;
    }

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

    public int getChineseScore() {
        return chineseScore;
    }

    public void setChineseSoore(int chineseScore) {
        this.chineseScore = chineseScore;
    }

    public int getMathScore() {
        return mathScore;
    }

    public void setMathScore(int mathScore) {
        this.mathScore = mathScore;
    }

    public int getEnglishScore() {
        return englishScore;
    }

    public void setEnglishScore(int englishScore) {
        this.englishScore = englishScore;
    }

    public int getTotalScore() {
        return chineseScore + mathScore + englishScore;
    }

    @Override
    public String toString() {
        return "Student{" + "name=" + name + ", chineseScore=" + chineseScore + ", mathScore=" + mathScore
                + ",englishScore=" + englishScore + '}';
    }



img

你的student类呢