以下是根据描述完成操作的Java代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Student> studentList = readFromFile("students.txt");
        int totalScore = calculateTotalScore(studentList);
        System.out.println("学生总分数为:" + totalScore);
    }

    public static List<Student> readFromFile(String fileName) {
        List<Student> studentList = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] data = line.split("-");
                String name = data[0];
                int age = Integer.parseInt(data[1]);
                int score = Integer.parseInt(data[2]);
                Student student = new Student(name, age, score);
                studentList.add(student);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return studentList;
    }

    public static int calculateTotalScore(List<Student> studentList) {
        int totalScore = 0;
        for (Student student : studentList) {
            totalScore += student.getScore();
        }
        return totalScore;
    }
}

class Student {
    private String name;
    private int age;
    private int score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getScore() {
        return score;
    }
}

请注意,上述代码中的students.txt是包含学生信息的文本文件,每行一个学生的信息,格式为"姓名-年龄-分数"。在运行代码之前,请确保将实际的文件路径传递给readFromFile方法中的fileName参数

java 请根据以下描述完成操作。小明-18-88;小红-17-90;1 读取文件中的内容2 将文件中的内容转换成Student对象姓名-年龄-分数3 求出学生总分数

原文地址: https://www.cveoy.top/t/topic/ipG3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录