• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

gffg5559

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

OOP三次大作业总结-BLOG-3

面向对象程序设计三次大作业总结

一、前言。

 7-1 课程成绩统计程序-1

该题涉及的Java知识点包括输入输出、字符串处理、数据结构和算法、异常处理等。

题目要求解析输入的课程信息和成绩信息,并根据规定的计算方式计算学生的总成绩和平均分,以及各门课程的平均分和班级的总成绩平均分。同时需要处理各种异常情况。

题量较大,涉及多个输入输出项和计算要求。

难度适中,需要对输入输出进行合理的处理,同时需要注意各种异常情况的处理。需要对字符串处理和数据结构算法有一定的掌握。

在解决该题目时,可以考虑以下方面:

1. 输入输出处理:首先需要读取输入的课程信息和成绩信息,并将其解析为合适的数据结构进行存储和处理。同时,需要注意对输入格式的合法性进行校验,并对异常情况进行适当的处理。

2. 数据结构和算法:可以使用合适的数据结构来存储课程信息和成绩信息,例如使用HashMap来存储课程名称和对应的成绩列表,或使用对象来存储每个学生和对应的课程成绩信息。在计算总成绩和平均分时,可以使用循环遍历数据结构,并按照题目要求进行计算。

3. 异常处理:需要考虑各种异常情况的处理,例如输入格式错误、重复的课程或成绩信息、课程性质和考核方式不匹配等。对于这些异常情况,可以通过合适的异常处理机制进行捕获和处理,并输出对应的错误信息。

4. 计算总成绩和平均分:根据题目要求,需要根据课程的性质和考核方式来计算总成绩和平均分。对于必修课,采用考试方式计算总成绩;对于选修课,可以根据考试或考察方式来计算总成绩。在计算平均分时,需要进行加权平均。

5. 排序输出:根据题目要求,需要对学生和班级的成绩进行排序输出。可以使用排序算法对学生或班级的成绩进行排序,然后按照题目要求格式进行输出。

综上所述,该题目需要综合运用Java的输入输出、字符串处理、数据结构和算法、异常处理等知识点。对于这些知识点的熟练掌握和灵活运用,将有助于解决该题目。

7-3 课程成绩统计程序-2

该题涉及的Java知识点包括输入输出、字符串处理、数据结构和算法、异常处理等。相比上一版本,本题增加了处理实验课的内容,需要对实验成绩进行计算。

题量适中,涉及多个输入输出项和计算要求。

难度中等偏上,需要综合运用Java的输入输出、字符串处理、数据结构和算法、异常处理等知识点。另外,需要考虑对实验成绩的处理,并进行对应的计算。同时,还需注意各种异常情况的处理和输出结果的格式要求。对于这些知识点的熟练掌握和灵活运用,将有助于解决该题目。

在解决该题目时,可以考虑以下方面进行完善:

1. 进一步优化数据结构:可以使用合适的数据结构来存储课程信息和成绩信息,例如使用HashMap或者ArrayList来存储课程名称和对应的成绩列表,或者使用对象来存储每个学生和对应的课程成绩信息。可以根据需求进行选择,以便在后续的计算和输出中更高效地处理数据。

2. 实验课成绩的计算:由于新增了实验课程,需要对实验成绩进行计算。可以在数据结构中增加相应的字段来存储实验次数和每次实验成绩,并在计算总成绩时考虑实验成绩的权重和计算方式。

3. 高效处理重复的课程/成绩信息:当输入中出现重复的课程或成绩信息时,可以通过判断是否已经存在相同课程名称或学生信息来避免重复处理。可以使用HashSet或其他数据结构来记录已经处理过的课程信息,以提高程序的效率。

4. 考虑代码的可读性和可维护性:根据题目要求,可以合理拆分代码逻辑为多个方法,每个方法负责处理一个特定的功能,便于代码的组织和维护。还可以添加适当的注释和命名规范,提高代码的可读性和可维护性。

5. 提高程序的健壮性:需要对输入进行充分的合法性检查,并对异常情况进行处理。可以使用正则表达式对输入进行匹配,以确保输入的格式正确,并且需要进行各种边界值的判断,避免超出范围的错误。

6. 进一步优化算法:在计算总成绩和平均分时,可以考虑使用累加的方式来计算,在遍历数据结构时累加各门课程成绩,这样可以减少不必要的循环和计算。

综上所述,该题目需要综合运用Java的输入输出、字符串处理、数据结构和算法、异常处理等知识点,对于这些知识点的熟练掌握和灵活运用,将有助于解决该题目。在代码的逻辑结构、代码风格和异常处理等方面进行优化,可以提高程序的可读性、可维护性和健壮性。

7-2 课程成绩统计程序-3

该题涉及的Java知识点包括输入输出、字符串处理、数据结构和算法、异常处理等。相比上两个版本,本题修改了成绩类的继承关系为组合关系,并增加了分项成绩类来进行成绩的计算。

题量适中,涉及多个输入输出项和计算要求,同时还有对继承和组合关系的理解和比较。

难度中等,需要综合运用Java的输入输出、字符串处理、数据结构和算法、异常处理等知识点来解决问题。此外,还需要理解并比较继承和组合关系的区别,思考哪种关系更适应变更,并相应地修改代码。

通过修改类结构、采用组合关系的方式,可以使代码更加灵活,模块化程度更高。通过使用分项成绩类,可以根据权重计算各个分项的成绩,并在计算总成绩时进行累加。这样可以更容易适应需求的变更,例如增加新的分项成绩或修改权重,而无需修改大量的代码。

因此,通过使用组合关系来构建成绩类,代码更具灵活性和可维护性,能够更好地适应变更。对于这些知识点的熟练掌握和灵活运用,将有助于解决该题目,并能更好地理解和应用继承和组合关系。

在解决课程成绩统计程序-3的问题时,可以进一步考虑以下方面:

1. 组合关系的优势:与继承关系相比,组合关系更具灵活性和可扩展性。通过将分项成绩类与课程成绩类进行组合,可以轻松地处理单门课程的多个成绩分项,并根据权重来计算总成绩。这种设计模式符合“组合优于继承”的原则,能够更好地适应需求变更和扩展。

2. 可配置的分项成绩权重:由于新增了分项成绩类,在录入课程信息时,需要输入每个分项成绩的权重。这样的设计允许灵活地对分项成绩进行权重分配,可以根据实际情况进行设置。例如,对于某门课程,可以根据教学目标和考核要求来确定各个分项成绩的比重,从而更准确地计算总成绩。

