首页 > 其他分享 >Blog3

Blog3

时间:2023-06-25 16:49:18浏览次数:35  
标签:String get dateThis Blog3 int new public

(1)前言:

  第7次PTA是沿袭之前的菜单题,这个题对之前的PTA题目集的完成度依赖很大,所以难度相较于成绩题更难一些。

第8次PTA是成绩的第一题,开始我想采用对象数组进行编程,发现使用起来特别不方便,后来学习了set和map,在之后的成绩题目中全部改用set和map,不仅使用更加方便,得到的分数也更多了。

第9次PTA是统计Java程序中关键词的出现次数,难点就在不同位置的关键字可能算数,可能不算数。

第10次PTA包括容器-HashMap-检索、容器-HashMap-排序、动物发声模拟器(多态)、课程成绩统计程序-2。前三个都比较简单,成绩的迭代还是有些难度,对之前的PTA题目集的完成度依赖很大,大体上已经完成了总体代码的设计和编写,只剩下细节的调整。

第11次PTA包括容器-ArrayList-排序、jmu-Java-02基本语法-03-身份证排序、jmu-Java-04面向对象进阶-03-接口-自定义接口ArrayIntegerStack、jmu-Java-03面向对象基础-05-覆盖、课程成绩统计程序-3。这次PTA体量还是可以的,也是我几次题目集里做的最好的,由于时间原因,还是没能满分。

(2)设计与分析:

这里我们主要围绕课程题目进行展开

1.首先来看一下第一次的课程题目源码:

import java.util.Scanner;

 

public class Main {

    static int i = 0;//课程数组的系数

    static int j = 0;//班级数组的系数

    static int k = 0;//学生数组的系数

    static String[] courseNameLine = new String[10];//课程名称字符串数组      用来判断课程是否已经创建

    static String[] stuClassNameLine = new String[10];//班级名称字符串数组      用来判断班级是否已经创建

    static String[] studentNameLine = new String[10];//班级名称字符串数组      用来判断班级是否已经创建

    static StuClass[] stuClass = new StuClass[10];//班级对象数组

    static Course[] course = new Course[10];//课程对象数组

    static Students[] students = new Students[10];//学生对象数组

    public static void main(String[] args) {

 

 

//        int courseNum = 0;//课程数量

        Scanner input = new Scanner(System.in);

        String date;

        while (true){

            date = input.nextLine();

            String dateThis[] = date.split(" ");

            if (date.equals("end")){

                break;

            }

            else{

                if(dateThis[1].equals("必修")||dateThis[1].equals("选修")){

                    if (dateThis[1].equals("必修")) {

                        if(dateThis[2].equals("考察")) {

                            System.out.println(dateThis[0] + " : course type & access mode mismatch");

                        }

                        else if(dateThis.length!=3||dateThis[2].equals("考试")){

                            if(!searchCourse(dateThis[0])) {

                                addCourse(dateThis[0]);

                                course[i-1] = new Course(dateThis[0],dateThis[1],dateThis[2]);

                            }

                            else {

                                continue;

                            }

                        }

                        else {

                            System.out.println("wrong format");

                        }

 

                    } else if ((dateThis[1].equals("选修")&&dateThis[2].equals("考察"))||(dateThis[1].equals("选修")&&dateThis[2].equals("考试"))) {

                        if(!searchCourse(dateThis[0])) {

                            addCourse(dateThis[0]);

                            course[i-1] = new Course(dateThis[0],dateThis[1],dateThis[2]);

                        }

                        else {

                            continue;

                        }

 

                    } else{

                        System.out.println("wrong format");

                    }

                }

                else if(dateThis[0].matches("\\d{8}")){

 

                    if(dateThis.length==5&&dateThis[3].matches("\\d{1,}")&&dateThis[4].matches("\\d{1,}")) {

                        if(!searchStuClass(dateThis[0].substring(0,6))){//班级不存在

                            addStuClass(dateThis[0].substring(0,6));

                            addStudents(dateThis[1]);

                            students[k-1] = new Students(dateThis[0], dateThis[1], dateThis[2], Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4]));

                            if(!searchCourse(dateThis[2])){

                                System.out.println(dateThis[0] +" " + dateThis[1] + " :" + dateThis[2] + " " + "does not exist");

                            }

                        }

                        if(searchStuClass(dateThis[0].substring(0,6))) {//班级存在

                            addStudents(dateThis[1]);

                            students[k-1] = new Students(dateThis[0], dateThis[1], dateThis[2], Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4]));

 

                            if(!searchCourse(dateThis[2])){

                                System.out.println(dateThis[0] +" " + dateThis[1] + " :" + dateThis[2] + " " + "does not exist");

                            }

                            getStuClass(dateThis[0].substring(0,6)).addStudent(new Students(dateThis[0], dateThis[1], dateThis[2], Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4])));

 

                        }

                    }

                    else if (dateThis.length==4&&dateThis[3].matches("\\d{1,}")) {

                        if(!searchStuClass(dateThis[0].substring(0,6))){//班级不存在

                            addStuClass(dateThis[0].substring(0,6));

                            addStudents(dateThis[1]);

                            students[k-1] = new Students(dateThis[0], dateThis[1], dateThis[2], Integer.valueOf(dateThis[3]));

                            getStuClass(dateThis[0].substring(0,6)).addStudent(new Students(dateThis[0], dateThis[1], dateThis[2], Integer.valueOf(dateThis[3])));

                            if(!searchCourse(dateThis[2])){

                                System.out.println(dateThis[0] +" " + dateThis[1] + " :" + dateThis[2] + " " + "does not exist");

                            }

                        }

                        if(searchStuClass(dateThis[0].substring(0,6))) {//班级存在

                            addStudents(dateThis[1]);

                            students[k-1] = new Students(dateThis[0], dateThis[1], dateThis[2], Integer.valueOf(dateThis[3]));

 

                            if(!searchCourse(dateThis[2])){

                                System.out.println(dateThis[0] +" " + dateThis[1] + " :" + dateThis[2] + " " + "does not exist");

                            }

                            getStuClass(dateThis[0].substring(0,6)).addStudent(new Students(dateThis[0], dateThis[1], dateThis[2], Integer.valueOf(dateThis[3])));

 

                        }

                    }

                    else{

                        System.out.println("wrong format");

                    }

 

                }

                else{

                    System.out.println("wrong format");

 

                }

            }

 

        }

        for(int a = 0;a<k;a++){

            System.out.println(students[a].getStudentNum() + " "+ students[a].StudentName + " " + (int)(students[a].getScore()));

        }

        for(int b = 0;b<i;b++){

            System.out.println(course[b].getCourseName() + " " + (int)course[b].getOrdAver() + " " + (int)course[b].getLastAver() + " " + (int)course[b].getAverScore());

        }

        for (int c = 0;c<j;c++){

            System.out.println(stuClass[c].getStuClassName() + " " + (int)stuClass[c].getAverScore());

        }

 

    }

    public static void addCourse(String course){//向字符串数组中加入课程名称,判断是否已经有课程信息

 

        courseNameLine[i] = course;

        i++;

    }

    public static boolean searchCourse(String course){//查找课程是否存在

        boolean flag = false;

        for(int a = 0;a < i;a++){

            if (courseNameLine[a].equals(course)) {

                flag = true;

                break;

            }

 

        }

        return flag;

    }

    public static void addStuClass(String stuClass){//向字符串数组中加入班级名称,判断是否已经有班级信息

 

        stuClassNameLine[j] = stuClass;

        j++;

    }

    public static boolean searchStuClass(String stuClass){//查找班级是否存在

        boolean flag = false;

        for(int b = 0;b <= j;b++){

            if (stuClassNameLine[b].equals(stuClass)) {

                flag = true;

                break;

            }

 

        }

        return flag;

    }

    public static void addStudents(String student){//向字符串数组中加入学生名称,判断是否已经有学生信息

 

        studentNameLine[k] = student;

        k++;

    }

    public static boolean searchStudents(String student){//查找xs是否存在

        boolean flag = false;

        for(int c = 0;c <= k;c++){

            if (studentNameLine[c].equals(student)) {

                flag = true;

                break;

            }

 

        }

        return flag;

    }

    public static StuClass getStuClass(String name){

        for (int d = 0;d<=j;d++){

            if(stuClass[d].getStuClassName().equals(name)){

                return stuClass[d];

            }

        }

        return stuClass[1];

    }

}

 

 

