java难题,希望各位能解决

img


请各位朋友解决下这个问题,这个学生成绩管理系统由成绩的录入,成绩的查询,成绩的统计组成,成绩为语文成绩,数学成绩,英语成绩

这不算难题,就是你们一个课程的练习题。

img

import java.util.*;

class Student {
    private int id;
    private String name;
    private int chineseScore;
    private int mathScore;
    private int englishScore;

    public Student(int id, String name, int chineseScore, int mathScore, int englishScore) {
        this.id = id;
        this.name = name;
        this.chineseScore = chineseScore;
        this.mathScore = mathScore;
        this.englishScore = englishScore;
    }

    public int getId() {
        return id;
    }

    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 double getAverageScore() {
        return getTotalScore() / 3.0;
    }
}

class StudentManager {
    private List<Student> students;
    private Map<Integer, Student> idToStudentMap;
    private Map<String, List<Student>> nameToStudentListMap;

    public StudentManager() {
        students = new ArrayList<>();
        idToStudentMap = new HashMap<>();
        nameToStudentListMap = new HashMap<>();
    }

    public void addStudent() {
        Scanner scanner = new Scanner(System.in);
        int id = 0;
        String name = null;
        int chineseScore = 0;
        int mathScore = 0;
        int englishScore = 0;
        boolean validInput = false;
        while (!validInput) {
            System.out.print("请输入学生学号(1-90):");
            id = scanner.nextInt();
            scanner.nextLine(); // consume the newline character
            if (id < 1 || id > 90) {
                System.out.println("学号不合法,请重新输入!");
            } else if (idToStudentMap.containsKey(id)) {
                System.out.println("学号已存在,请重新输入!");
            } else {
                validInput = true;
            }
        }
        validInput = false;
        while (!validInput) {
            System.out.print("请输入学生姓名(只包含英文字母和空格):");
            name = scanner.nextLine();
            if (!name.matches("[a-zA-Z ]+")) {
                System.out.println("姓名不合法,请重新输入!");
            } else {
                validInput = true;
            }
        }
        validInput = false;
        while (!validInput) {
            System.out.print("请输入语文成绩(0-100):");
            chineseScore = scanner.nextInt();
            if (chineseScore < 0 || chineseScore > 100) {
                System.out.println("成绩不合法,请重新输入!");
            } else {
                validInput = true;
            }
        }
        validInput = false;
        while (!validInput) {
            System.out.print("请输入数学成绩(0-100):");
            mathScore = scanner.nextInt();
            if (mathScore < 0 || mathScore > 100) {
                System.out.println("成绩不合法,请重新输入!");
            } else {
                validInput = true;
            }
        }
        validInput = false;
        while (!validInput) {
            System.out.print("请输入英语成绩(0-100):");
            englishScore = scanner.nextInt();
            if (englishScore < 0 || englishScore > 100) {
                System.out.println("成绩不合法,请重新输入!");
            } else {
                validInput = true;
            }
        }
        Student student = new Student(id, name, chineseScore, mathScore, englishScore);
        students.add(student);
        idToStudentMap.put(id, student);
        List<Student> studentList = nameToStudentListMap.get(name.toLowerCase());
        if (studentList == null) {
            studentList = new ArrayList<>();
            nameToStudentListMap.put(name.toLowerCase(), studentList);
        }
        studentList.add(student);
        System.out.println("学生信息录入成功!");
    }

