[Java IO流]键盘录入5个学生的信息(姓名,年龄,语文成绩,数学成绩,英语成绩),按照总分排序, 总分相同分别按照语文,数学,英语,年龄,姓名排序,并且将排序的结果写入到文件当中保存起来

该博客介绍了如何使用Java IO流从键盘录入5个学生(姓名、年龄、语文、数学、英语成绩),按总分排序。当总分相同时,依据语文、数学、英语成绩、年龄及姓名进行排序。最后,将排序后的信息写入文件保存。
摘要由CSDN通过智能技术生成

键盘录入5个学生的信息(姓名,年龄,语文成绩,数学成绩,英语成绩),
按照总分排序,总分相同分别按照语文,数学,英语,年龄,姓名排序,
并且将排序的结果写入到文件当中保存起来

1.现在main方法外定义一个学生类

class Student{
   
    private String name;
    private int age;
    private int chinese;
    private int math;
    private int english;

    public Student() {
   
    }

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

    //获得总成绩的方法
    public int getSum() {
   
    //返回总成绩
        return chinese + math + english;
    }

    public String getName() {
   
        return name;
    }

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

    public int getAge() {
   
        return age;
    }

    public void setAge(int age) {
   
        this.age = age;
    }

    public int getChinese() {
   
        return chinese;
    }

    public void setChinese(int chinese) {
   
        this
可以按照以下步骤实现: 1. 创建一个Student类,包含姓名和三门课成绩的属性,以及按照成绩排序的方法。 ``` public class Student { private String name; private int chineseScore; private int mathScore; private int englishScore; public Student(String name, int chineseScore, int mathScore, int englishScore) { this.name = name; this.chineseScore = chineseScore; this.mathScore = mathScore; this.englishScore = englishScore; } public String getName() { return name; } public int getChineseScore() { return chineseScore; } public int getMathScore() { return mathScore; } public int getEnglishScore() { return englishScore; } public int getTotalScore() { return chineseScore + mathScore + englishScore; } public void sortScore() { int[] scores = {chineseScore, mathScore, englishScore}; Arrays.sort(scores); chineseScore = scores[2]; mathScore = scores[1]; englishScore = scores[0]; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", chineseScore=" + chineseScore + ", mathScore=" + mathScore + ", englishScore=" + englishScore + '}'; } } ``` 2. 在主方法中,通过Scanner类从键盘输入三个学生信息,并创建Student对象。 ``` public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); List<Student> students = new ArrayList<>(); for (int i = 1; i <= 3; i++) { System.out.println("请输入第" + i + "个学生姓名语文成绩数学成绩英语成绩:"); String name = scanner.next(); int chineseScore = scanner.nextInt(); int mathScore = scanner.nextInt(); int englishScore = scanner.nextInt(); Student student = new Student(name, chineseScore, mathScore, englishScore); student.sortScore(); // 按成绩排序 students.add(student); } } ``` 3. 将学生信息按照成绩从高到低写入文本文件。 ``` File file = new File("students.txt"); if (!file.exists()) { file.createNewFile(); } PrintWriter writer = new PrintWriter(new FileWriter(file)); for (Student student : students) { writer.println(student.getName() + " " + student.getChineseScore() + " " + student.getMathScore() + " " + student.getEnglishScore()); } writer.close(); ``` 4. 从文件中读取数据,并显示在控制台上。 ``` Scanner reader = new Scanner(file); while (reader.hasNextLine()) { String line = reader.nextLine(); String[] parts = line.split(" "); String name = parts[0]; int chineseScore = Integer.parseInt(parts[1]); int mathScore = Integer.parseInt(parts[2]); int englishScore = Integer.parseInt(parts[3]); Student student = new Student(name, chineseScore, mathScore, englishScore); System.out.println(student); } reader.close(); ``` 完整代码如下: ``` import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); List<Student> students = new ArrayList<>(); for (int i = 1; i <= 3; i++) { System.out.println("请输入第" + i + "个学生姓名语文成绩数学成绩英语成绩:"); String name = scanner.next(); int chineseScore = scanner.nextInt(); int mathScore = scanner.nextInt(); int englishScore = scanner.nextInt(); Student student = new Student(name, chineseScore, mathScore, englishScore); student.sortScore(); // 按成绩排序 students.add(student); } File file = new File("students.txt"); if (!file.exists()) { file.createNewFile(); } PrintWriter writer = new PrintWriter(new FileWriter(file)); for (Student student : students) { writer.println(student.getName() + " " + student.getChineseScore() + " " + student.getMathScore() + " " + student.getEnglishScore()); } writer.close(); Scanner reader = new Scanner(file); while (reader.hasNextLine()) { String line = reader.nextLine(); String[] parts = line.split(" "); String name = parts[0]; int chineseScore = Integer.parseInt(parts[1]); int mathScore = Integer.parseInt(parts[2]); int englishScore = Integer.parseInt(parts[3]); Student student = new Student(name, chineseScore, mathScore, englishScore); System.out.println(student); } reader.close(); } } class Student { private String name; private int chineseScore; private int mathScore; private int englishScore; public Student(String name, int chineseScore, int mathScore, int englishScore) { this.name = name; this.chineseScore = chineseScore; this.mathScore = mathScore; this.englishScore = englishScore; } public String getName() { return name; } public int getChineseScore() { return chineseScore; } public int getMathScore() { return mathScore; } public int getEnglishScore() { return englishScore; } public int getTotalScore() { return chineseScore + mathScore + englishScore; } public void sortScore() { int[] scores = {chineseScore, mathScore, englishScore}; Arrays.sort(scores); chineseScore = scores[2]; mathScore = scores[1]; englishScore = scores[0]; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", chineseScore=" + chineseScore + ", mathScore=" + mathScore + ", englishScore=" + englishScore + '}'; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值