class StuClass {

    static int i = 0;//课程数组系数

    static int j = 0;//学生数组系数

    String StuClassName;

    Students[] students = new Students[10];

    Course[] course = new Course[10];

    StuClass[] stuClasses = new StuClass[10];

    double sum = 0;

    public StuClass(){}

    public StuClass(String StuClassName){

        this.StuClassName = StuClassName;

    }

    public void addClass(String className){

        stuClasses[i] = new StuClass(className);

        i++;

    }

 

    public String getStuClassName() {

        return StuClassName;

    }

 

    public void setStuClassName(String stuClassName) {

        StuClassName = stuClassName;

    }

 

    public void addStudent(String studentName){

        students[j] = new Students(studentName);

    }

    public void addStudent(Students students){

        this.students[j] = students;

    }

    public double getAverScore(){

        for(int a=0;a<j;a++){

            sum = sum + students[a].getScore();

        }

        return sum/(j+1);

    }

}

 

 

class Students {

    static int i = 0;

    String StudentNum;//学号

    String StudentName;//姓名

    Course[] courses = new Course[10];

    String StudentExam;//参加的考试科目

    int OrdScore;//平时成绩

    int lastScore;//期末成绩

    double score;//总成绩

//    OptionalCourse optionalCourse = new OptionalCourse();//学生选课

    public Students(){

 

    }

    public Students(String studentName){

        this.StudentName = studentName;

    }

    public Students(String StudentNum,String StudentName,String StudentExam,int OrdScore,int lastScore){

        this.StudentNum = StudentNum;

        this.StudentName = StudentName;

        this.StudentExam = StudentExam;

        this.OrdScore = OrdScore;

        this.lastScore = lastScore;

    }

    public Students(String StudentNum, String StudentName, String StudentExam, int lastScore){

        this.StudentNum = StudentNum;

        this.StudentName = StudentName;

        this.StudentExam = StudentExam;

        this.lastScore = lastScore;

    }

    public void setStudentNum(String studentNum) {

        StudentNum = studentNum;

    }

 

    public void setStudentName(String studentName) {

        StudentName = studentName;

    }

 

    public void setStudentExam(String studentExam) {

        StudentExam = studentExam;

    }

 

    public void setOrdScore(int ordScore) {

        OrdScore = ordScore;

    }

 

    public void setLastScore(int lastScore) {

        this.lastScore = lastScore;

    }

 

    public void setScore(double score) {

        this.score = score;

    }

 

 

    public String getStudentNum(){

        return StudentNum;

    }

    public String getStudentName(){

        return StudentName;

    }

    public String getStudentExam(){

        return StudentExam;

    }

 

    public int getOrdScore() {

        return OrdScore;

    }

 

    public int getLastScore() {

        return lastScore;

    }

 

    public double getScore(){//注意总成绩方法返回的是double型,使用时可强制类型转换

        score = 0.3 * OrdScore + 0.7 * lastScore;

        return score;

    }

    public void addCourse(Course course){//为学生丰富课程信息

        courses[i] = course;

        i++;

    }

    public int getAllAver(){//需要重点写

//        for(int a=0 ;a<i;i++){

//            courses[a].

//        }

        return 0;

    }

}

 

 

class Course {

    static int k = 0;//学生对象数组编号

    double OrdSum = 0;//平时成绩之和

    double lastSum = 0;//期末成绩之和

    double sum = 0;//总成绩之和

    String CourseName;//课程名称

    String CourseSize;//课程类型

    String way;//考查方式

    Students[] students = new Students[10];

    double AverScore;

    public Course(){}

//    public Course(Course course){

//         this.course = course;

//    }

    public Course(String CourseName,String CourseSize,String way){

        this.CourseName = CourseName;

        this.CourseSize = CourseSize;

        this.way = way;

 

    }

 

    public String getCourseName() {

        return CourseName;

    }

 

    public void setCourseName(String courseName) {

        CourseName = courseName;

    }

 

    public void addStudents(Students students){

        this.students[k] = students;

        k++;

    }

    public  double getOrdAver(){//获取该课程平时成绩平均分

        for(int i = 0;i <= k;i++){

            OrdSum = OrdSum + students[i].getOrdScore();

 

        }

        return OrdSum/k;

    }

 

    public double getLastAver() {//获取该课程期末成绩平均分

        for(int i = 0;i <= k;i++){

            lastSum = lastSum + students[i].getLastScore();

 

        }

        return lastSum/k;

    }

 

    public double getAverScore(){//获取该课程总成绩平均分

        for(int i = 0;i <= k;i++){

            sum = sum + students[i].getScore();

 

        }

        return sum/k;

    }

}

可以看到这次的源码是用数组写的,各种数据乱七八糟一堆,非常难更改,这也是影响之后迭代的重要因素。

  1. 接下来看第二次课程题目

import java.util.*;

 