3. 总成绩计算的实现:在计算总成绩时,通过分项成绩类的权重和成绩来计算各个分项的得分,然后将得分相加得到总成绩。这种分项成绩和总成绩的计算方式更加灵活,可以适应不同课程的考核方式和评分标准。

4. 异常处理的完善:需要对异常情况进行处理。例如,检查输入的分项成绩数量和权重的数量是否匹配,检查分项成绩权重的总和是否为1,以及对其他格式错误和越界的输入进行处理。通过合理的异常处理机制,可以更好地保证程序的稳定性和可靠性。

总的来说,课程成绩统计程序-3在前两个版本的基础上,通过改变成绩类的继承关系为组合关系,并引入了分项成绩类来处理分项成绩和计算总成绩。通过这种设计方式,代码变得更灵活、可扩展性更强,能够更好地适应需求的变更。对于这些知识点的理解和灵活运用,将有助于解决该问题,并在实际应用中具备更好的适应性和可维护性。

二、设计与分析。

对PTA中成绩计算系列题目的设计与分析。

课程成绩统计程序-1

1)类图

 

 2)分析

 

该代码是一个学生成绩管理系统,能够进行课程信息的输入、学生成绩的录入和统计分析。主要流程如下:

1. 首先,通过键盘输入获取课程和学生成绩的输入数据。

2. 输入数据以换行符分割成不同的行,然后分别处理每一行的数据。

3. 对于输入的每一行数据,使用正则表达式进行匹配,以确定输入类型。

4. 如果输入是课程信息,则根据输入的课程名称、性质和考核方式创建课程对象,并根据输入信息设置相应的属性。

5. 如果输入是学生成绩信息,则根据输入的学生学号、姓名和课程名称,创建学生对象和成绩对象,并根据输入信息设置相应的属性。如果该成绩信息对应的课程不存在,则输出错误提示信息。

6. 每录入一个学生的成绩,将成绩信息添加到相应学生的成绩列表中。

7. 在学生成绩录入完毕后,遍历学生列表,输出每个学生的学号、姓名和最终成绩。

8. 遍历课程列表,输出每门课程的名称、平时成绩、考试成绩和最终成绩。

9. 遍历班级列表,输出每个班级的班级号和平均成绩。

10. 最后,程序输出是否需要存储所有课程的信息的提示,如果需要,则创建一个存储课程信息的Map,然后通过键盘输入获取课程信息,并存储到Map中。

根据代码分析,总结该系统的功能如下:

- 可以录入课程信息,包括课程名称、性质和考核方式。
- 可以录入学生成绩信息,包括学生学号、姓名、课程名称和成绩。
- 统计并输出每个学生的最终成绩。
- 统计并输出每门课程的平时成绩、考试成绩和最终成绩。
- 统计并输出每个班级的平均成绩。
- 可以选择是否存储所有课程的信息,并提供相应的提示。

该代码使用了面向对象的思想,将课程、学生和班级的信息封装成了类,并通过对象的属性和方法进行操作和管理。同时,代码使用了正则表达式来匹配输入的内容,确保输入的数据格式正确。

需要注意的是,该代码中存在一些问题和改进空间。例如,Course类中的变量和方法应该使用private修饰符进行封装,避免直接访问和修改,可以增加相应的get和set方法。还有一些逻辑判断和错误处理的代码可以进一步优化。