    public void searchById() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要查询的学生学号:");
        int id = scanner.nextInt();
        Student student = idToStudentMap.get(id);
        if (student == null) {
            System.out.println("找不到该学生!");
        } else {
            System.out.println("学号\t姓名\t语文\t数学\t英语\t总分\t平均分");
            System.out.println(student.getId() + "\t" + student.getName() + "\t" +
                student.getChineseScore() + "\t" + student.getMathScore() + "\t" +
                student.getEnglishScore() + "\t" + student.getTotalScore() + "\t" +
                String.format("%.2f", student.getAverageScore()));
        }
    }

    public void searchByName() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要查询的学生姓名:");
        String name = scanner.nextLine().toLowerCase();
        List<Student> studentList = nameToStudentListMap.get(name);
        if (studentList == null) {
            System.out.println("找不到该学生!");
        } else {
            System.out.println("学号\t姓名\t语文\t数学\t英语\t总分\t平均分");
            for (Student student : studentList) {
                System.out.println(student.getId() + "\t" + student.getName() + "\t" +
                    student.getChineseScore() + "\t" + student.getMathScore() + "\t" +
                    student.getEnglishScore() + "\t" + student.getTotalScore() + "\t" +
                    String.format("%.2f", student.getAverageScore()));
            }
        }
    }

    public void showStatistics() {
        int chineseFailCount = 0;
        int mathFailCount = 0;
        int englishFailCount = 0;
        int chineseTotalScore = 0;
        int mathTotalScore = 0;
        int englishTotalScore = 0;
        int chineseMaxScore = 0;
        int mathMaxScore = 0;
        int englishMaxScore = 0;
        int chineseMinScore = 100;
        int mathMinScore = 100;
        int englishMinScore = 100;
        List<Student> studentsCopy = new ArrayList<>(students);
        Collections.sort(studentsCopy, new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                return s2.getTotalScore() - s1.getTotalScore();
            }
        });
        System.out.println("各科成绩不及格人数和比例:");
        for (Student student : students) {
            if (student.getChineseScore() < 60) {
                chineseFailCount++;
            }
            if (student.getMathScore() < 60) {
                mathFailCount++;
            }
            if (student.getEnglishScore() < 60) {
                englishFailCount++;
            }
        }
        System.out.println("语文:不及格人数=" + chineseFailCount + ",比例=" +
            String.format("%.2f", chineseFailCount / (double) students.size()));
        System.out.println("数学:不及格人数=" + mathFailCount + ",比例=" +
            String.format("%.2f", mathFailCount / (double) students.size()));
        System.out.println("英语:不及格人数=" + englishFailCount + ",比例=" +
            String.format("%.2f", englishFailCount / (double) students.size()));
        System.out.println("各科平均分:");
        for (Student student : students) {
            chineseTotalScore += student.getChineseScore();
            mathTotalScore += student.getMathScore();
            englishTotalScore += student.getEnglishScore();
            if (student.getChineseScore() > chineseMaxScore) {
                chineseMaxScore = student.getChineseScore();
            }
            if (student.getMathScore() > mathMaxScore) {
                mathMaxScore = student.getMathScore();
            }
            if (student.getEnglishScore() > englishMaxScore) {
                englishMaxScore = student.getEnglishScore();
            }
            if (student.getChineseScore() < chineseMinScore) {
                chineseMinScore = student.getChineseScore();
            }
            if (student.getMathScore() < mathMinScore) {
                mathMinScore = student.getMathScore();
            }
            if (student.getEnglishScore() < englishMinScore) {
                englishMinScore = student.getEnglishScore();
            }
        }
        System.out.println("语文平均分=" + chineseTotalScore / (double) students.size());
        System.out.println("数学平均分=" + mathTotalScore / (double) students.size());
        System.out.println("英语平均分=" + englishTotalScore / (double) students.size());
        System.out.println("各科最高分和最低分:");
        System.out.println("语文最高分=" + chineseMaxScore + ",最低分=" + chineseMinScore);
        System.out.println("数学最高分=" + mathMaxScore + ",最低分=" + mathMinScore);
        System.out.println("英语最高分=" + englishMaxScore + ",最低分=" + englishMinScore);
        System.out.println("各科成绩排名:");
        System.out.println("排名\t学号\t姓名\t语文\t数学\t英语\t总分\t平均分");
        for (int i = 0; i < studentsCopy.size(); i++) {
            Student student = studentsCopy.get(i);
            System.out.println((i + 1) + "\t" + student.getId() + "\t" + student.getName() + "\t" +
                student.getChineseScore() + "\t" + student.getMathScore() + "\t" +
                student.getEnglishScore() + "\t" + student.getTotalScore() + "\t" +
                String.format("%.2f", student.getAverageScore()));
        }
    }
}