public class Main {

    static HashMap<String, Course> courseHashMap = new HashMap<>();

    static HashMap<String, Students> studentsHashMap = new HashMap<>();

    static HashMap<String, StuClass> stuClassHashMap = new HashMap<>();

 

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String date;

        while (true) {

            date = input.nextLine();

            String[] dateThis = date.split(" ");

            if (date.equals("end")) {

                break;

            }

            else {

                if (dateThis[1].equals("必修") || dateThis[1].equals("选修") || dateThis[1].equals("实验")) {

                    if (dateThis[0].length() > 10) {

                        System.out.println("wrong format");

                    }

                    else if (dateThis[1].equals("必修") && (dateThis.length==2||dateThis.length==3)) {

                        if (dateThis.length==3) {

                            if(!dateThis[2].equals("考试")){

                                System.out.println(dateThis[0] + " : course type & access mode mismatch");

                            }

                            else {

                                if (!courseHashMap.containsKey(dateThis[0])) {

                                    courseHashMap.put(dateThis[0], new Course(dateThis[0], "必修", "考试"));

                                }

                            }

                        }

                        else {

                            if (!courseHashMap.containsKey(dateThis[0])) {

                                courseHashMap.put(dateThis[0], new Course(dateThis[0], "必修", "考试"));

                            }

                        }

                    }

                    else if (dateThis[1].equals("选修")&&dateThis.length==3) {

                        if(!dateThis[2].matches("考试|考察")){

                            System.out.println(dateThis[0] + " : course type & access mode mismatch");

                        }

                        else if (!courseHashMap.containsKey(dateThis[0])) {

                            courseHashMap.put(dateThis[0], new Course(dateThis[0], dateThis[1], dateThis[2]));

                        }

                    }

                    else if (dateThis[1].equals("实验")&&dateThis.length==3) {

                        if(!dateThis[2].equals("实验")){

                            System.out.println(dateThis[0] + " : course type & access mode mismatch");

                        }

                        else {

                            if (!courseHashMap.containsKey(dateThis[0])) {

                                courseHashMap.put(dateThis[0], new Course(dateThis[0], "实验", "实验"));

                            }

                        }

                    }

                    else

                        System.out.println("wrong format");

                }

                else if (dateThis[0].matches("\\d{8}")) {

                    if (!stuClassHashMap.containsKey(dateThis[0].substring(0, 6))) {//若班级不存在,存班级

                        stuClassHashMap.put(dateThis[0].substring(0, 6), new StuClass(dateThis[0].substring(0, 6)));

                    }

//

                    if(studentsHashMap.get(dateThis[0]).courseHashMap.containsKey(dateThis[3])){

                        continue;

                    }

                    if(!courseHashMap.isEmpty()) {

                        if (courseHashMap.get(dateThis[2]).CourseSize.matches("必修") && dateThis.length == 5) {//若输入不是两个成绩信息,无法处理,输出"wrong format"

 

                            if (!dateThis[3].matches("\\d{1,}") || !dateThis[4].matches("\\d{1,}")) {//输入的成绩数量和课程的考核方式不匹配

 

                                System.out.println(dateThis[0] + " " + dateThis[1] + " : " + "access mode mismatch");//20201103 张三 : access mode mismatch

                                studentsHashMap.put(dateThis[0], new Students(dateThis[0], dateThis[1], false));

                                courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], false));

                                stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], false));

                                continue;

                            } else {

                                if (Integer.valueOf(dateThis[3]) > 100 || Integer.valueOf(dateThis[3]) < 0 || Integer.valueOf(dateThis[4]) > 100 || Integer.valueOf(dateThis[4]) < 0) {

                                    System.out.println("wrong format");

                                    continue;

                                }

                                courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4])));

                                studentsHashMap.put(dateThis[0], new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4])));

                                stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4])));

 

                            }

                        } else if (courseHashMap.get(dateThis[2]).CourseSize.matches("选修")) {

                            if (Integer.valueOf(dateThis[3]) > 100 || Integer.valueOf(dateThis[3]) < 0) {

                                System.out.println("wrong format");

                            }

                            if (courseHashMap.get(dateThis[2]).way.matches("考试")) {

                                courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4])));

                                studentsHashMap.put(dateThis[1], new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4])));

                                stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4])));

 

                            } else if (courseHashMap.get(dateThis[2]).way.matches("考察")) {

                                courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), 101, Integer.valueOf(dateThis[3])));

                                studentsHashMap.put(dateThis[1], new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), 101, Integer.valueOf(dateThis[3])));

                                stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), 101, Integer.valueOf(dateThis[3])));

 

                            }

                        } else if (courseHashMap.get(dateThis[2]).CourseSize.matches("实验") && dateThis[3].matches("[4-9]")) {

                            if (Integer.valueOf(dateThis[3]) != dateThis.length - 4) {//成绩个数不匹配

                                System.out.println(dateThis[0] + " " + dateThis[1] + " : " + "access mode mismatch");//"20201103 张三 : access mode mismatch"

                                courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], false));

                                studentsHashMap.put(dateThis[1], new Students(dateThis[0], dateThis[1], false));

                                stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], false));

                            } else {

                                int sum = 0;

                                int aver = 0;

                                boolean flag = true;//成绩范围是否正确,true正确。

                                for (int a = 0; a < Integer.valueOf(dateThis[3]); a++) {

                                    sum = sum + Integer.valueOf(dateThis[4 + a]);

                                    aver = sum / Integer.valueOf(dateThis[3]);

                                    if (Integer.valueOf(dateThis[4 + a]) > 100 || Integer.valueOf(dateThis[4 + a]) < 0) {

                                        flag = false;

                                        courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], false));

                                        studentsHashMap.put(dateThis[1], new Students(dateThis[0], dateThis[1], false));

                                        stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], false));

                                        System.out.println("wrong format");

                                        break;

                                    }

 

                                }

                                if (flag) {

                                    courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), 101, aver));

                                    studentsHashMap.put(dateThis[1], new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), 101, aver));

                                    stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), 101, aver));

                                }

 

                            }

                        } else if (!courseHashMap.containsKey(dateThis[2])) {//课程不存在,输出

                            System.out.println(dateThis[0] + " " + dateThis[1] + " :" + dateThis[2] + " " + "does not exist");

                        } else {

                            System.out.println("wrong format");

                        }

                    }

                    else {

                        System.out.println("wrong format");

                        System.exit(0);

                    }

                }

                else {

                    System.out.println("wrong format");

 

                }

            }

        }

//        //学生:按顺序输出信息

//        List<Map.Entry<String,Students>> list1 = new ArrayList<>(studentsHashMap.entrySet());