3)源码

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Course[] course = new Course[100];
        Student[] student = new Student[100];
        Cls[] cls = new Cls[100];
        int courseNumber = 0;                            //课程数量
        int studentNumber = 0;                           //学生数量
        int classCount = 0;                             //班级数量
        StringBuilder t = new StringBuilder();
        while (sc.hasNextLine()) {
            String s = sc.nextLine();
            if (s.equals("end")) {
                break;
            }
            t.append(s).append("\n");
        }
        String str = t.toString();
        String[] lines = str.split("\n");
        for(String line : lines)
        {
            String[] words = line.split(" ");
            if(InputMatching.matchingInput(line)==1)
            {
                course[courseNumber] = new Course();
                course[courseNumber].setName(words[0]);
                if(words[1].equals("必修")) course[courseNumber].required = true;
                else if(words[1].equals("选修")) course[courseNumber].required = false;
                if(course[courseNumber].required) {
                    course[courseNumber].test = true;
                    if(words[2].equals("考察")){
                        course[courseNumber].format = false;
                        System.out.println(words[0]+" : course type & access mode mismatch");
                        course[courseNumber].setName(null);
                    }
                }else{
                    if(words[2].equals("考试")){
                        course[courseNumber].test = true;
                    }else if(words[2].equals("考察")){
                        course[courseNumber].test = false;
                    }
                }
                courseNumber++;
            }
            else if(InputMatching.matchingInput(line)==2)
            {
                int courseIndex = 0;
                int classIndex = 0;
                boolean exist = false;
                boolean createStudent = true;
                boolean createClass = true;
                String classID = words[0].substring(0,6);
                /*获取课程下标*/
                for(int i=0;i<courseNumber;i++){
                    if(words[2].equals(course[i].getName())){
                        courseIndex = i;
                        exist = true;
                        break;
                    }
                }
                if(!exist){
                    System.out.println(words[2]+" does not exist");
                }
                for(int i=0;i<classCount;i++){
                    if(classID.equals(cls[i].getID())){
                        createClass = false;
                        classIndex = i;
                        break;
                    }
                }
                if(createClass){
                    classIndex = courseNumber-1;
                    cls[classCount] = new Cls();
                    cls[classCount].setID(classID);
                    classCount++;
                }
                for(int i=0;i<studentNumber;i++){
                    if(words[0].equals(student[i].getID())){
                        createStudent = false;
                        break;
                    }
                }
                if(createStudent)
                {
                    student[studentNumber] = new Student();
                    student[studentNumber].setID(words[0]);
                    student[studentNumber].setName(words[1]);
                    if(words.length==4){
                        if(course[courseIndex].test){
                            if(exist)
                                System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            student[studentNumber].format = false;
                        }else{
                            student[studentNumber].setScore(Integer.parseInt(words[3]));
                            course[courseIndex].setTestScore(Integer.parseInt(words[3]));
                            course[courseIndex].setScore(Integer.parseInt(words[3]));
                            cls[classIndex].setScore(Integer.parseInt(words[3]));
                        }
                    }else if(words.length==5){
                        if(!course[courseIndex].test){
                            if(exist)
                                System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            student[studentNumber].format = false;
                        }else{
                            student[studentNumber].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                            cls[classIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                            course[courseIndex].setTestScore(Integer.parseInt(words[4]));
                            course[courseIndex].setTimeScore(Integer.parseInt(words[3]));
                            course[courseIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                        }
                    }
                    studentNumber++;
                }
            }
            else System.out.println("wrong format");
        }
        for(int i=0;i<studentNumber;i++){
            if(student[i].getScore()==0){
                System.out.println(student[i].getID()+" "+student[i].getName()+ " did not take any exams");
            }
            else{
                System.out.println(student[i].getID()+" "+student[i].getName()+" "+ student[i].lastScore());
            }
        }
        for(int i=0;i<courseNumber;i++){
            if(course[i].getScore()==0&&course[i].format){
                System.out.println(course[i].getName()+" has no grades yet");
            }else{
                if(course[i].test&&course[i].format){
                    System.out.println(course[i].getName()+" "+(int)course[i].getTimeScore()/course[i].getScoreCount()+" "+
                            (int)course[i].getTestScore()/course[i].getScoreCount()+" "+(int)course[i].getScore()/course[i].getScoreCount());
                }else if(course[i].format){
                    System.out.println(course[i].getName()+" "+
                            (int)course[i].getTestScore()/course[i].getScoreCount()+" "+(int)course[i].getScore()/course[i].getScoreCount());
                }
            }
        }
        for(int i=0;i<classCount;i++){
            if(cls[i].getScore()==0){
                System.out.println(cls[i].getID()+" has no grades yet");
            }else{
                System.out.println(cls[i].getID()+" "+(int)cls[i].getScore()/cls[i].getScoreCount());
            }
        }
        boolean output = false;
        if(output){
            Scanner scanner = new Scanner(System.in);
            // 存储所有课程的信息
            Map<String, Course> courses = new TreeMap<>();

            // 存储每个学生的成绩信息
            Map<String, List<Grade>> students = new TreeMap<>();

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String[] parts = line.split(" ");

                if (parts.length == 3) { // 处理课程信息
                    String name = parts[0];
                    String nature = parts[1];
                    String mode = parts[2];

                    // 检查是否有重复的课程信息


                    // 检查课程性质和考核方式是否匹配


                } else { // 处理成绩信息
                    if(parts[0].equals("end"))
                        break;
                    String id = parts[0];
                    String name = parts[1];
                    String courseName = parts[2];

                    List<Grade> grades = students.get(id);
                    if (grades == null) {
                        grades = new ArrayList<>();
                        students.put(id, grades);
                    }

                    // 检查成绩信息中的课程是否存在

                    if (course == null) {
                        System.out.println(id + " " + name + " : " + courseName + " does not exist");
                        continue;
                    }

                    if (grades.stream().anyMatch(g -> g.courseName.equals(courseName))) { // 已有该门课程的成绩信息,忽略后续的成绩信息
                        continue;
                    }

                    int usualScore = -1;
                    int examScore;
                    try {
                        examScore = Integer.parseInt(parts[parts.length - 1]);
                        if (parts.length == 5) {
                            usualScore = Integer.parseInt(parts[3]);
                        }
                    } catch (NumberFormatException e) {
                        System.out.println("wrong format");
                        continue;
                    }

                    // 检查成绩数量和课程的考核方式是否匹配

                    Grade grade = new Grade(id, name, courseName, usualScore, examScore);
                    grades.add(grade);
                }
            }
        }
    }
}
class Course{
    private String name;
    private double timeScore=0;
    private double testScore=0;
    private double score=0;
    private int scoreCount=0;
    public boolean test;
    public boolean required;
    public boolean format = true;

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

    public double getTimeScore() {
        return timeScore;
    }

    public void setTimeScore(double timeScore) {
        this.timeScore += timeScore;
    }

    public double getTestScore() {
        return testScore;
    }

    public void setTestScore(double testScore) {
        this.testScore += testScore;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score += score;
        this.scoreCount++;
    }

    public int getScoreCount() {
        return scoreCount;
    }
}
class Student{
    private String ID;
    private String name;
    private double score;
    private int scoreCount=0;
    public boolean format = true;

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score += score;
        this.scoreCount++;
    }
    public int lastScore(){
        return (int)score/scoreCount;
    }
}
class Cls{
    private String ID;
    private double score;
    private int scoreCount=0;
    public void setID(String ID){
        this.ID = ID;
    }
    public String getID(){
        return ID;
    }

    public void setScore(double score) {
        this.score += score;
        this.scoreCount++;
    }
    public double getScore(){
        return score;
    }

    public int getScoreCount() {
        return scoreCount;
    }
}
class InputMatching {
    static String stuNumMatching = "[0-9]{8}";//8个0-9的数字
    static String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
    static String scoreMatching = "([1-9]?[0-9]|100)";
    static String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
    static String courseTypeMatching = "(选修|必修)";
    static String checkCourseTypeMatching = "(考试|考察)";

    static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching;
    /*courseInput用于定义课程信息模式(正则表达式)*/
    static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
            scoreMatching + "(scoreMatching)?";
    /*scoreInput用于定义成绩信息模式(正则表达式)*/

    public static int matchingInput(String s) {
        if (matchingCourse(s)) {
            return 1;
        }
        if (matchingScore(s)||s.startsWith("2")) {
            return 2;
        }
        return 0;
    }
    private static boolean matchingCourse(String s) {
        return s.matches(courseInput);
    }
    private static boolean matchingScore(String s) {
        return s.matches(scoreInput);
    }
}
class Course14 {
    String name;
    String nature;
    String mode;

    public void Course(String name, String nature, String mode) {
        this.name = name;
        this.nature = nature;
        this.mode = mode;
    }
}

class Grade {
    String id;
    String name;
    String courseName;
    int usualScore;
    int examScore;

    public Grade(String id, String name, String courseName, int usualScore, int examScore) {
        this.id = id;
        this.name = name;
        this.courseName = courseName;
        this.usualScore = usualScore;
        this.examScore = examScore;
    }

    public int getScore() {
        if (usualScore == -1 || examScore == -1) {
            return -1; // 成绩不合法
        }
        return (int)Math.round(usualScore * 0.3 + examScore * 0.7); // 四舍五入取整
    }
}

 

课程成绩统计程序-2

1)类图

 2)分析

该代码是一个学生成绩管理系统,与上一个代码相比,做了一些修改和优化。主要改动如下:

1. 在课程信息录入部分,增加了"实验"作为课程类型,并对该类型课程进行了单独处理。

2. 在学生成绩录入部分,对输入的成绩进行了一些合法性校验和错误处理。如果成绩超过100或小于0,则输出错误提示信息。