public class Main {
    public static void main(String[] args) {
        StudentManager studentManager = new StudentManager();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请选择要进行的操作:");
            System.out.println("1. 录入学生成绩");
            System.out.println("2. 按学号查询学生成绩");
            System.out.println("3. 按姓名查询学生成绩");
            System.out.println("4. 显示成绩统计信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号:");
            int choice = scanner.nextInt();
            scanner.nextLine(); // consume the newline character
            switch (choice) {
                case 1:
                    studentManager.addStudent();
                    break;
                case 2:
                    studentManager.searchById();
                    break;
                case 3:
                    studentManager.searchByName();
                    break;
                case 4:
                    studentManager.showStatistics();
                    break;
                case 5:
                    System.out.println("程序已退出!");
                    System.exit(0);
                default:
                    System.out.println("无效的操作编号,请重新输入!");
            }
            System.out.println();
        }
    }
}


1 建表 包含字段在要求1中
2 创建项目 实体 controller service impl dao
3 前端支持layui有现成模板 套一份
4 对接调试 完成

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

class Student {
    private int id;
    private String name;
    private int chineseScore;
    private int mathScore;
    private int englishScore;

    public Student(int id, String name, int chineseScore, int mathScore, int englishScore) {
        this.id = id;
        this.name = name;
        this.chineseScore = chineseScore;
        this.mathScore = mathScore;
        this.englishScore = englishScore;
    }

    // Getters and setters

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getChineseScore() {
        return chineseScore;
    }

    public int getMathScore() {
        return mathScore;
    }

    public int getEnglishScore() {
        return englishScore;
    }
}

class ScoreManagementSystem {
    private List<Student> students;

    public ScoreManagementSystem() {
        students = new ArrayList<>();
    }

    public void addStudent(Student student) {
        students.add(student);
    }

    public void inputScores() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入学号:");
        int id = scanner.nextInt();
        System.out.print("请输入姓名:");
        scanner.nextLine();
        String name = scanner.nextLine();
        System.out.print("请输入语文成绩:");
        int chineseScore = scanner.nextInt();
        System.out.print("请输入数学成绩:");
        int mathScore = scanner.nextInt();
        System.out.print("请输入英语成绩:");
        int englishScore = scanner.nextInt();

        // 验证数据有效性
        if (id < 1 || id > 90 || chineseScore < 0 || chineseScore > 100 ||
                mathScore < 0 || mathScore > 100 || englishScore < 0 || englishScore > 100) {
            System.out.println("数据无效,无法录入成绩!");
            return;
        }

        Student student = new Student(id, name, chineseScore, mathScore, englishScore);
        addStudent(student);