//        list1.sort(new Comparator<Map.Entry<String, Students>>() {

//            @Override

//            public int compare(Map.Entry<String, Students> o1, Map.Entry<String, Students> o2) {

//                return o1.getKey().compareTo(o2.getKey());

//            }

//        });

//        for (Map.Entry<String, Students> stringStudentsEntry : list1) {

//            if (stringStudentsEntry.getValue().courseHashMap.size() == 0 || !stringStudentsEntry.getValue().isTure) {//如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"

//                System.out.println(stringStudentsEntry.getValue().StudentNum + " " + stringStudentsEntry.getValue().StudentName + " " + "did not take any exams");

//            } else

//                System.out.println(stringStudentsEntry.getKey() + " " + stringStudentsEntry.getValue().StudentName + " " + stringStudentsEntry.getValue().getAllAver());

//        }

 

//        //课程:按顺序输出信息

//        List<Map.Entry<String,Course>> list2 = new ArrayList<>(courseHashMap.entrySet());

//        list2.sort(new Comparator<Map.Entry<String, Course>>() {

//            @Override

//            public int compare(Map.Entry<String, Course> o1, Map.Entry<String, Course> o2) {

//                return o1.getKey().compareTo(o2.getKey());

//            }

//        });

//        for (Map.Entry<String, Course> stringCourseEntry : list2) {

//            if (stringCourseEntry.getValue().studentsHashMap.size() == 0 || stringCourseEntry.getValue().isHasTureInformation()==false) {//如果某门课程没有任何正确成绩信息,输出:课程名称+英文空格+"has no grades yet"

//                System.out.println(stringCourseEntry.getValue().CourseName + " " + "did not take any exams");

//            } else {

//                if (stringCourseEntry.getValue().CourseSize.equals("必修")) {

//                    System.out.println(stringCourseEntry.getKey() + " " + (int) stringCourseEntry.getValue().getOrdAver() + " " + (int) stringCourseEntry.getValue().getLastAver() + " " + (int) stringCourseEntry.getValue().getAverScore());

//                } else if (stringCourseEntry.getValue().CourseSize.equals("选修")) {

//                    System.out.println(stringCourseEntry.getKey() + " " + (int) stringCourseEntry.getValue().getLastAver() + " " + (int) stringCourseEntry.getValue().getAverScore());

//                } else if (stringCourseEntry.getValue().CourseSize.equals("实验")) {

//                    System.out.println(stringCourseEntry.getKey() + " " + (int) stringCourseEntry.getValue().getAverScore());

//                }

//            }

//        }

//        //班级:按顺序输出信息

//        List<Map.Entry<String,StuClass>> list3 = new ArrayList<>(stuClassHashMap.entrySet());

//        list3.sort(Comparator.comparing(Map.Entry::getKey));

//

//        for (Map.Entry<String, StuClass> stringStuClassEntry : list3) {

//            if (stringStuClassEntry.getValue().studentsHashMap.size() == 0 || !stringStuClassEntry.getValue().isHasTureInformation()) {

//                System.out.println(stringStuClassEntry.getValue().getStuClassName() + " " + "did not take any exams");

//            } else {

//                System.out.println(stringStuClassEntry.getValue().getStuClassName() + " " + (int) stringStuClassEntry.getValue().getAverScore());

//            }

//        }

 

 

        //学生:按顺序输出信息

        List<Map.Entry<String,Students>> list1 = new ArrayList<>(studentsHashMap.entrySet());

        list1.sort(new Comparator<Map.Entry<String, Students>>() {

            @Override

            public int compare(Map.Entry<String, Students> o1, Map.Entry<String, Students> o2) {

                return o1.getValue().StudentNum.compareTo(o2.getValue().StudentNum);

            }

        });

        for (int i=0;i<list1.size();i++){

            if(list1.get(i).getValue().courseHashMap.size()==0){//如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"

                System.out.println(list1.get(i).getValue().getStudentNum() + " " + list1.get(i).getValue().StudentName + " " + "did not take any exams");

            }

            else

                System.out.println(list1.get(i).getValue().getStudentNum() + " " + list1.get(i).getValue().StudentName + " " + (int)list1.get(i).getValue().getScore());

        }

 

 

        //课程:按顺序输出信息

        List<Map.Entry<String,Course>> list2 = new ArrayList<>(courseHashMap.entrySet());

        list2.sort(new Comparator<Map.Entry<String, Course>>() {

            @Override

            public int compare(Map.Entry<String, Course> o1, Map.Entry<String, Course> o2) {

                return o1.getKey().compareTo(o2.getKey());

            }

        });

        for (int j=0;j<list2.size();j++){

            if(list2.get(j).getValue().studentsHashMap.size()==0||!list2.get(j).getValue().isHasTureInformation()){//如果某门课程没有任何正确成绩信息,输出:课程名称+英文空格+"has no grades yet"

                System.out.println(list2.get(j).getValue().CourseName + " " + "has no grades yet");

            }

            else {

                if(list2.get(j).getValue().CourseSize.equals("必修")) {

                    System.out.println(list2.get(j).getValue().getCourseName() + " " + (int)list2.get(j).getValue().getOrdAver() + " " + (int)list2.get(j).getValue().getLastAver() + " " + (int) list2.get(j).getValue().getAverScore());

                }

                else if (list2.get(j).getValue().CourseSize.equals("选修")) {

                    System.out.println(list2.get(j).getValue().getCourseName() + " " + (int)list2.get(j).getValue().getLastAver() + " " + (int) list2.get(j).getValue().getAverScore());

                }

                else if (list2.get(j).getValue().CourseSize.equals("实验")) {

                    System.out.println(list2.get(j).getValue().getCourseName() + " " + (int) list2.get(j).getValue().getAverScore());

                }

            }

        }

        //班级:按顺序输出信息

        List<Map.Entry<String,StuClass>> list3 = new ArrayList<>(stuClassHashMap.entrySet());

        list3.sort(new Comparator<Map.Entry<String, StuClass>>() {

            @Override

            public int compare(Map.Entry<String, StuClass> o1, Map.Entry<String, StuClass> o2) {

                return o1.getKey().compareTo(o2.getKey());

            }

        });

 

        for (int k=0;k<list3.size();k++){

            if(list3.get(k).getValue().studentsHashMap.size()==0||!list3.get(k).getValue().isHasTureInformation()){

                System.out.println(list3.get(k).getValue().getStuClassName() + " " + "has no grades yet");

            }

            else {

                System.out.println(list3.get(k).getValue().getStuClassName() + " " + (int)list3.get(k).getValue().getAverScore());

            }

        }

    }

}

 