3. 根据输入行中的长度判断是否存在多个成绩,如果存在,则对多个成绩进行处理,计算平均值。

4. 在输出部分,对学生列表、课程列表和班级列表进行了排序。学生列表按照学号升序排列,课程列表按照课程名称升序排列,班级列表按照班级号升序排列。

5. 修改了InputMatching类中正则表达式的定义,以适应新的输入格式。

6. 删除了一些没有使用的变量和代码块。

总体来说,该版本的代码在功能和逻辑上与上一个版本相似,但进行了一些优化和调整,使得代码更加清晰和简洁。同时,对输入的成绩进行了合法性校验和错误处理,对输出结果进行了排序,提高了程序的稳定性和可读性。

3)源码

import java.util.*;
import java.text.Collator;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Course[] course = new Course[100];
        Student[] student = new Student[100];
        Cls[] cls = new Cls[100];
        int fsfu = 0;                            //课程数量
        int afhas = 0;                           //学生数量
        int classCount = 0;                             //班级数量
        /*获取输入*/
        StringBuilder sb = new StringBuilder();
        HashSet<String> set = new HashSet<>();
        while (sc.hasNextLine()) {
            String s = sc.nextLine();
            if (s.equals("end")) {
                break;
            }
            if (set.add(s)) { // 添加成功则说明是第一次出现的行,才将其加入 StringBuilder 中
                sb.append(s).append("\n");
            }
        }
        String str = sb.toString();
        String[] lines = str.split("\n");
        for(String line : lines)
        {
            String[] words = line.split(" ");
            boolean repeat = false;
            if(InputMatching.matchingInput(line)==1)
            {
                for(int i=0;i<fsfu;i++){
                    if(words[0].equals(course[i].getName())){
                        repeat = true;
                        break;
                    }
                }if(repeat) continue;
                course[fsfu] = new Course();
                course[fsfu].setName(words[0]);
                /*选修与必修*/
                switch (words[1]) {
                    case "必修":
                        course[fsfu].required = true;
                        break;
                    case "选修":
                        course[fsfu].required = false;
                        break;
                    case "实验":
                        course[classCount].pre = true;
                        break;
                }
                /*考试与考察与实验*/
                boolean pra = false;
                if(words[1].equals("实验")){
                    pra = true;
                    if(words[2].equals("实验")){
                        course[fsfu].pre = true;
                    }else {
                        course[fsfu].format = false;
                        System.out.println(words[0]+" : course type & access mode mismatch");
                        course[fsfu].setName(null);
                    }
                }
                if(course[fsfu].required&&!pra) {
                    course[fsfu].test = true;
                    if(words[2].equals("考察")||words[2].equals("实验")){
                        course[fsfu].format = false;
                        System.out.println(words[0]+" : course type & access mode mismatch");
                        course[fsfu].setName(null);
                    }
                }else if(!pra){
                    switch (words[2]) {
                        case "考试":
                            course[fsfu].test = true;
                            break;
                        case "考察":
                            course[fsfu].test = false;
                            break;
                        case "实验":
                            course[fsfu].format = false;
                            System.out.println(words[0] + " : course type & access mode mismatch");
                            course[fsfu].setName(null);
                            break;
                    }
                }

                fsfu++;
            }
            else if(InputMatching.matchingInput(line)==2||words.length>=5)
            {
                boolean mistake = false;
                if(Integer.parseInt(words[3])>100){
                    System.out.println("wrong format");
                    continue;
                }
                if(words.length>5){
                    if(Integer.parseInt(words[3])<4||Integer.parseInt(words[3])>9){
                        System.out.println("wrong format");
                        continue;
                    }try {
                        for (int i = 0; i < Integer.parseInt(words[3]); i++) {
                            if (Integer.parseInt(words[i + 4]) > 100 || Integer.parseInt(words[i + 4]) < 0) {
                                mistake = true;
                            }
                        }
                    }catch (ArrayIndexOutOfBoundsException ignored){

                    }
                    if(mistake) {
                        System.out.println("wrong format");
                        continue;
                    }
                }
                int dgjhds = 0;
                int gdghjds = 0;
                int studentIndex = 0;
                boolean exist = false;
                boolean createStudent = true;
                boolean createClass = true;
                String classID = words[0].substring(0,6);
                /*获取课程下标*/
                for(int i=0;i<fsfu;i++){
                    if(words[2].equals(course[i].getName())){
                        dgjhds = i;
                        exist = true;
                        break;
                    }
                }
                /*不存在课程*/
                if(!exist){
                    System.out.println(words[2]+" does not exist");
                }
                /*已存在该班*/
                for(int i=0;i<classCount;i++){
                    if(classID.equals(cls[i].getID())){
                        createClass = false;
                        gdghjds = i;
                        break;
                    }
                }
                if(createClass){
                    gdghjds = classCount;
                    cls[classCount] = new Cls();
                    cls[classCount].setID(classID);
                    classCount++;
                }
                /*已存在该学生*/
                for(int i=0;i<afhas;i++){
                    if(words[0].equals(student[i].getID())){
                        createStudent = false;
                        studentIndex = i;
                        break;
                    }
                }
                if(createStudent) {
                    student[afhas] = new Student();
                    student[afhas].setID(words[0]);
                    student[afhas].setName(words[1]);
                    if(words.length==4){
                        if(course[dgjhds].pre){
                            System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            continue;
                        }
                        if(course[dgjhds].test){
                            if(exist)
                                System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            student[afhas].format = false;
                        }else{
                            student[afhas].setScore(Integer.parseInt(words[3]));
                            course[dgjhds].setTestScore(Integer.parseInt(words[3]));
                            course[dgjhds].setScore(Integer.parseInt(words[3]));
                            cls[gdghjds].setScore(Integer.parseInt(words[3]));
                        }
                    }else if(words.length==5){
                        if(course[dgjhds].pre){
                            System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            continue;
                        }
                        if(!course[dgjhds].test){
                            if(exist)
                                System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            student[afhas].format = false;
                        }else{
                            student[afhas].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                            cls[gdghjds].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                            course[dgjhds].setTestScore(Integer.parseInt(words[4]));
                            course[dgjhds].setTimeScore(Integer.parseInt(words[3]));
                            course[dgjhds].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                        }
                    }else if(words.length<8){
                        System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                    }else {
                        double theWhole = 0;
                        double theAverage;
                        int gdsfdshgd = Integer.parseInt(words[3]);
                        if(words.length-4 != gdsfdshgd){
                            System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            continue;
                        }
                        for(int i = 0;i<gdsfdshgd;i++){
                            theWhole += Integer.parseInt(words[i+4]);
                        }
                        theAverage = theWhole / gdsfdshgd;
                        student[afhas].setScore(theAverage);
                        course[dgjhds].setScore(theAverage);
                        cls[gdghjds].setScore(theAverage);
                    }
                    afhas++;
                }else {
                    if(words.length==4){
                        if(course[dgjhds].test){
                            if(exist)
                                System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            student[studentIndex].format = false;
                        }else{
                            student[studentIndex].setScore(Integer.parseInt(words[3]));
                            course[dgjhds].setTestScore(Integer.parseInt(words[3]));
                            course[dgjhds].setScore(Integer.parseInt(words[3]));
                            cls[gdghjds].setScore(Integer.parseInt(words[3]));
                        }
                    }else if(words.length==5){
                        if(!course[dgjhds].test){
                            if(exist)
                                System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            student[studentIndex].format = false;
                        }else{
                            student[studentIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                            cls[gdghjds].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                            course[dgjhds].setTestScore(Integer.parseInt(words[4]));
                            course[dgjhds].setTimeScore(Integer.parseInt(words[3]));
                            course[dgjhds].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                        }
                    }else if(words.length<8){
                        System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                    }
                    else {
                        double theWhole = 0;
                        double theAverage;
                        int gdsfdshgd = Integer.parseInt(words[3]);
                        if(words.length-4 != gdsfdshgd){
                            System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            continue;
                        }
                        for(int i = 0;i<gdsfdshgd;i++){
                            theWhole += Integer.parseInt(words[i+4]);
                        }
                        theAverage = theWhole / gdsfdshgd;
                        student[afhas].setScore(theAverage);
                        course[dgjhds].setScore(theAverage);
                        cls[gdghjds].setScore(theAverage);
                    }
                }
            }else System.out.println("wrong format");
        }
        List<Student> studentList = new ArrayList<>(Arrays.asList(student).subList(0, afhas));
        List<Cls> clsList = new ArrayList<>(Arrays.asList(cls).subList(0, classCount));
        List<Course> courseList = new ArrayList<>(Arrays.asList(course).subList(0, fsfu));
        Collections.sort(studentList);
        Collections.sort(clsList);
        courseList.sort(new CourseComparator());
        for(Student students : studentList){
            if(students.getScore()==0){
                System.out.println(students.getID()+" "+students.getName()+ " did not take any exams");
            }
            else{
                System.out.println(students.getID()+" "+students.getName()+" "+ students.lastScore());
            }
        }

        for(Course course1 : courseList){
            if(course1.getScore()==0&&course1.format){
                System.out.println(course1.getName()+" has no grades yet");
            }else{
                if(course1.pre&&course1.format){
                    System.out.println(course1.getName()+" "+ (int)course1.getScore()/course1.getScoreCount());
                }
                else if(course1.test&&course1.format){
                    System.out.println(course1.getName()+" "+(int)course1.getTimeScore()/course1.getScoreCount()+" "+
                            (int)course1.getTestScore()/course1.getScoreCount()+" "+(int)course1.getScore()/course1.getScoreCount());
                }else if(course1.format){
                    System.out.println(course1.getName()+" "+
                            (int)course1.getTestScore()/course1.getScoreCount()+" "+(int)course1.getScore()/course1.getScoreCount());
                }
            }
        }
        for(Cls cls1 : clsList){
            if(cls1.getScore()==0){
                System.out.println(cls1.getID()+" has no grades yet");
            }else{
                System.out.println(cls1.getID()+" "+(int)cls1.getScore()/cls1.getScoreCount());
            }
        }
        boolean useful = false;
        if(useful){
            HashMap<String,String> map = new HashMap<>();
            String str2;
            while(true) {
                str = sc.nextLine();
                if(str.equals("end")){
                    break;
                }
                String []arr = str.split(" ");
                String num = arr[0];
                String name = arr[1];
                String grade = arr[2];
                if(map.containsKey(num)){
                    System.out.println("学号重复,请重新输入");
                } else {
                    map.put(num,name+" "+grade);
                }
            }
            String s = sc.nextLine();
            if(map.containsKey(s)){
                String []arr = map.get(s).split(" ");
                System.out.println(s+" "+arr[0]+" "+arr[1]);
            } else {
                System.out.println("The student "+s+" does not exist");
            }
        }
        Map<Integer, String[]> map = new HashMap<>(); //使用HashMap存储学生信息,键为学号,值为字符串数组,依次存储学号、姓名、成绩

        while (true) {
            String[] input = sc.nextLine().split(" ");
            if (input[0].equals("end")) {
                break;
            }
            int id = Integer.parseInt(input[0]);
            String[] info = {input[1], input[2]}; //将姓名和成绩存储在一个字符串数组中
            map.put(id, info);
        }

        List<Integer> ids = new ArrayList<>(map.keySet());
        Collections.sort(ids, Collections.reverseOrder()); //按学号从大到小排序

        for (int id : ids) {
            String[] info = map.get(id);
            System.out.println(id + " " + info[0] + " " + info[1]); //依次输出学号、姓名、成绩
        }

    }
}
class CourseComparator implements Comparator<Course> {
    @Override
    public int compare(Course c1, Course c2) {
        Collator collator = Collator.getInstance(Locale.CHINA);
        return collator.compare(c1.getName(), c2.getName());
    }
}
class Course implements Comparable<Course>{
    private String name;
    private double timeScore=0;
    private double testScore=0;
    private double score=0;
    private int scoreCount=0;
    public boolean test;
    public boolean required;
    public boolean format = true;
    public boolean pre;

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