        System.out.println("成绩录入成功!");
    }

    public void queryByStudentId(int id) {
        List<Student> results = new ArrayList<>();
        for (Student student : students) {
            if (student.getId() == id) {
                results.add(student);
            }
        }

        if (results.isEmpty()) {
            System.out.println("未找到该学生的成绩记录!");
            return;
        }

        System.out.println("查询结果:");
        for (Student student : results) {
            System.out.println("学号:" + student.getId() +
                    ",姓名:" + student.getName() +
                    ",语文成绩:" + student.getChineseScore() +
                    ",数学成绩:" + student.getMathScore() +
                    ",英语成绩:" + student.getEnglishScore());
        }
    }

    public void queryByStudentName(String name) {
        List<Student> results = new ArrayList<>();
        for (Student student : students) {
            if (student.getName().equalsIgnoreCase(name)) {
                results.add(student);
            }
        }

        if (results.isEmpty()) {
            System.out.println("未找到该学生的成绩记录!");
            return;
        }

        System.out.println("查询结果:");
        for (Student student : results) {
            System.out.println("学号:" + student.getId() +
                    ",姓名:" + student.getName() +
                    ",语文成绩:" + student.getChineseScore() +
                    ",数学成绩:" + student.getMathScore() +
                    ",英语成绩:" + student.getEnglishScore());
        }
    }

    public void queryFailedSubjects() {
        List<Student> results = new ArrayList<>();
        for (Student student : students) {
            if (student.getChineseScore() < 60) {
                results.add(student);
            }
            if (student.getMathScore() < 60) {
                results.add(student);
            }
            if (student.getEnglishScore() < 60) {
                results.add(student);
            }
        }

        if (results.isEmpty()) {
            System.out.println("未找到不及格的科目!");
            return;
        }

        System.out.println("不及格的科目:");
        for (Student student : results) {
            System.out.println("学号:" + student.getId() +
                    ",姓名:" + student.getName());
        }
    }

    public void calculateAverageScores() {
        double chineseTotal = 0;
        double mathTotal = 0;
        double englishTotal = 0;

        for (Student student : students) {
            chineseTotal += student.getChineseScore();
            mathTotal += student.getMathScore();
            englishTotal += student.getEnglishScore();
        }

        double chineseAverage = chineseTotal / students.size();
        double mathAverage = mathTotal / students.size();
        double englishAverage = englishTotal / students.size();

        System.out.println("各门课的平均分:");
        System.out.println("语文平均分:" + chineseAverage);
        System.out.println("数学平均分:" + mathAverage);
        System.out.println("英语平均分:" + englishAverage);
    }

    public void rankScores() {
        List<Student> chineseScores = new ArrayList<>(students);
        List<Student> mathScores = new ArrayList<>(students);
        List<Student> englishScores = new ArrayList<>(students);

        Comparator<Student> scoreComparator = Comparator.comparingInt(Student::getChineseScore);
        chineseScores.sort(scoreComparator.reversed());
        scoreComparator = Comparator.comparingInt(Student::getMathScore);
        mathScores.sort(scoreComparator.reversed());
        scoreComparator = Comparator.comparingInt(Student::getEnglishScore);
        englishScores.sort(scoreComparator.reversed());

        System.out.println("语文成绩排名:");
        for (int i = 0; i < chineseScores.size(); i++) {
            Student student = chineseScores.get(i);
            System.out.println("排名第 " + (i + 1) +
                    " 的学生:学号:" + student.getId() +
                    ",姓名:" + student.getName() +
                    ",成绩:" + student.getChineseScore());
        }

        System.out.println("数学成绩排名:");
        for (int i = 0; i < mathScores.size(); i++) {
            Student student = mathScores.get(i);
            System.out.println("排名第 " + (i + 1) +
                    " 的学生:学号:" + student.getId() +
                    ",姓名:" + student.getName() +
                    ",成绩:" + student.getMathScore());
        }

        System.out.println("英语成绩排名:");
        for (int i = 0; i < englishScores.size(); i++) {
            Student student = englishScores.get(i);
            System.out.println("排名第 " + (i + 1) +
                    " 的学生:学号:" + student.getId() +
                    ",姓名:" + student.getName() +
                    ",成绩:" + student.getEnglishScore());
        }
    }

    public void calculateGradeDistribution() {
        int below60 = 0;
        int from60to70 = 0;
        int from70to80 = 0;
        int from80to90 = 0;
        int above90 = 0;

        for (Student student : students) {
            int totalScore = student.getChineseScore() + student.getMathScore() + student.getEnglishScore();
            if (totalScore < 60) {
                below60++;
            } else if (totalScore < 70) {
                from60to70++;
            } else if (totalScore < 80) {
                from70to80++;
            } else if (totalScore < 90) {
                from80to90++;
            } else {
                above90++;
            }
        }

        System.out.println("各成绩区段的人数比例:");
        System.out.println("60分以下:" + (double) below60 / students.size());
        System.out.println("60分-70分:" + (double) from60to70 / students.size());
        System.out.println("70分-80分:" + (double) from70to80 / students.size());
        System.out.println("80分-90分:" + (double) from80to90 / students.size());
        System.out.println("90分以上:" + (double) above90 / students.size());
    }
}