class StuClass {

    String StuClassName;

    HashMap<String,Students> studentsHashMap = new HashMap<>();//存学生

    double sum = 0;

    public StuClass(){}

    public StuClass(String StuClassName){

        this.StuClassName = StuClassName;

    }

 

    public String getStuClassName() {

        return StuClassName;

    }

 

    public void setStuClassName(String stuClassName) {

        StuClassName = stuClassName;

    }

 

    public void addStudent(Students students){

        studentsHashMap.put(students.StudentNum,students);

    }

 

    public double getAverScore(){

        for(Map.Entry<String,Students> entry : studentsHashMap.entrySet()){

            sum = sum + entry.getValue().getScore();

        }

        return sum/studentsHashMap.size();

    }

    public boolean isHasTureInformation(){//判断班级是否有有效学生信息

        for (Map.Entry<String,Students> entry : studentsHashMap.entrySet()){

            if(entry.getValue().isTure){

                return true;

            }

        }

        return false;

    }

}

 

class Students {

    static double sum = 0;

    String StudentNum;//学号

    String StudentName;//姓名

//    Course[] courses = new Course[10];

    static HashMap<Course,Integer> courseHashMap = new HashMap<>();//存课程和总成绩

    int OrdScore;//平时成绩

    int lastScore;//期末成绩

    double score;//总成绩

    boolean isTure = true;//学生成绩信息是否有误

 

    public Students(){

 

    }

    public Students(String StudentNum,String studentName,boolean isTure){//可能要改的地方,若学生有正确成绩信息,也有错误信息,无法处理;

        this.StudentNum = StudentNum;

        this.StudentName = studentName;

        this.isTure = isTure;

    }

    public Students(String StudentNum,String StudentName,Course course,int OrdScore,int lastScore){

        this.StudentNum = StudentNum;

        this.StudentName = StudentName;

        addCourse(course,OrdScore,lastScore);

        this.OrdScore = OrdScore;

        this.lastScore = lastScore;

 

    }

    public void setStudentNum(String studentNum) {

        StudentNum = studentNum;

    }

 

    public void setStudentName(String studentName) {

        StudentName = studentName;

    }

 

    public void setOrdScore(int ordScore) {

        OrdScore = ordScore;

    }

 

    public void setLastScore(int lastScore) {

        this.lastScore = lastScore;

    }

 

    public void setScore(double score) {

        this.score = score;

    }

 

 

    public String getStudentNum(){

        return StudentNum;

    }

    public String getStudentName(){

        return StudentName;

    }

 

    public int getOrdScore() {

        return OrdScore;

    }

 

    public int getLastScore() {

        return lastScore;

    }

 

    public double getScore(){//注意总成绩方法返回的是double型,使用时可强制类型转换

        score = 0.3 * OrdScore + 0.7 * lastScore;

        return score;

    }

    public void addCourse(Course course,int ordScore,int lastScore){//为学生丰富课程信息,包括成绩

        if(ordScore==101) {

            courseHashMap.put(course, lastScore);

            sum = sum + lastScore;

        }

        else {

            double endScore = (0.3 * OrdScore + 0.7 * lastScore);

            courseHashMap.put(course, (int)endScore);

            sum = sum + endScore;

        }

 

    }

 

    public int getAllAver(){//总成绩平均分

        return (int)(sum/courseHashMap.size()) ;

    }

}

 

class Course {

    static int k = 0;//学生对象数组编号

    double OrdSum = 0;//平时成绩之和

    double lastSum = 0;//期末成绩之和

    double sum = 0;//总成绩之和

    String CourseName;//课程名称

    String CourseSize;//课程类型

    String way;//考查方式

    Students[] students = new Students[10];

    HashMap<String,Students> studentsHashMap = new HashMap<>();//存学生

    double AverScore;

 

    public Course() {

    }

 

    public Course(String CourseName, String CourseSize, String way) {

        this.CourseName = CourseName;

        this.CourseSize = CourseSize;

        this.way = way;

 

    }

 

    public String getCourseName() {

        return CourseName;

    }

 

    public void setCourseName(String courseName) {

        CourseName = courseName;

    }

 

    public void addStudents(Students students) {

        studentsHashMap.put(students.StudentNum,students);

    }

 

    public double getOrdAver() {//获取该课程平时成绩平均分

        for (Map.Entry<String,Students> entry : studentsHashMap.entrySet()) {

            if(!(entry.getValue().getOrdScore()==101)){

                OrdSum = OrdSum + entry.getValue().getOrdScore();

            }

            else

                continue;

        }

        return OrdSum / studentsHashMap.size();

    }

 

    public double getLastAver() {//获取该课程期末成绩平均分

        for (Map.Entry<String,Students> entry : studentsHashMap.entrySet()) {

            lastSum = lastSum + entry.getValue().getLastScore();

 

        }

        return lastSum / studentsHashMap.size();

    }

 

    public double getAverScore() {//获取该课程总成绩平均分

        for (Map.Entry<String,Students> entry : studentsHashMap.entrySet()) {

            sum = sum +  entry.getValue().getScore();

 

        }

        return sum / studentsHashMap.size();

    }

 

    public boolean isHasTureInformation(){//判断课程是否有有效学生信息

        for (Map.Entry<String,Students> entry : studentsHashMap.entrySet()){

            if(entry.getValue().isTure){

                return true;

            }

        }

        return false;

    }

}

可以看到在这里我将数组全部换成了hashmap,这样既方便我们查找,也方便我们排序。

  1. 最后看第三次

import java.util.*;

 

public class Main {

    static HashMap<String, Course> courseHashMap = new HashMap<>();

    static HashMap<String, Students> studentsHashMap = new HashMap<>();

    static HashMap<String, StuClass> stuClassHashMap = new HashMap<>();

 

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String date;