    public double getTimeScore() {
        return timeScore;
    }

    public void setTimeScore(double timeScore) {
        this.timeScore += timeScore;
    }

    public double getTestScore() {
        return testScore;
    }

    public void setTestScore(double testScore) {
        this.testScore += testScore;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score += score;
        this.scoreCount++;
    }

    public int getScoreCount() {
        return scoreCount;
    }
    // 实现Comparable接口中的compareTo方法,按照课程名称字符顺序排列
    @Override
    public int compareTo(Course other) {
        return this.name.compareTo(other.getName());
    }
}
class Student implements Comparable<Student>{
    private String ID;
    private String name;
    private double score;
    private int scoreCount=0;
    public boolean format = true;

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score += score;
        this.scoreCount++;
    }
    public int lastScore(){
        return (int)score/scoreCount;
    }
    // 定义比较规则:按照id升序排列
    @Override
    public int compareTo(Student o) {
        return Integer.compare(Integer.parseInt(this.ID), Integer.parseInt(o.ID));
    }
}
class Cls implements Comparable<Cls>{
    private String ID;
    private double score;
    private int scoreCount=0;
    public void setID(String ID){
        this.ID = ID;
    }
    public String getID(){
        return ID;
    }

    public void setScore(double score) {
        this.score += score;
        this.scoreCount++;
    }
    public double getScore(){
        return score;
    }

