jahshsgsgd 2022-05-10 17:11 采纳率: 50%
浏览 39
已结题

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

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();
}

}
}

  • 写回答

2条回答 默认 最新

  • 家有可爱多呢 2022-05-10 18:50
    关注

    大概看了一下,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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月20日
  • 已采纳回答 5月12日
  • 创建了问题 5月10日

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集