        while (true) {

            date = input.nextLine();

            String[] dateThis = date.split(" ");

            if (date.equals("end")) {

                break;

            }

            else {

                if (dateThis[1].equals("必修") || dateThis[1].equals("选修") || dateThis[1].equals("实验")) {

                    if (dateThis[0].length() > 10) {

                        System.out.println("wrong format");

                        break;

                    }

 

                    if (dateThis[1].equals("必修")) {

                        if(!dateThis[2].equals("考试")){

                            System.out.println(dateThis[0] + " : course type & access mode mismatch");

                        }

                        else if (dateThis.length==5&&dateThis[3].matches("0.\\d{1,}")) {

                            if (!courseHashMap.containsKey(dateThis[0])) {

                                courseHashMap.put(dateThis[0], new Course(dateThis[0], "必修", "考试",2));

                                for(int i=0;i<2;i++){

                                    courseHashMap.get(dateThis[0]).courseScore.eachScore.weight[i] =  Integer.valueOf(dateThis[3+i]);

                                }

                            }

                        }

                        else {

                            System.out.println("wrong format");

                        }

                    }

                    else if (dateThis[1].equals("实验")) {

                        if(courseHashMap.containsKey(dateThis[0])){

                            continue;

                        }

                        if(!dateThis[2].equals("实验")){

                            System.out.println(dateThis[0] + " : course type & access mode mismatch");

                        }

                        else if(dateThis.length!=4 + Integer.valueOf(dateThis[3])){

                            System.out.println(dateThis[0] + " : number of scores does not match");

                        }

                        else {

                            if(Integer.valueOf(dateThis[3])>9||Integer.valueOf(dateThis[3])<4){

                                System.out.println("wrong format");

                            }

                            else {

                                double sum1 = 0;

                                for (int i = 0; i < Double.valueOf(dateThis[3]); i++) {

                                    sum1 = sum1 + Double.valueOf(dateThis[4 + i]);

                                }

                                if (sum1 != 1) {

                                    System.out.println(dateThis[0] + " : weight value error");

                                }

                                else if (!courseHashMap.containsKey(dateThis[0])) {

                                    courseHashMap.put(dateThis[0], new Course(dateThis[0], "实验", "实验", Integer.valueOf(dateThis[3])));

                                    for(int i =0;i< Integer.valueOf(dateThis[3]);i++) {

                                        courseHashMap.get(dateThis[0]).courseScore.eachScore.weight[i] = Double.valueOf(dateThis[4 + i]);

                                    }

                                }

                            }

                        }

                    }

 

                    else if (dateThis[1].equals("选修")) {

                        if(!dateThis[2].matches("考试|考察")){

                            System.out.println(dateThis[0] + " : course type & access mode mismatch");

                        }

                        else if (!courseHashMap.containsKey(dateThis[0])) {

                            if(dateThis[2].matches("考试")){

                                if (dateThis.length==5&&dateThis[3].matches("0.\\d{1,}")) {

                                    if (!courseHashMap.containsKey(dateThis[0])) {

                                        courseHashMap.put(dateThis[0], new Course(dateThis[0], "选修", "考试",2));

                                        for(int i=0;i<2;i++){

                                            courseHashMap.get(dateThis[0]).courseScore.eachScore.weight[i] =  Integer.valueOf(dateThis[3+i]);

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

                else if (dateThis[0].matches("\\d{8}")) {

                    if (!stuClassHashMap.containsKey(dateThis[0].substring(0, 6))) {//若班级不存在,存班级

                        stuClassHashMap.put(dateThis[0].substring(0, 6), new StuClass(dateThis[0].substring(0, 6)));

                    }

//

                    if(studentsHashMap.get(dateThis[0]).courseHashMap.containsKey(dateThis[2])){//学生个人课程信息已经包含该课程

                        continue;

                    }

                    if(!courseHashMap.isEmpty()) {

                        if (courseHashMap.get(dateThis[2]).CourseSize.matches("必修") && dateThis.length == 5) {//若输入不是两个成绩信息,无法处理,输出"wrong format"

 

                            if (!dateThis[3].matches("\\d{1,}") || !dateThis[4].matches("\\d{1,}")) {//输入的成绩数量和课程的考核方式不匹配

 

                                System.out.println(dateThis[0] + " " + dateThis[1] + " : " + "access mode mismatch");//20201103 张三 : access mode mismatch

                                studentsHashMap.put(dateThis[0], new Students(dateThis[0], dateThis[1], false));

                                courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], false));

                                stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], false));

                                continue;

                            } else {

                                if (Integer.valueOf(dateThis[3]) > 100 || Integer.valueOf(dateThis[3]) < 0 || Integer.valueOf(dateThis[4]) > 100 || Integer.valueOf(dateThis[4]) < 0) {

                                    System.out.println("wrong format");

                                    continue;

                                }

                                courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4])));

                                studentsHashMap.put(dateThis[0], new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4])));