    public int getScoreCount() {
        return scoreCount;
    }
    // 定义比较规则:按照id升序排列
    @Override
    public int compareTo(Cls o) {
        return Integer.compare(Integer.parseInt(this.ID), Integer.parseInt(o.ID));
    }
}
class InputMatching {
    static String stuNumMatching = "[0-9]{8}";//8个0-9的数字
    static String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
    static String scoreMatching = "([1-9]?[0-9]|100)";
    static String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
    static String courseTypeMatching = "(选修|必修|实验)";
    static String checkCourseTypeMatching = "(考试|考察|实验)";

    static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching;
    /*courseInput用于定义课程信息模式(正则表达式)*/
    static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
            scoreMatching + "(scoreMatching)?";
    static String scoreInput2 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
            scoreMatching + "(scoreMatching)?" + " " + scoreMatching + "(scoreMatching)?";

    public static int matchingInput(String s) {
        if (matchingCourse(s)) {
            return 1;
        }
        if (matchingScore(s)) {
            return 2;
        }
        return 0;
    }
    private static boolean matchingCourse(String s) {
        return s.matches(courseInput);
    }
    private static boolean matchingScore(String s) {
        return s.matches(scoreInput)||s.matches(scoreInput2);
    }
}

 

课程成绩统计程序-3

1)类图

 

2)分析

该代码实现了一个学生选课成绩管理系统。主要功能包括输入课程信息和学生成绩,然后按照一定规则进行处理和排序,并最终输出结果。

在Main类中,首先通过Scanner从控制台获取输入的课程信息和学生成绩,然后根据不同的输入情况进行处理和计算。最后将处理后的结果按照一定的排序规则输出。

Course类封装了课程的相关信息和成绩,包括课程名称、考试成绩、平时成绩等。实现了Comparable接口,通过课程名称进行排序。

Student类封装了学生的相关信息和成绩,包括学生ID、姓名、成绩等。实现了Comparable接口,通过学生ID进行排序。

Cls类封装了班级的相关信息和成绩,包括班级ID和班级平均成绩。实现了Comparable接口,通过班级ID进行排序。

InputMatching类用于检查输入的格式是否符合要求,定义了一系列正则表达式来进行匹配。

CourseComparator类实现了Comparator接口,用于对课程进行排序,按照课程名称字符顺序排列。

综上所述,该代码实现了学生选课成绩管理系统,并通过合适的数据结构和排序算法对数据进行处理和输出。

3)源码

