键盘录入3个学生信息(姓名,语文成绩,数学成绩,英语成绩)。 要求按照成绩总分从高到低写入文本文件,最后在从文件中把读取数据显示在控制台 * * 格式:姓名,语文成绩,数学成绩,英语成

方法一:通过ArrayList集合,将学生对象放进集合,然后通过Collections的sort集合进行自定义排序,最后遍历集合通过字符流将信息写入文件中去,同时通过字符输入流将文件的信息读取打印到控制台。

package Test2;



import java.io.*;
import java.util.*;


public class Demo2 {
    public static void main(String[] args) {
        ArrayList<Student> treeSet = new ArrayList<>();
        //录入成绩
        Scanner sc = new Scanner(System.in);
        for (int i = 1; i <= 3; i++) {
            if (i>=2) {
                System.out.println("请输入第"+i+"位学生的姓名");
            }

            System.out.println("请输入学生姓名:");
            String s = sc.next();
            System.out.println("请输入语文成绩");
            int a = sc.nextInt();
            System.out.println("请输入数学成绩");
            int b = sc.nextInt();
            System.out.println("请输入英语成绩");
            int c = sc.nextInt();
            //创建学生对象,并将输入的信息初始化入学生对象,存入集合
            Student student = new Student();
            student.setName(s);
            student.setChinese(a);
            student.setMath(b);
            student.setEnglish(c);
            treeSet.add(student);
        }
        Collections.sort(treeSet, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int sum1 = o1.getChinese() + o1.getMath() + o1.getEnglish();
                int sum2 = o2.getChinese() + o2.getMath() + o2.getEnglish();
                return Integer.compare(sum2,sum1);
            }
        });
        try (
                FileWriter fw = new FileWriter("random_pencil/src/Test2/abc.txt");
                BufferedWriter bw = new BufferedWriter(fw);
        ) {

            for (Student student : treeSet) {
                bw.write(student.getName()+",");
                bw.write(student.getChinese()+",");
                bw.write(student.getMath()+",");
                bw.write(student.getEnglish()+"");
                bw.newLine();
//                bw.write(student.toString());
//                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        try (
                FileReader fr = new FileReader("random_pencil/src/Test2/abc.txt");
                BufferedReader br=new BufferedReader(fr)
                ){
            String str;
            while ((str = br.readLine()) != null) {
                System.out.println(str);
            }

        }  catch (IOException e) {
            e.printStackTrace();
        }

    }
}
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;
        this.chinese = chinese;
        this.math = math;
        this.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) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

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

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    @Override
    public String toString() {
        return name+","+chinese+","+math+","+english;
    }
}

方法二:通过Treeset集合,先自定义好排序规则,将学生对象放进集合,遍历集合通过字符流将信息写入文件中去,同时通过字符输入流将文件的信息读取打印到控制台。

package Test2;


import java.io.*;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class Demo2_1 {
    public static void main(String[] args) {
        TreeSet<Student1> treeSet = new TreeSet<>(new Comparator<Student1>() {
            @Override
            public int compare(Student1 o1, Student1 o2) {
                int sum1=o1.getChinese()+o1.getMath()+o1.getEnglish();
                int sum2=o2.getChinese()+o2.getMath()+o2.getEnglish();
                return Integer.compare(sum2,sum1);
            }
        });
        Scanner sc = new Scanner(System.in);
        for (int i = 1; i <= 3; i++) {
            if (i>=2) {
                System.out.println("请输入第"+i+"位学生的姓名");
            }

            System.out.println("请输入学生姓名:");
            String s = sc.next();
            System.out.println("请输入语文成绩");
            int a = sc.nextInt();
            System.out.println("请输入数学成绩");
            int b = sc.nextInt();
            System.out.println("请输入英语成绩");
            int c = sc.nextInt();
            //创建学生对象,并将输入的信息初始化入学生对象,存入集合
            Student1 student = new Student1();
            student.setName(s);
            student.setChinese(a);
            student.setMath(b);
            student.setEnglish(c);
            treeSet.add(student);
        }
        treeSet.forEach(System.out::println);
        try(FileWriter fw = new FileWriter("random_pencil/src/Test2/123.txt");
        BufferedWriter bw = new BufferedWriter(fw); ){
            for (Student1 student1 : treeSet) {
                bw.write(student1.getName()+",");
                bw.write(student1.getChinese()+",");
                bw.write(student1.getMath()+",");
                bw.write(student1.getEnglish()+"");
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try (
                FileReader fr = new FileReader("random_pencil/src/Test2/123.txt");
                BufferedReader br = new BufferedReader(fr)
        ) {
            String str;
            while ((str = br.readLine()) != null) {
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

class Student1 {
    private String name;
    private int chinese;
    private int math;
    private int english;

    public Student1() {
    }

    public Student1(String name, int chinese, int math, int english) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.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) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

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

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "name='" + name + '\'' +
                ", chinese=" + chinese +
                ", math=" + math +
                ", english=" + english +
                '}';
    }
}

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

king123t

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值