public class Main {
    public static void main(String[] args) {
        ScoreManagementSystem system = new ScoreManagementSystem();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("------- 学生成绩管理系统 -------");
            System.out.println("1. 成绩录入");
            System.out.println("2. 成绩查询");
            System.out.println("3. 成绩统计");
            System.out.println("0. 退出系统");
            System.out.print("请选择操作:");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    system.inputScores();
                    break;
                case 2:
                    System.out.println("------- 成绩查询 -------");
                    System.out.println("1. 按学号查询");
                    System.out.println("2. 按姓名查询");
                    System.out.print("请选择查询方式:");
                    int queryChoice = scanner.nextInt();
                    switch (queryChoice) {
                        case 1:
                            System.out.print("请输入学号:");
                            int id = scanner.nextInt();
                            system.queryByStudentId(id);
                            break;
                        case 2:
                            System.out.print("请输入姓名:");
                            scanner.nextLine();
                            String name = scanner.nextLine();
                            system.queryByStudentName(name);
                            break;
                        default:
                            System.out.println("无效的选择!");
                            break;
                    }
                    break;
                case 3:
                    System.out.println("------- 成绩统计 -------");
                    System.out.println("1. 统计各门课的不及格人数及比例");
                    System.out.println("2. 统计各门课的平均分");
                    System.out.println("3. 按从最高分到最低分对各门课成绩进行排名");
                    System.out.println("4. 统计各成绩区段的人数比例");
                    System.out.print("请选择统计方式:");
                    int statChoice = scanner.nextInt();
                    switch (statChoice) {
                        case 1:
                            system.queryFailedSubjects();
                            break;
                        case 2:
                            system.calculateAverageScores();
                            break;
                        case 3:
                            system.rankScores();
                            break;
                        case 4:
                            system.calculateGradeDistribution();
                            break;
                        default:
                            System.out.println("无效的选择!");
                            break;
                    }
                    break;
                case 0:
                    System.out.println("已退出学生成绩管理系统!");
                    System.exit(0);
                default:
                    System.out.println("无效的选择!");
                    break;
            }
        }
    }
}



import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

class Student {
    private int id;
    private String name;
    private int chineseScore;
    private int mathScore;
    private int englishScore;

    public Student(int id, String name, int chineseScore, int mathScore, int englishScore) {
        this.id = id;
        this.name = name;
        this.chineseScore = chineseScore;
        this.mathScore = mathScore;
        this.englishScore = englishScore;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getChineseScore() {
        return chineseScore;
    }

    public int getMathScore() {
        return mathScore;
    }

    public int getEnglishScore() {
        return englishScore;
    }
}

class ScoreManagementSystem {
    private List<Student> students;

    public ScoreManagementSystem() {
        students = new ArrayList<>();
    }

    public void addStudent(Student student) {
        students.add(student);
    }

    public List<Student> searchById(int id) {
        List<Student> result = new ArrayList<>();
        for (Student student : students) {
            if (student.getId() == id) {
                result.add(student);
            }
        }
        return result;
    }

    public List<Student> searchByName(String name) {
        List<Student> result = new ArrayList<>();
        for (Student student : students) {
            if (student.getName().equalsIgnoreCase(name)) {
                result.add(student);
            }
        }
        return result;
    }

    public List<Student> searchByPartialName(String partialName) {
        List<Student> result = new ArrayList<>();
        for (Student student : students) {
            if (student.getName().toLowerCase().contains(partialName.toLowerCase())) {
                result.add(student);
            }
        }
        return result;
    }

    public void displayScores(List<Student> studentList) {
        for (Student student : studentList) {
            System.out.println("Student ID: " + student.getId());
            System.out.println("Student Name: " + student.getName());
            System.out.println("Chinese Score: " + student.getChineseScore());
            System.out.println("Math Score: " + student.getMathScore());
            System.out.println("English Score: " + student.getEnglishScore());
            System.out.println();
        }
    }