import java.util.*;
import java.text.Collator;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Course[] course = new Course[100];
        Student[] student = new Student[100];
        Cls[] cls = new Cls[100];
        int courseCount = 0;                            //课程数量
        int studentCount = 0;                           //学生数量
        int classCount = 0;                             //班级数量
        /*获取输入*/
        StringBuilder sb = new StringBuilder();
        HashSet<String> set = new HashSet<>();
        while (sc.hasNextLine()) {
            String s = sc.nextLine();
            if (s.equals("end")) {
                break;
            }
            if (set.add(s)) { // 添加成功则说明是第一次出现的行,才将其加入 StringBuilder 中
                sb.append(s).append("\n");
            }
        }
        String str = sb.toString();
        String[] lines = str.split("\n");
        for(String line : lines)
        {
            String[] words = line.split(" ");
            boolean repeat = false;
            if(InputMatching.matchingInput(line)==1)
            {
                for(int i=0;i<courseCount;i++){
                    if(words[0].equals(course[i].getName())){
                        repeat = true;
                        break;
                    }
                }if(repeat) continue;
                course[courseCount] = new Course();
                course[courseCount].setName(words[0]);
                /*选修与必修*/
                switch (words[1]) {
                    case "必修":
                        course[courseCount].required = true;
                        break;
                    case "选修":
                        course[courseCount].required = false;
                        break;
                    case "实验":
                        course[classCount].pre = true;
                        break;
                }
                /*考试与考察与实验*/
                boolean pra = false;
                if(words[1].equals("实验")){
                    pra = true;
                    if(words[2].equals("实验")){
                        course[courseCount].pre = true;
                    }else {
                        course[courseCount].format = false;
                        System.out.println(words[0]+" : course type & access mode mismatch");
                        course[courseCount].setName(null);
                    }
                }
                if(course[courseCount].required&&!pra) {
                    course[courseCount].test = true;
                    if(words[2].equals("考察")||words[2].equals("实验")){
                        course[courseCount].format = false;
                        System.out.println(words[0]+" : course type & access mode mismatch");
                        course[courseCount].setName(null);
                    }
                }else if(!pra){
                    switch (words[2]) {
                        case "考试":
                            course[courseCount].test = true;
                            break;
                        case "考察":
                            course[courseCount].test = false;
                            break;
                        case "实验":
                            course[courseCount].format = false;
                            System.out.println(words[0] + " : course type & access mode mismatch");
                            course[courseCount].setName(null);
                            break;
                    }
                }

                courseCount++;
            }
            else if(InputMatching.matchingInput(line)==2||words.length>=5)
            {
                boolean mistake = false;
                if(Integer.parseInt(words[3])>100){
                    System.out.println("wrong format");
                    continue;
                }
                if(words.length>5){
                    if(Integer.parseInt(words[3])<4||Integer.parseInt(words[3])>9){
                        System.out.println("wrong format");
                        continue;
                    }try {
                        for (int i = 0; i < Integer.parseInt(words[3]); i++) {
                            if (Integer.parseInt(words[i + 4]) > 100 || Integer.parseInt(words[i + 4]) < 0) {
                                mistake = true;
                            }
                        }
                    }catch (ArrayIndexOutOfBoundsException ignored){

                    }
                    if(mistake) {
                        System.out.println("wrong format");
                        continue;
                    }
                }
                int courseIndex = 0;
                int classIndex = 0;
                int studentIndex = 0;
                boolean exist = false;
                boolean createStudent = true;
                boolean createClass = true;
                String classID = words[0].substring(0,6);
                /*获取课程下标*/
                for(int i=0;i<courseCount;i++){
                    if(words[2].equals(course[i].getName())){
                        courseIndex = i;
                        exist = true;
                        break;
                    }
                }
                /*不存在课程*/
                if(!exist){
                    System.out.println(words[2]+" does not exist");
                }
                /*已存在该班*/
                for(int i=0;i<classCount;i++){
                    if(classID.equals(cls[i].getID())){
                        createClass = false;
                        classIndex = i;
                        break;
                    }
                }
                if(createClass){
                    classIndex = classCount;
                    cls[classCount] = new Cls();
                    cls[classCount].setID(classID);
                    classCount++;
                }
                /*已存在该学生*/
                for(int i=0;i<studentCount;i++){
                    if(words[0].equals(student[i].getID())){
                        createStudent = false;
                        studentIndex = i;
                        break;
                    }
                }
                if(createStudent) {
                    student[studentCount] = new Student();
                    student[studentCount].setID(words[0]);
                    student[studentCount].setName(words[1]);
                    if(words.length==4){
                        if(course[courseIndex].pre){
                            System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            continue;
                        }
                        if(course[courseIndex].test){
                            if(exist)
                                System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            student[studentCount].format = false;
                        }else{
                            student[studentCount].setScore(Integer.parseInt(words[3]));
                            course[courseIndex].setTestScore(Integer.parseInt(words[3]));
                            course[courseIndex].setScore(Integer.parseInt(words[3]));
                            cls[classIndex].setScore(Integer.parseInt(words[3]));
                        }
                    }else if(words.length==5){
                        if(course[courseIndex].pre){
                            System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            continue;
                        }
                        if(!course[courseIndex].test){
                            if(exist)
                                System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            student[studentCount].format = false;
                        }else{
                            student[studentCount].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                            cls[classIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                            course[courseIndex].setTestScore(Integer.parseInt(words[4]));
                            course[courseIndex].setTimeScore(Integer.parseInt(words[3]));
                            course[courseIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                        }
                    }else if(words.length<8){
                        System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                    }else {
                        double theWhole = 0;
                        double theAverage;
                        int thePre = Integer.parseInt(words[3]);
                        if(words.length-4 != thePre){
                            System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            continue;
                        }
                        for(int i = 0;i<thePre;i++){
                            theWhole += Integer.parseInt(words[i+4]);
                        }
                        theAverage = theWhole / thePre;
                        student[studentCount].setScore(theAverage);
                        course[courseIndex].setScore(theAverage);
                        cls[classIndex].setScore(theAverage);
                    }
                    studentCount++;
                }else {
                    if(words.length==4){
                        if(course[courseIndex].test){
                            if(exist)
                                System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            student[studentIndex].format = false;
                        }else{
                            student[studentIndex].setScore(Integer.parseInt(words[3]));
                            course[courseIndex].setTestScore(Integer.parseInt(words[3]));
                            course[courseIndex].setScore(Integer.parseInt(words[3]));
                            cls[classIndex].setScore(Integer.parseInt(words[3]));
                        }
                    }else if(words.length==5){
                        if(!course[courseIndex].test){
                            if(exist)
                                System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            student[studentIndex].format = false;
                        }else{
                            student[studentIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                            cls[classIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                            course[courseIndex].setTestScore(Integer.parseInt(words[4]));
                            course[courseIndex].setTimeScore(Integer.parseInt(words[3]));
                            course[courseIndex].setScore(Integer.parseInt(words[3])*0.3+Integer.parseInt(words[4])*0.7);
                        }
                    }else if(words.length<8){
                        System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                    }
                    else {
                        double theWhole = 0;
                        double theAverage;
                        int thePre = Integer.parseInt(words[3]);
                        if(words.length-4 != thePre){
                            System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
                            continue;
                        }
                        for(int i = 0;i<thePre;i++){
                            theWhole += Integer.parseInt(words[i+4]);
                        }
                        theAverage = theWhole / thePre;
                        student[studentCount].setScore(theAverage);
                        course[courseIndex].setScore(theAverage);
                        cls[classIndex].setScore(theAverage);
                    }
                }
            }else System.out.println("wrong format");
        }
        List<Student> studentList = new ArrayList<>(Arrays.asList(student).subList(0, studentCount));
        List<Cls> clsList = new ArrayList<>(Arrays.asList(cls).subList(0, classCount));
        List<Course> courseList = new ArrayList<>(Arrays.asList(course).subList(0, courseCount));
        Collections.sort(studentList);
        Collections.sort(clsList);
        courseList.sort(new CourseComparator());
        for(Student students : studentList){
            if(students.getScore()==0){
                System.out.println(students.getID()+" "+students.getName()+ " did not take any exams");
            }
            else{
                System.out.println(students.getID()+" "+students.getName()+" "+ students.lastScore());
            }
        }

        for(Course course1 : courseList){
            if(course1.getScore()==0&&course1.format){
                System.out.println(course1.getName()+" has no grades yet");
            }else{
                if(course1.pre&&course1.format){
                    System.out.println(course1.getName()+" "+ (int)course1.getScore()/course1.getScoreCount());
                }
                else if(course1.test&&course1.format){
                    System.out.println(course1.getName()+" "+(int)course1.getTimeScore()/course1.getScoreCount()+" "+
                            (int)course1.getTestScore()/course1.getScoreCount()+" "+(int)course1.getScore()/course1.getScoreCount());
                }else if(course1.format){
                    System.out.println(course1.getName()+" "+
                            (int)course1.getTestScore()/course1.getScoreCount()+" "+(int)course1.getScore()/course1.getScoreCount());
                }
            }
        }
        for(Cls cls1 : clsList){
            if(cls1.getScore()==0){
                System.out.println(cls1.getID()+" has no grades yet");
            }else{
                System.out.println(cls1.getID()+" "+(int)cls1.getScore()/cls1.getScoreCount());
            }
        }
    }
}
class CourseComparator implements Comparator<Course> {
    @Override
    public int compare(Course c1, Course c2) {
        Collator collator = Collator.getInstance(Locale.CHINA);
        return collator.compare(c1.getName(), c2.getName());
    }
}
class Course implements Comparable<Course>{
    private String name;
    private double timeScore=0;
    private double testScore=0;
    private double score=0;
    private int scoreCount=0;
    public boolean test;
    public boolean required;
    public boolean format = true;
    public boolean pre;

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

    public double getTimeScore() {
        return timeScore;
    }

    public void setTimeScore(double timeScore) {
        this.timeScore += timeScore;
    }

    public double getTestScore() {
        return testScore;
    }

    public void setTestScore(double testScore) {
        this.testScore += testScore;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score += score;
        this.scoreCount++;
    }

    public int getScoreCount() {
        return scoreCount;
    }
    // 实现Comparable接口中的compareTo方法,按照课程名称字符顺序排列
    @Override
    public int compareTo(Course other) {
        return this.name.compareTo(other.getName());
    }
}
class Student implements Comparable<Student>{
    private String ID;
    private String name;
    private double score;
    private int scoreCount=0;
    public boolean format = true;

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score += score;
        this.scoreCount++;
    }
    public int lastScore(){
        return (int)score/scoreCount;
    }
    // 定义比较规则:按照id升序排列
    @Override
    public int compareTo(Student o) {
        return Integer.compare(Integer.parseInt(this.ID), Integer.parseInt(o.ID));
    }
}
class Cls implements Comparable<Cls>{
    private String ID;
    private double score;
    private int scoreCount=0;
    public void setID(String ID){
        this.ID = ID;
    }
    public String getID(){
        return ID;
    }

    public void setScore(double score) {
        this.score += score;
        this.scoreCount++;
    }
    public double getScore(){
        return score;
    }

    public int getScoreCount() {
        return scoreCount;
    }
    // 定义比较规则:按照id升序排列
    @Override
    public int compareTo(Cls o) {
        return Integer.compare(Integer.parseInt(this.ID), Integer.parseInt(o.ID));
    }
}
class InputMatching {
    static String stuNumMatching = "[0-9]{8}";//8个0-9的数字
    static String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
    static String scoreMatching = "([1-9]?[0-9]|100)";
    static String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
    static String courseTypeMatching = "(选修|必修|实验)";
    static String checkCourseTypeMatching = "(考试|考察|实验)";

    static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching;
    /*courseInput用于定义课程信息模式(正则表达式)*/
    static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
            scoreMatching + "(scoreMatching)?";
    static String scoreInput2 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
            scoreMatching + "(scoreMatching)?" + " " + scoreMatching + "(scoreMatching)?";

    public static int matchingInput(String s) {
        if (matchingCourse(s)) {
            return 1;
        }
        if (matchingScore(s)) {
            return 2;
        }
        return 0;
    }
    private static boolean matchingCourse(String s) {
        return s.matches(courseInput);
    }
    private static boolean matchingScore(String s) {
        return s.matches(scoreInput)||s.matches(scoreInput2);
    }
}

 

三、主要困难及改进建议。

主要困难:
1. 输入合法性验证:代码中对输入的合法性进行了一些验证和错误处理。然而,这部分代码仍然比较冗长,可读性较差,很多重复的判断逻辑。这可能会导致代码的维护和扩展困难。
2. 代码重复:代码中存在一些重复的判断逻辑和重复的代码块,使得代码的冗余度较高,可读性较差,也增加了代码维护的难度。

改进的建议:
1. 提取公共方法:将输入合法性验证和错误处理的逻辑提取为独立的方法,以提高代码的可读性和可维护性。可以考虑设计一个输入处理类,将输入的合法性判断、错误处理和信息存储等功能封装在该类中,使得代码更加模块化和易于扩展。
2. 减少重复代码:通过提取公共方法、使用循环和条件语句等方式,减少代码中的重复逻辑和重复代码的存在。可以通过抽象出公共的判断和处理逻辑,将其封装成独立的方法或工具类,方便复用和维护。
3. 引入异常处理:对于输入合法性验证和错误处理,可以引入异常处理机制,使用自定义异常类来表示不合法的输入和错误情况,从而增加代码的可读性和可维护性。
4. 简化数据结构:通过优化数据结构的设计,减少不必要的属性和方法,可以提高代码的简洁性和可读性。比如,可以考虑使用Map来存储学生和课程信息,以简化代码的逻辑。

综上所述,改进的主要方向是简化代码逻辑、减少重复代码和提高代码的可读性和可维护性。通过引入更好的设计模式和编码规范,可以使代码更加简洁和易于理解。

四、总结

这两段代码都是一个学生成绩管理系统,能够录入课程信息和学生成绩,并进行统计分析和输出结果。

第一段代码主要采用面向对象的思想,通过类和对象对课程、学生和班级的信息进行封装和操作。代码中使用了正则表达式对输入的合法性进行验证,并进行相应的错误处理。然而,代码存在一些冗余和不必要的重复逻辑,可读性和可维护性有所欠缺。针对这些问题,建议提取公共方法、减少重复代码、引入异常处理机制,并简化数据结构的设计。

第二段代码在第一段代码的基础上进行了一些优化和调整。主要改进包括增加了"实验"作为课程类型的处理、对成绩进行合法性校验和错误处理、对输出结果进行排序等。然而,代码中仍然存在一些冗余和重复的逻辑,可读性和可维护性仍有改进的空间。建议进一步提取公共方法、减少重复代码,并通过异常处理、数据结构设计和代码简化等方面进行优化。

总的来说,这两段代码都是一个学生成绩管理系统的简单实现,存在一些改进的空间。通过优化代码逻辑、减少重复代码和提高代码可读性和可维护性,可以使代码更加简洁、易于理解和扩展。同时,根据实际需求,可以进一步优化输入处理、异常处理以及数据结构的设计等方面,以提升系统的稳定性和扩展性。

学到了:

1. 了解了一个学生成绩管理系统的基本功能和实现方式。
2. 熟悉了使用面向对象的思想进行代码设计和封装,以及使用类和对象管理数据。
3. 掌握了使用正则表达式对输入合法性进行验证的方法。
4. 学会了使用循环、条件语句和数组等基本编程技巧处理输入数据和进行相应的操作。
5. 理解了优化代码的重要性,如提取公共方法、减少重复代码、简化逻辑和数据结构等,以提高代码的可读性、可维护性和性能。
6. 意识到代码中存在的一些问题,如冗余逻辑、重复代码和不合理的数据结构设计。

需要进一步学习和研究的方面包括:

1. 异常处理:深入学习异常处理的原理和机制,在代码中灵活运用异常处理来提高代码的健壮性和可维护性。
2. 设计模式:学习和掌握常用的设计模式,以提高代码的可重用性、可扩展性和可维护性。
3. 大型项目架构和设计:了解大型项目的架构设计原则、模块划分、层次结构等,学习如何进行系统性的项目规划和架构设计。
4. 性能优化:进一步学习如何进行代码和程序的性能优化,减少资源的消耗,并提高系统的响应速度和稳定性。
5. 设计原则和编码规范:学习常用的设计原则和编码规范,如单一职责原则、开闭原则、代码重构等,用于指导和规范自己的代码编写。

 

posted on 2023-06-28 15:10  ultraman令  阅读(44)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3