                                stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4])));

 

                            }

                        } else if (courseHashMap.get(dateThis[2]).CourseSize.matches("选修")) {

                            if (Integer.valueOf(dateThis[3]) > 100 || Integer.valueOf(dateThis[3]) < 0) {

                                System.out.println("wrong format");

                            }

                            if (courseHashMap.get(dateThis[2]).way.matches("考试")) {

                                courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4])));

                                studentsHashMap.put(dateThis[1], new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4])));

                                stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), Integer.valueOf(dateThis[3]), Integer.valueOf(dateThis[4])));

 

                            } else if (courseHashMap.get(dateThis[2]).way.matches("考察")) {

                                courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), 101, Integer.valueOf(dateThis[3])));

                                studentsHashMap.put(dateThis[1], new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), 101, Integer.valueOf(dateThis[3])));

                                stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), 101, Integer.valueOf(dateThis[3])));

 

                            }

                        } else if (courseHashMap.get(dateThis[2]).CourseSize.matches("实验")) {

                            if (dateThis.length != courseHashMap.get(dateThis[2]).ScoreNum+3) {//成绩个数不匹配

                                System.out.println(dateThis[0] + " " + dateThis[1] + " : " + "access mode mismatch");//"20201103 张三 : access mode mismatch"

                                courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], false));

                                studentsHashMap.put(dateThis[1], new Students(dateThis[0], dateThis[1], false));

                                stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], false));

                            } else {

                                int aver = 0;

                                boolean flag = true;//成绩范围是否正确,true正确。

                                for (int a = 0; a < courseHashMap.get(dateThis[2]).ScoreNum; a++) {

                                    if (Integer.valueOf(dateThis[3 + a]) > 100 || Integer.valueOf(dateThis[3 + a]) < 0) {

                                        flag = false;

                                        courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], false));

                                        studentsHashMap.put(dateThis[1], new Students(dateThis[0], dateThis[1], false));

                                        stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], false));

                                        System.out.println("wrong format");

                                        break;

                                    }else

                                        courseHashMap.get(dateThis[2]).courseScore.eachScore.Score[a] = Integer.valueOf(dateThis[3 + a]);

 

                                }

                                aver = (int)courseHashMap.get(dateThis[2]).courseScore.getScore();

                                if (flag) {

                                    courseHashMap.get(dateThis[2]).addStudents(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), 101, aver));

                                    studentsHashMap.put(dateThis[1], new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), 101, aver));

                                    stuClassHashMap.get(dateThis[0].substring(0, 6)).addStudent(new Students(dateThis[0], dateThis[1], courseHashMap.get(dateThis[2]), 101, aver));

                                }

 

                            }

                        } else if (!courseHashMap.containsKey(dateThis[2])) {//课程不存在,输出

                            System.out.println(dateThis[0] + " " + dateThis[1] + " :" + dateThis[2] + " " + "does not exist");

                        } else {

                            System.out.println("wrong format");

                        }

                    }

                    else {

                        System.out.println("wrong format");

                        System.exit(0);

                    }

                }

                else {

                    System.out.println("wrong format");

 

                }

            }

        }

 

        //学生:按顺序输出信息

        List<Map.Entry<String,Students>> list1 = new ArrayList<>(studentsHashMap.entrySet());

        list1.sort(new Comparator<Map.Entry<String, Students>>() {

            @Override

            public int compare(Map.Entry<String, Students> o1, Map.Entry<String, Students> o2) {

                return o1.getValue().StudentNum.compareTo(o2.getValue().StudentNum);

            }

        });

        for (int i=0;i<list1.size();i++){

            if(list1.get(i).getValue().courseHashMap.size()==0){//如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"

                System.out.println(list1.get(i).getValue().getStudentNum() + " " + list1.get(i).getValue().StudentName + " " + "did not take any exams");

            }

            else

                System.out.println(list1.get(i).getValue().getStudentNum() + " " + list1.get(i).getValue().StudentName + " " + (int)list1.get(i).getValue().getScore());

        }

 

 

        //课程:按顺序输出信息

        List<Map.Entry<String,Course>> list2 = new ArrayList<>(courseHashMap.entrySet());

        list2.sort(new Comparator<Map.Entry<String, Course>>() {

            @Override

            public int compare(Map.Entry<String, Course> o1, Map.Entry<String, Course> o2) {

                return o1.getKey().compareTo(o2.getKey());

            }

        });

        for (int j=0;j<list2.size();j++){

            if(list2.get(j).getValue().studentsHashMap.size()==0||!list2.get(j).getValue().isHasTureInformation()){//如果某门课程没有任何正确成绩信息,输出:课程名称+英文空格+"has no grades yet"

                System.out.println(list2.get(j).getValue().CourseName + " " + "has no grades yet");

            }

            else {

                if(list2.get(j).getValue().CourseSize.equals("必修")) {

                    System.out.println(list2.get(j).getValue().getCourseName() + " " + (int)list2.get(j).getValue().getOrdAver() + " " + (int)list2.get(j).getValue().getLastAver() + " " + (int) list2.get(j).getValue().getAverScore());

                }

                else if (list2.get(j).getValue().CourseSize.equals("选修")) {

                    System.out.println(list2.get(j).getValue().getCourseName() + " " + (int)list2.get(j).getValue().getLastAver() + " " + (int) list2.get(j).getValue().getAverScore());

                }

                else if (list2.get(j).getValue().CourseSize.equals("实验")) {

                    System.out.println(list2.get(j).getValue().getCourseName() + " " + (int) list2.get(j).getValue().getAverScore());

                }

            }

        }

        //班级:按顺序输出信息

        List<Map.Entry<String,StuClass>> list3 = new ArrayList<>(stuClassHashMap.entrySet());

        list3.sort(new Comparator<Map.Entry<String, StuClass>>() {

            @Override

            public int compare(Map.Entry<String, StuClass> o1, Map.Entry<String, StuClass> o2) {

                return o1.getKey().compareTo(o2.getKey());

            }

        });

 

        for (int k=0;k<list3.size();k++){

            if(list3.get(k).getValue().studentsHashMap.size()==0||!list3.get(k).getValue().isHasTureInformation()){

                System.out.println(list3.get(k).getValue().getStuClassName() + " " + "has no grades yet");

            }

            else {

                System.out.println(list3.get(k).getValue().getStuClassName() + " " + (int)list3.get(k).getValue().getAverScore());

            }

        }

    }

}

 

class StuClass {

    String StuClassName;

    HashMap<String,Students> studentsHashMap = new HashMap<>();//存学生

    double sum = 0;

    public StuClass(){}

    public StuClass(String StuClassName){

        this.StuClassName = StuClassName;

    }

 

    public String getStuClassName() {

        return StuClassName;

    }

 

    public void setStuClassName(String stuClassName) {

        StuClassName = stuClassName;

    }

 

    public void addStudent(Students students){

        studentsHashMap.put(students.StudentNum,students);

    }

 

    public double getAverScore(){

        for(Map.Entry<String,Students> entry : studentsHashMap.entrySet()){

            sum = sum + entry.getValue().getScore();

        }

        return sum/studentsHashMap.size();

    }

    public boolean isHasTureInformation(){//判断班级是否有有效学生信息

        for (Map.Entry<String,Students> entry : studentsHashMap.entrySet()){

            if(entry.getValue().isTure){

                return true;

            }

        }

        return false;

    }

}

 

 

class Students {

    static double sum = 0;

    String StudentNum;//学号

    String StudentName;//姓名

    static HashMap<Course,Integer> courseHashMap = new HashMap<>();//存课程和总成绩

    int OrdScore;//平时成绩

    int lastScore;//期末成绩

    double score;//总成绩

    boolean isTure = true;//学生成绩信息是否有误,ture信息正确

 

 

    public Students(){

 

    }

    public Students(String StudentNum,String studentName,boolean isTure){//可能要改的地方,若学生有正确成绩信息,也有错误信息,无法处理;

        this.StudentNum = StudentNum;

        this.StudentName = studentName;

        this.isTure = isTure;

    }

    public Students(String StudentNum,String StudentName,Course course,int OrdScore,int lastScore){

        this.StudentNum = StudentNum;

        this.StudentName = StudentName;

        addCourse(course,OrdScore,lastScore);

        this.OrdScore = OrdScore;

        this.lastScore = lastScore;

 

    }

    public void setStudentNum(String studentNum) {

        StudentNum = studentNum;

    }

 

    public void setStudentName(String studentName) {

        StudentName = studentName;

    }

 

    public void setOrdScore(int ordScore) {

        OrdScore = ordScore;

    }

 

    public void setLastScore(int lastScore) {

        this.lastScore = lastScore;

    }

 

    public void setScore(double score) {

        this.score = score;

    }

 

 

    public String getStudentNum(){

        return StudentNum;

    }

    public String getStudentName(){

        return StudentName;

    }

 

    public int getOrdScore() {

        return OrdScore;

    }

 

    public int getLastScore() {

        return lastScore;

    }

 

    public double getScore(){//注意总成绩方法返回的是double型,使用时可强制类型转换

        if(OrdScore==101)

            score = lastScore;

        else

            score = 0.3 * OrdScore + 0.7 * lastScore;

        return score;

    }