    public void displayFailedSubjects(List<Student> studentList) {
        for (Student student : studentList) {
            System.out.println("Student ID: " + student.getId());
            System.out.println("Student Name: " + student.getName());

            if (student.getChineseScore() < 60) {
                System.out.println("Failed in Chinese");
            }
            if (student.getMathScore() < 60) {
                System.out.println("Failed in Math");
            }
            if (student.getEnglishScore() < 60) {
                System.out.println("Failed in English");
            }

            System.out.println();
        }
    }

    public void displayScoreRankings() {
        students.sort(Comparator.comparingInt(Student::getChineseScore).reversed());
        System.out.println("Chinese Score Rankings:");
        displayRankings(students);

        students.sort(Comparator.comparingInt(Student::getMathScore).reversed());
        System.out.println("Math Score Rankings:");
        displayRankings(students);

        students.sort(Comparator.comparingInt(Student::getEnglishScore).reversed());
        System.out.println("English Score Rankings:");
        displayRankings(students);
    }

   private void displayRankings(List<Student> studentList) {
        int rank = 1;
        for (Student student : studentList) {
            System.out.println("Rank: " + rank);
            System.out.println("Student ID: " + student.getId());
            System.out.println("Student Name: " + student.getName());
            System.out.println("Chinese Score: " + student.getChineseScore());
            System.out.println("Math Score: " + student.getMathScore());
            System.out.println("English Score: " + student.getEnglishScore());
            System.out.println();
            rank++;
        }
    }

    public void displayScoreStatistics() {
        int chineseFailedCount = 0;
        int mathFailedCount = 0;
        int englishFailedCount = 0;

        double chineseAverage = 0.0;
        double mathAverage = 0.0;
        double englishAverage = 0.0;

        int totalStudents = students.size();

        for (Student student : students) {
            if (student.getChineseScore() < 60) {
                chineseFailedCount++;
            }
            if (student.getMathScore() < 60) {
                mathFailedCount++;
            }
            if (student.getEnglishScore() < 60) {
                englishFailedCount++;
            }

            chineseAverage += student.getChineseScore();
            mathAverage += student.getMathScore();
            englishAverage += student.getEnglishScore();
        }

        chineseAverage /= totalStudents;
        mathAverage /= totalStudents;
        englishAverage /= totalStudents;

        System.out.println("Chinese Failed Count: " + chineseFailedCount);
        System.out.println("Chinese Failed Ratio: " + (double)chineseFailedCount / totalStudents);

        System.out.println("Math Failed Count: " + mathFailedCount);
        System.out.println("Math Failed Ratio: " + (double)mathFailedCount / totalStudents);

        System.out.println("English Failed Count: " + englishFailedCount);
        System.out.println("English Failed Ratio: " + (double)englishFailedCount / totalStudents);

        System.out.println("Chinese Average Score: " + chineseAverage);
        System.out.println("Math Average Score: " + mathAverage);
        System.out.println("English Average Score: " + englishAverage);
    }
}

public class Main {
    public static void main(String[] args) {
        ScoreManagementSystem scoreSystem = new ScoreManagementSystem();

        // 添加学生及成绩信息
        scoreSystem.addStudent(new Student(1, "Alice", 90, 85, 95));
        scoreSystem.addStudent(new Student(2, "Bob", 80, 75, 85));
        scoreSystem.addStudent(new Student(3, "Charlie", 70, 65, 75));
        scoreSystem.addStudent(new Student(4, "David", 60, 55, 65));

        // 演示成绩查询功能
        List<Student> resultById = scoreSystem.searchById(2);
        scoreSystem.displayScores(resultById);

        List<Student> resultByName = scoreSystem.searchByName("Alice");
        scoreSystem.displayScores(resultByName);

        List<Student> resultByPartialName = scoreSystem.searchByPartialName("a");
        scoreSystem.displayScores(resultByPartialName);

        // 演示成绩统计功能
        scoreSystem.displayFailedSubjects(scoreSystem.searchByPartialName("a"));
        scoreSystem.displayScoreRankings();
        scoreSystem.displayScoreStatistics();
    }
}

  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/7517564
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:【JAVA笔记】习题:尝试编写一个矩形类,将长与宽作为矩形类的属性,在构造方法中将长,宽初始化,定义一个成员方法求此矩形的面积。
  • 你还可以看下java参考手册中的 java-学习Java语言 - 描述Java编程语言的基本概念和特点的课程。-泛型是Java编程语言的一个强大功能。它们提高了代码的类型安全性,使更多的错误可以在编译时发现。-野生动物
  • 除此之外, 这篇博客: java反射,反射常用方法,获取类模板,通过类模板对象创建实体对象,类中未定义有参或无参构造器的情况下创建对象,不调用方法尝试给一个类的私有属性赋值,通过反射获取一个类的父类并获取它实现的接口中的 java反射 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • Reflection(反射)是被视为动态语言的关键,反射机制允许程序在执行期(运行时)借助于Reflection API取得任何类的内部信息,并能直接操作任意对象的内部属性及方法