    public void addCourse(Course course,int ordScore,int lastScore){//为学生丰富课程信息,包括成绩

        if(ordScore==101) {//考察或实验

            courseHashMap.put(course, lastScore);

            sum = sum + lastScore;

        }

        else {

            double endScore = (0.3 * OrdScore + 0.7 * lastScore);

            courseHashMap.put(course, (int)endScore);

            sum = sum + endScore;

        }

 

    }

 

    public int getAllAver(){//总成绩平均分

        return (int)(sum/courseHashMap.size()) ;

    }

    public boolean isHasTureInformation(){//判断班级是否有有效学生信息

        for (Map.Entry<Course,Integer> entry : courseHashMap.entrySet()){

            if(entry.getKey().isHasTureInformation()){

                return true;

            }

        }

        return false;

    }

}

 

 

class Course {

    static int k = 0;//学生对象数组编号

    double OrdSum = 0;//平时成绩之和

    double lastSum = 0;//期末成绩之和

    double sum = 0;//总成绩之和

    String CourseName;//课程名称

    String CourseSize;//课程类型

    String way;//考查方式

    int ScoreNum;

//    Students[] students = new Students[10];

    HashMap<String,Students> studentsHashMap = new HashMap<>();//存学生

    double AverScore;

    CourseScore courseScore;

    public Course() {

    }

 

    public Course(String CourseName, String CourseSize, String way,int ScoreNum) {//加上成绩个数

        this.CourseName = CourseName;

        this.CourseSize = CourseSize;

        this.way = way;

        this.ScoreNum = ScoreNum;

        courseScore = new CourseScore(ScoreNum);

    }

 

    public String getCourseName() {

        return CourseName;

    }

 

    public void setCourseName(String courseName) {

        CourseName = courseName;

    }

 

    public void addStudents(Students students) {

        studentsHashMap.put(students.StudentNum,students);

    }

 

    public double getOrdAver() {//获取该课程平时成绩平均分

        for (Map.Entry<String,Students> entry : studentsHashMap.entrySet()) {

            if(!(entry.getValue().getOrdScore()==101)){

                OrdSum = OrdSum + entry.getValue().getOrdScore();

            }

            else

                continue;

        }

        return OrdSum / studentsHashMap.size();

    }

 

    public double getLastAver() {//获取该课程期末成绩平均分

        for (Map.Entry<String,Students> entry : studentsHashMap.entrySet()) {

            lastSum = lastSum + entry.getValue().getLastScore();

 

        }

        return lastSum / studentsHashMap.size();

    }

 

    public double getAverScore() {//获取该课程总成绩平均分

        for (Map.Entry<String,Students> entry : studentsHashMap.entrySet()) {

            sum = sum +  entry.getValue().getScore();

 

        }

        return sum / studentsHashMap.size();

    }

 

    public boolean isHasTureInformation(){//判断课程是否有有效学生信息

        for (Map.Entry<String,Students> entry : studentsHashMap.entrySet()){

            if(entry.getValue().isTure){

                return true;

            }

        }

        return false;

    }

}

 

 

class CourseScore {

    int n;

    EachScore eachScore ;

    public CourseScore(int n){

        eachScore = new EachScore(n);

        this.n = n;

    }

    public double getScore(){//单门课程总成绩

        double sum = 0;

        for(int i=0;i<n;i++){

            sum = sum + eachScore.Score[i]*eachScore.weight[i];

        }

        return sum;

    }

}

 

class EachScore {

    int[] Score;//成绩分值;

    double[] weight;//成绩权重

    public EachScore(int n){

        Score = new int[n];

        weight = new double[n];

    }

}

这里主要是根据题目要求更改了不同测试方式的输入,比如实验的要输入每次的权重,考试的两次也有权重。

 

(3)采坑心得:

能不用对象数组就不要用,数据处理不方便。

代码要综合所学选取最好的方法,在编写过程中发现好方法不要怕改起来麻烦,改之后就方便了。

(4)改进建议:对相应题目的编码改进给出自己的见解,做到可持续改进

我在课程题目中没有对数据进行统一,比如班级中的学生,课程中的学生,学生的课程,都是分开存的数据,导致我的主函数存数据时代码很长。我希望可以实现数据的统一存储。

 

(5)总结:对本阶段(10-16周)综合性总结,学到了什么,哪些地方需要进一步学习及研究,对教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见。

单就知识而言,我认为我学到的最重要的就是set和map,极大的方便了我的编程。

课程的不足就是上课纪律不好尤其是翻转课堂的时候。

 

标签:String,get,dateThis,Blog3,int,new,public
From: https://www.cnblogs.com/qiyan22201330/p/17502944.html

相关文章

  • BLOG3
    一、前言通过一个学期的学习,我们终于结束了面向对象程序设计这门课,在过去的几周时间内,我们进行了实验、pta以及最后的期末考试,课程虽然结束了,但需要学习和巩固的知识点还有很多,这也是最后一次blog总结。      显而易见,这两次的PTA作业所考察的知识点以及难度呈现不......
  • MyBlog3:4月5日
    MyBlog3:4月5日LeetCode239滑动窗口最大值importjava.util.LinkedList;//leetcodesubmitregionbegin(Prohibitmodificationanddeletion)classSolution{publicint[]maxSlidingWindow(int[]nums,intk){if(k<1||nums==null||nums.length<......
  • blog3
    一、前言这几次的作业题量适中,主要重点封装、继承、多态、抽象类的使用、正则表达式,接口,还有一些和javafx相关的知识,主要题型就是看类图补全代码,其中电信计费系列是比较难......
  • Blog3:nchu-software-oop-2022-6~8
    Blog3:nchu-software-oop-2022-6~8一、前言不知不觉,已到了本学期末尾,面向对象程序设计这门课也已经结课。课程虽然结束了,但需要学习和巩固的知识点还有很多。同时,这也......
  • Remember的Java-blog3
    一、前言    此次blog为电信计费系列的座机,手机,短信大作业总结。相对于前几次的多边形,这次在算法上更简单,但是涉及到的内容很多,比如容器的使用、抛出异常、抽象类......
  • BLOG3
    一、前言1、知识点 类和对象:  类(class)和对象(object)是两种以计算机为载体的计算机语言的合称。对象是对客观事物的抽象,类是对对象的抽象。类是一种抽象......
  • Blog3-电信计费系列题目集
    前言电信计费系列题目虽然难度相对于多边形系列有所下降,但涉及知识点很广,主要如下:1、容器的使用2、抛出异常3、抽象类4、继承与多态5、正则表达式6、类和对象设计......