  • 您还可以看一下 徐无忌老师的Java进阶高手课-并发编程透彻理解课程中的 什么是多线程的上下文切换,有什么影响?如何进行优化?(一)小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    针对学生成绩管理系统,我会给出以下解决方案:

    1. 定义一个学生类Student 具有属性:姓名name、语文成绩chinese、数学成绩math、英语成绩english 具有方法:get和set方法

    2. 定义一个成绩管理类ScoreManager 具有属性:studentList(ArrayList) 具有方法: (1)addScore(添加学生成绩) 接收参数:Student对象 将该学生添加到studentList中 (2)searchScore(查询学生成绩) 接收参数:需要查询的科目(比如"语文") 遍历studentList,找到所有学生在该科目上的成绩,返回成绩列表 (3)statistics(统计学生成绩) 接收参数:需要统计的科目(比如"英语") 遍历studentList,计算所有学生在该科目上的总成绩,返回总分和平均分

    3. 在main方法中,实例化ScoreManager类,并调用各个方法实现相应功能。

    下面是Java代码实现:

    public class Student {
        private String name;
        private int chinese;
        private int math;
        private int english;
        // 构造方法
        public Student(String n, int c, int m, int e) {
            name = n;
            chinese = c;
            math = m;
            english = e;
        }
        // get和set方法
        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;
        }
    }
    
    import java.util.ArrayList;
    
    public class ScoreManager {
        private ArrayList<Student> studentList = new ArrayList<Student>();
        // 添加学生成绩
        public void addScore(Student s) {
            studentList.add(s);
        }
        // 查询学生成绩
        public ArrayList<Integer> searchScore(String subject) {
            ArrayList<Integer> scoreList = new ArrayList<Integer>();
            for (Student s : studentList) {
                if (subject.equals("语文")) {
                    scoreList.add(s.getChinese());
                } else if (subject.equals("数学")) {
                    scoreList.add(s.getMath());
                } else if (subject.equals("英语")) {
                    scoreList.add(s.getEnglish());
                }
            }
            return scoreList;
        }
        // 统计学生成绩
        public String statistics(String subject) {
            int sum = 0;
            if (subject.equals("语文")) {
                for (Student s : studentList) {
                    sum += s.getChinese();
                }
            } else if (subject.equals("数学")) {
                for (Student s : studentList) {
                    sum += s.getMath();
                }
            } else if (subject.equals("英语")) {
                for (Student s : studentList) {
                    sum += s.getEnglish();
                }
            }
            double avg = (double)sum / studentList.size();
            return "总分为" + sum + ",平均分为" + String.format("%.2f", avg);
        }
    }
    
    import java.util.ArrayList;
    
    public class Main {
        public static void main(String[] args) {
            Student s1 = new Student("Tom", 80, 90, 70);
            Student s2 = new Student("Jerry", 90, 70, 80);
            Student s3 = new Student("Jack", 70, 80, 90);
            ScoreManager sm = new ScoreManager();
            sm.addScore(s1);
            sm.addScore(s2);
            sm.addScore(s3);
            ArrayList<Integer> scoreList = sm.searchScore("语文");
            System.out.println("语文成绩列表:" + scoreList.toString());
            String result = sm.statistics("数学");
            System.out.println("数学成绩统计:" + result);
        }
    }