前言:6-8次PTA写了一份成绩统计程序,与之前的点菜系统有类似的地方,但比点菜系统更加的复杂,选课的时候需要考虑到学生与课程两个信息,涉及到字符的排序,与菜单不一样的地方在于会出现单词与汉字进行比较的情况,这就需要将汉字改成拼音再将拼音进行排序(在设计与分析中会详细描述)。这几次的成绩统计还存在错误提示的情况,不同的错误有不同的提示,这点比之前的作业要难。对我来说我在这方面花费了好多时间(设计与分析中详细描述),总的来说这三次作业假如第一次写的好了,后两次迭代不需要太长时间,要是没写好要大改代码(难在错误提示不明不白)。
设计与分析:
1.将汉字改成拼音再将拼音进行排序:
Collator collator = Collator.getInstance(Locale.CHINA);Collections.sort(courseGradeList, (o1, o2) -> collator.compare(o1.courseName, o2.courseName));
上述代码将能实现该功能。
2.在该题目中总共有三个排序(学生成绩排序,学科成绩排序,班级成绩排序)因为每个排序的计算方式存在差异(累加方式不同),所以我就单独开了三个类专门储存和计算这三种不同的成绩(StudentGrade,CourseGrade,Classroom),这三个类可以单独存储以及计算,现在想想可以将这三个类变成一个类中的三个方法,可以减少内存空间。
其中利用Map存储数据,我添加了一个类Choose_Course。
String number; String name; String Course_Name; int Grade1; int Grade2; int Full_Grade; int Flag ;
包含了学生的信息以及选课的信息,StudentGrade,CourseGrade,Classroom,中的计算价格都需要用到Choose_Course中的信息
其中计算班级分数的代码如下:
Map<String, Classroom> classrooms = new HashMap<>(); // 遍历Choose_courses,将学生按班级分组,并累计每个班级的总成绩和班级人数 for (Choose_course choose_course : grades.Choose_courses) { String number = choose_course.number; String classroomNum = number.substring(0, 6); Classroom classroom = classrooms.getOrDefault(classroomNum, new Classroom(classroomNum)); classroom.add(choose_course.Full_Grade); classrooms.put(classroomNum, classroom); } // 计算每个班级的平均分,并将平均分存储到班级对象中 List<Classroom> classroomList = new ArrayList<>(classrooms.values()); for (Classroom classroom : classroomList) { classroom.calculateAverage(); } // 按平均分排序, Collections.sort(classroomList, (c1, c2) -> c1.getClassroomNum().compareTo(c2.getClassroomNum())); for (Classroom classroom : classroomList) { if(classroom.getAverage()!=0){ System.out.println( classroom.getClassroomNum()+" "+classroom.getAverage());} if(classroom.getAverage()==0){ System.out.println( classroom.getClassroomNum()+" has no grades yet"); } }
在计算相同班级的成绩相加时需要注意学生考号前6位相同的为同一班级。存在两种情况有成绩和没成绩,有成绩的会输出成绩,没成绩的就会输出 has no grades yet。
3.
这个地方卡了好久,后来进行调试,发现自己的错误提示顺序不太对,当时没有考虑那么多,想的是将所有的错误全都统计起来一步步进行分类然后输出,后来室友提醒是程序输入将输入的东西一行一行读取,边读取边标记,与上次计价方式差不多,一个是最后进行折扣,一个是边折扣边相加。
4.第7次作业增加了实验,实验有实验的次数以及成绩,我是直接在主函数内进行了更改,在读取数据时就进行了区分:
if (parts[0].charAt(0) > 58) { int Flag = 0; if(parts[1].equals("实验")&&parts[2].equals("实验")&&parts[0].length()<= 10){ Flag = -2; String course_Name = parts[0]; String course_Nature = parts[1]; String course_Way = parts[2]; Course course = courses.addCourse(course_Name, course_Nature, course_Way, Flag); Course course_1 = courses.addCourse1(course_Name, course_Nature, course_Way, Flag); } if (parts[0].length() <= 10&&parts[1].equals("选修")&&(parts[2].equals("考试")||parts[2].equals("考察"))) { Flag = 1; String course_Name = parts[0]; String course_Nature = parts[1]; String course_Way = parts[2]; Course course = courses.addCourse(course_Name, course_Nature, course_Way, Flag); Course course_1 = courses.addCourse1(course_Name, course_Nature, course_Way, Flag); } if(parts[0].length()<=10&&parts[1].equals("必修")&&parts[2].equals("考试")){ Flag = 1; String course_Name = parts[0]; String course_Nature = parts[1]; String course_Way = parts[2]; Course course = courses.addCourse(course_Name, course_Nature, course_Way, Flag); Course course_1 = courses.addCourse1(course_Name, course_Nature, course_Way, Flag); } if(!parts[1].equals("实验")){ Flag = -3; if(parts[2].equals("实验")){ System.out.println(parts[0] + " : course type & access mode mismatch"); String course_Name = parts[0]; String course_Nature = parts[1]; String course_Way = parts[2]; Course course = courses.addCourse(course_Name, course_Nature, course_Way, Flag); Course course_1 = courses.addCourse1(course_Name, course_Nature, course_Way, Flag); } } if (parts[0].charAt(0) > 58 && parts.length == 3 && parts[1].equals("必修") && parts[2].equals("考察")) { Flag = -3; System.out.println(parts[0] + " : course type & access mode mismatch"); // System.out.println(parts[0] + " does not exist");//修改后的值 String course_Name = parts[0]; String course_Nature = parts[1]; String course_Way = parts[2]; Course course = courses.addCourse(course_Name, course_Nature, course_Way, Flag); Course course_1 = courses.addCourse1(course_Name, course_Nature, course_Way, Flag); }//不匹配 if(Flag==0){ System.out.println("wrong format"); String course_Name = parts[0]; String course_Nature = parts[1]; String course_Way = parts[2]; Course course = courses.addCourse(course_Name, course_Nature, course_Way, Flag); Course course_1 = courses.addCourse1(course_Name, course_Nature, course_Way, Flag); } }
我的思路是直接判断是否为正常的实验,是的就直接传入数据,不是就根据具体情况进行报错提示,其中的Flag为判断是否为正常输入,如果不是可以根据其来判断错误的情况。
5.Hashmap的用法:
HashMap<String,Class> 班级=new HashMap<>(); HashMap<String,Course> 课程=new HashMap<>(); HashMap<String,Student>学生=new HashMap<>();
Hashmap就相当于一个地图,地图上有你存储的东西,但是呢,每当你存储一个东西的时候,相应的也会存储进去一个key也就是索引,可以一键的找到你所需要的东西,我认为是非常的方便的。下面是Hashmap的一些基本用法:
Iterator <String> iterator=班级.keySet().iterator(); while (iterator.hasNext()){ String key=iterator.next(); 学生.putAll(班级.get(key).Stu);} Set set=学生.keySet(); Object []arr=set.toArray(); Arrays.sort(arr);
6.还可以用多态,多态真的可以很好的解决一些繁琐的东西,但是不能用多态而滥用的话就会使之变得很麻烦,所以也还是要看清楚怎么用,该不该用之后更好。
接下来是有关题目和具体代码:
题目如下:
7-1 课程成绩统计程序-1
分数 100
作者 蔡轲
单位 南昌航空大学
某高校课程从性质上分为:必修课、选修课,从考核方式上分为:考试、考察。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩0.3+期末成绩0.7。
考察的总成绩直接等于期末成绩
必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。
课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
课程性质输入项:必修、选修
考核方式输入选项:考试、考察
课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
以上信息的相关约束:
1)平时成绩和期末成绩的权重默认为0.3、0.7
2)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】
3)学号由8位数字组成
4)姓名不超过10个字符
5)课程名称不超过10个字符
6)不特别输入班级信息,班级号是学号的前6位。
2、输出:
输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。
为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。
1)学生课程总成绩平均分按学号由低到高排序输出
格式:学号+英文空格+姓名+英文空格+总成绩平均分
如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"
2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出
格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分
如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"
3)班级所有课程总成绩平均分按班级由低到高排序输出
格式:班级号+英文空格+总成绩平均分
如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"
异常情况:
1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"
以上两种情况如果同时出现,按第一种情况输出结果。
3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"
4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"
5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。
信息约束:
1)成绩平均分只取整数部分,小数部分丢弃
在这里给出一组输入。例如:
java 必修 考试 数据结构 选修 考试 形式与政治 选修 考察 end
输出样例1:
仅有课程。例如:
java has no grades yet 数据结构 has no grades yet 形式与政治 has no grades yet
输入样例2:
单门考试课程 单个学生。例如:
java 必修 考试 20201103 张三 java 20 40 end
输出样例2:
在这里给出相应的输出。例如:
20201103 张三 34 java 20 40 34 202011 34
输入样例3:
单门考察课程 单个学生。例如:
java 选修 考察 20201103 张三 java 40 end
输出样例3:
在这里给出相应的输出。例如:
20201103 张三 40 java 40 40 202011 40
输入样例4:
单门课程,单个学生,课程类型与考核类型不匹配。例如:
形式与政治 必修 考试 数据库 选修 考试 java 选修 考察 数据结构 选修 考察 20201103 李四 数据结构 70 20201103 李四 形式与政治 80 90 20201103 李四 java 60 20201103 李四 数据库 70 78 end
输出样例4:
在这里给出相应的输出。例如:
20201103 李四 73 java 60 60 数据结构 70 70 数据库 70 78 75 形式与政治 80 90 87 202011 73
其中还有许多的样例输入,这里就不一一赘述了。
我的源码如下:
import java.awt.color.ICC_ColorSpace; import java.text.Collator; import java.util.*; class Student{ String number,name; public Student(String number,String name){ this.name = name; this.number = number; } } class Students{ Map<String,Student>studentMap = new HashMap<>(); public Student addStudent(String studentNumber,String studentName){ Student student = new Student(studentNumber,studentName); studentMap.put(studentNumber,student); return student; } } class Classroom { private String classroomNum; private int total,count,average; public Classroom(String classroomNum) { this.classroomNum = classroomNum; } public String getClassroomNum() { return classroomNum; } public int getAverage() { return average; } public void add(int score) { total += score; count++; } public void calculateAverage() { average = (int)(total * 1.0 / count); } } class StudentGrade { String number; String name; private int total,count,average,Flag; public StudentGrade(String number ,String name,int Flag) { this.number = number; this.name = name; this.Flag = Flag; } public String getNumber() { return number; } public String getName() { return name; } public int getAverage() { return average; } public int getFlag() { return Flag; } public int getTotal() { return total; } public int getCount() { return count; } public void add(int grade) { total += grade; count++; } public void calculateAverage() { average = (int)(total * 1.0 / count); } } class CourseGrade { String courseName; private int count,Grade1,average1,Grade2,average2,Grade,average,Flag; public CourseGrade(String courseName) { this.courseName = courseName; } public int getAverage() { return average; } public int getAverage1() { return average1; } public int getAverage2() { return average2; } public int getFlag() { return Flag; } public void add(int grade1,int grade2,int grade,int Flag) { Grade1 +=grade1; Grade2 +=grade2; Grade +=grade; count++; this.Flag = Flag; } public void calculateAverage() { average1 = (int)(Grade1 * 1.0 / count); average2 = (int)(Grade2 * 1.0 / count); average = (int)(Grade * 1.0 / count); } } class Course{ String Course_Name; String Course_Nature; String Course_Way; int flag = 0; public Course(String Course_Name,String Course_Nature,String Course_Way,int Flag){ this.Course_Name = Course_Name; this.Course_Nature = Course_Nature; this.Course_Way = Course_Way; this.flag = Flag; } } class Courses{ Map<String,Course>courseMap = new HashMap<>(); List<Course>Course_s = new ArrayList<>(); public Course searchCourse(String Course_Name){ Course course = courseMap.get(Course_Name); course.flag = 1; return course; } public Course addCourse(String Course_Name,String Course_Nature,String Course_Way,int Flag){ Course course = new Course(Course_Name,Course_Nature,Course_Way,Flag); courseMap.put(Course_Name,course); return course; } public Course addCourse1(String Course_Name,String Course_Nature,String Course_Way,int Flag){ Course course = new Course(Course_Name,Course_Nature,Course_Way,Flag); Course_s.add(course); return course; } } class Choose_course{ String number; String name; String Course_Name; int Grade1; int Grade2; int Full_Grade; int Flag ; public Choose_course(String number,String name,String Course_Name,int Grade1,int Grade2,int Flag){ this.number = number; this.name = name; this.Course_Name = Course_Name; this.Grade1 = Grade1; this.Grade2 = Grade2; this.Flag = Flag; if(Grade2==0){ this.Full_Grade = Grade1 ; } if(Grade2!=0){ this.Full_Grade = (int)(Grade1*0.3 +Grade2*0.7); } } } class Grades{ List<Choose_course>Choose_courses = new ArrayList<>(); public Choose_course addChoose_course(String number,String name,String Course_Name,int Grade1,int Grade2,int Flag,Courses courses,Students students){ Choose_course choose_course = new Choose_course(number,name,Course_Name,Grade1,Grade2,Flag); Choose_courses.add(choose_course); return choose_course; } } public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); Students students = new Students(); Courses courses = new Courses(); Grades grades = new Grades(); while(scanner.hasNext()) { String line = scanner.nextLine(); if (line.equals("end")) { break; } String[] parts = line.split("\\s+"); if (parts[0].charAt(0) > 58) { if (parts[0].length() <= 10) { String course_Name = parts[0]; String course_Nature = parts[1]; String course_Way = parts[2]; int Flag = 0; Course course = courses.addCourse(course_Name, course_Nature, course_Way, Flag); Course course_1 = courses.addCourse1(course_Name, course_Nature, course_Way, Flag); } if (parts[0].length() > 10) { System.out.println("wrong format"); }//格式错误 if (parts[0].charAt(0) > 58 && parts.length == 3 && parts[1].equals("必修") && parts[2].equals("考察")) { System.out.println(parts[0] + " : course type & access mode mismatch"); System.out.println(parts[0] + " does not exist"); }//不匹配 } if (parts[0].charAt(0) < 58 && parts.length == 4) { if (parts[0].length() != 8 || parts[1].length() > 10 || parts[2].length() > 10 || Integer.parseInt(parts[3]) > 100 || Integer.parseInt(parts[3]) < 0) { System.out.println("wrong format");//格式错误 } } if (parts[0].charAt(0) < 58 && parts.length == 5) { if (parts[0].length() != 8 || parts[1].length() > 10 || parts[2].length() > 10 || Integer.parseInt(parts[3]) > 100 || Integer.parseInt(parts[3]) < 0 || Integer.parseInt(parts[4]) > 100 || Integer.parseInt(parts[4]) < 0) { System.out.println("wrong format");//格式错误 } } if (parts[0].charAt(0) < 58 && parts.length == 4 && Integer.parseInt(parts[3]) <= 100 && Integer.parseInt(parts[3]) >= 0) {//格式正确 String number = parts[0]; String name = parts[1]; String student_course_Name = parts[2]; int Grade1 = Integer.parseInt(parts[3]); int Grade2 = 0; int Flag = 1; students.addStudent(number, name); for (Course course_1 : courses.Course_s) { if (course_1.Course_Name.equals(student_course_Name) && course_1.Course_Nature.equals("选修") && course_1.Course_Way.equals("考察")) { Flag = 1; course_1.flag = 1;//完全正确的(分割成4份) Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, Grade1, Grade2, Flag, courses, students); } if (course_1.Course_Name.equals(student_course_Name) && course_1.Course_Way.equals("考试")) { Flag = 2; course_1.flag = 1;//成绩数目与考试方式不匹配 System.out.println(number + " " + name + " : access mode mismatch"); Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, 0, 0, Flag, courses, students); } if (course_1.Course_Name.equals(student_course_Name) && course_1.Course_Way.equals("考察") && course_1.Course_Nature.equals("必修")) { Flag = 3; course_1.flag = 1;//错误考核方式与考试不对应 Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, 0, 0, Flag, courses, students); } } } if (parts[0].charAt(0) < 58 && parts.length == 5 && Integer.parseInt(parts[3]) <= 100 && Integer.parseInt(parts[3]) >= 0 && Integer.parseInt(parts[4]) <= 100 && Integer.parseInt(parts[4]) >= 0) { String number = parts[0]; String name = parts[1]; String student_course_Name = parts[2]; int Flag = 1; int Grade1 = Integer.parseInt(parts[3]); int Grade2 = Integer.parseInt(parts[4]); students.addStudent(number, name); for (Course course_1 : courses.Course_s) { if (course_1.Course_Name.equals(student_course_Name) && course_1.Course_Way.equals("考试")) { Flag = 1; course_1.flag = 1;//完全正确的(分割成5份) Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, Grade1, Grade2, Flag, courses, students); } if (course_1.Course_Name.equals(student_course_Name) && course_1.Course_Way.equals("考察") && course_1.Course_Nature.equals("选修")) { Flag = 2; course_1.flag = 1;//成绩数目与考试方式不匹配 System.out.println(number + " " + name + " : access mode mismatch"); Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, 0, 0, Flag, courses, students); } if (course_1.Course_Name.equals(student_course_Name) && course_1.Course_Way.equals("考察") && course_1.Course_Nature.equals("必修")) { Flag = 3; course_1.flag = 1;//错误考核方式与考试不对应 Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, 0, 0, Flag, courses, students); } } } } //学生输出 Map<String,StudentGrade>studentGrades = new HashMap<>(); for (Choose_course choose_course : grades.Choose_courses) { int flag = choose_course.Flag; String number = choose_course.number; String name = choose_course.name; StudentGrade studentGrade = studentGrades.getOrDefault(number, new StudentGrade(number,name,flag)); studentGrade.add(choose_course.Full_Grade); studentGrades.put(number, studentGrade); } List<StudentGrade> studentGradeList = new ArrayList<>(studentGrades.values()); for (StudentGrade studentGrade : studentGradeList) { studentGrade.calculateAverage(); } Collections.sort(studentGradeList, (c1, c2) -> c1.number.compareTo(c2.number)); for (StudentGrade studentGrade : studentGradeList) { if(studentGrade.getFlag()==1&&studentGrade.getAverage()!=0) {System.out.println( studentGrade.getNumber()+" "+studentGrade.getName()+" "+studentGrade.getAverage());} if(studentGrade.getAverage()==0){System.out.println( studentGrade.getNumber()+" "+studentGrade.getName()+" did not take any exams");} } //课目成绩排序 Map<String,CourseGrade>courseGrades = new HashMap<>(); for (Choose_course choose_course : grades.Choose_courses) { String name = choose_course.Course_Name; CourseGrade courseGrade = courseGrades.getOrDefault(name, new CourseGrade(name)); courseGrade.add(choose_course.Grade1, choose_course.Grade2, choose_course.Full_Grade,choose_course.Flag); courseGrades.put(name, courseGrade); } List<CourseGrade> courseGradeList = new ArrayList<>(courseGrades.values()); for (CourseGrade courseGrade : courseGradeList) { courseGrade.calculateAverage(); } Collator collator = Collator.getInstance(Locale.CHINA); Collections.sort(courseGradeList, (o1, o2) -> collator.compare(o1.courseName, o2.courseName)); for (CourseGrade courseGrade : courseGradeList) { if(courseGrade.getAverage2()!=0&& courseGrade.getFlag()==1&&courseGrade.getAverage()!=0) { System.out.println(courseGrade.courseName+" "+courseGrade.getAverage1() + " " + courseGrade.getAverage2() + " " + courseGrade.getAverage()); } if(courseGrade.getAverage2()==0&&courseGrade.getFlag()==1&&courseGrade.getAverage()!=0) { System.out.println(courseGrade.courseName+" "+courseGrade.getAverage1() + " " + courseGrade.getAverage()); } if(courseGrade.getAverage()==0&&courseGrade.getFlag()!=3){ System.out.println(courseGrade.courseName+ " " + "has no grades yet"); } } for(Course course:courses.Course_s){ if(course.flag==0){ System.out.println(course.Course_Name+" "+"has no grades yet"); } } //求班级的平均分,输出班级 Map<String, Classroom> classrooms = new HashMap<>(); // 遍历Choose_courses,将学生按班级分组,并累计每个班级的总成绩和班级人数 for (Choose_course choose_course : grades.Choose_courses) { String number = choose_course.number; String classroomNum = number.substring(0, 6); Classroom classroom = classrooms.getOrDefault(classroomNum, new Classroom(classroomNum)); classroom.add(choose_course.Full_Grade); classrooms.put(classroomNum, classroom); } // 计算每个班级的平均分,并将平均分存储到班级对象中 List<Classroom> classroomList = new ArrayList<>(classrooms.values()); for (Classroom classroom : classroomList) { classroom.calculateAverage(); } // 按平均分排序, Collections.sort(classroomList, (c1, c2) -> c1.getClassroomNum().compareTo(c2.getClassroomNum())); for (Classroom classroom : classroomList) { if(classroom.getAverage()!=0){ System.out.println( classroom.getClassroomNum()+" "+classroom.getAverage());} if(classroom.getAverage()==0){ System.out.println( classroom.getClassroomNum()+" has no grades yet"); } } } }
和菜单的思路差不多先是将输入给分开然后再进行判断,还有许许多多类似的判断,其实都大同小异,这里便不再赘述。
然后呢分好类之后,就要根据不同的部分进行相应的操作,输入课程这块的话,需要先用split方法将字符串以空格分隔,然后将分好的存进一个数组当中,接着把成绩和分数存进已经new好的Hashmap课程数组和Hashmap学生数组当中,这就是课程的存储方法,接着就是像一个学生成绩的啊之类的操作。其实和这个都有所相似,细节不同而已。
然后将不匹配的成绩和课程的操作完成之后,删除的代码也补上,就到了最后的计算总平均分的环节了,这里要注意时间不同的课程的不同性质,对应的的计算分数方式也不同,所以还要进行区别课程性质的操作,然后根据学生,课程,数组的顺序,依次输出每个课程,每个学生的总平均分,就大功告成了。
-
成绩分析2:(题目过长就不再赘述了)
-
import java.awt.color.ICC_ColorSpace; import java.text.Collator; import java.util.*; class Student{ String number,name; public Student(String number,String name){ this.name = name; this.number = number; } } class Students{ Map<String,Student>studentMap = new HashMap<>(); public Student addStudent(String studentNumber,String studentName){ Student student = new Student(studentNumber,studentName); studentMap.put(studentNumber,student); return student; } } class Classroom { private String classroomNum; private int total,count,average; public Classroom(String classroomNum) { this.classroomNum = classroomNum; } public String getClassroomNum() { return classroomNum; } public int getAverage() { return average; } public void add(int score) { total += score; count++; } public void calculateAverage() { average = (int)(total * 1.0 / count); } } class StudentGrade { String number; String name; private int total,count,average,Flag; public StudentGrade(String number ,String name,int Flag) { this.number = number; this.name = name; this.Flag = Flag; } public String getNumber() { return number; } public String getName() { return name; } public int getAverage() { return average; } public int getFlag() { return Flag; } public int getTotal() { return total; } public int getCount() { return count; } public void add(int grade) { total += grade; count++; } public void calculateAverage() { average = (int)(total * 1.0 / count); } } class CourseGrade { String courseName; private int count,Grade1,average1,Grade2,average2,Grade,average,Flag; public CourseGrade(String courseName) { this.courseName = courseName; } public int getAverage() { return average; } public int getAverage1() { return average1; } public int getAverage2() { return average2; } public int getFlag() { return Flag; } public void add(int grade1,int grade2,int grade,int Flag) { if(Flag!=6){ Grade1 +=grade1; Grade2 +=grade2; Grade +=grade; count++;} this.Flag = Flag; } public void calculateAverage() { average1 = (int)(Grade1 * 1.0 / count); average2 = (int)(Grade2 * 1.0 / count); average = (int)(Grade * 1.0 / count); } } class Course{ String Course_Name; String Course_Nature; String Course_Way; int flag = 0; public Course(String Course_Name,String Course_Nature,String Course_Way,int Flag){ this.Course_Name = Course_Name; this.Course_Nature = Course_Nature; this.Course_Way = Course_Way; this.flag = Flag; } } class Courses{ Map<String,Course>courseMap = new HashMap<>(); List<Course>Course_s = new ArrayList<>(); public Course searchCourse(String Course_Name){ Course course = courseMap.get(Course_Name); course.flag = 1; return course; } public Course addCourse(String Course_Name,String Course_Nature,String Course_Way,int Flag){ Course course = new Course(Course_Name,Course_Nature,Course_Way,Flag); courseMap.put(Course_Name,course); return course; } public Course addCourse1(String Course_Name,String Course_Nature,String Course_Way,int Flag){ Course course = new Course(Course_Name,Course_Nature,Course_Way,Flag); Course_s.add(course); return course; } } class Choose_course{ String number; String name; String Course_Name; int Grade1; int Grade2; int Full_Grade; int Flag ; public Choose_course(String number,String name,String Course_Name,int Grade1,int Grade2,int Flag){ this.number = number; this.name = name; this.Course_Name = Course_Name; this.Grade1 = Grade1; this.Grade2 = Grade2; this.Flag = Flag; if(Grade2==0){ this.Full_Grade = Grade1 ; } if(Grade2!=0){ this.Full_Grade = (int)(Grade1*0.3 +Grade2*0.7); } } } class Grades{ List<Choose_course>Choose_courses = new ArrayList<>(); public Choose_course addChoose_course(String number,String name,String Course_Name,int Grade1,int Grade2,int Flag,Courses courses,Students students){ Choose_course choose_course = new Choose_course(number,name,Course_Name,Grade1,Grade2,Flag); Choose_courses.add(choose_course); return choose_course; } } public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); Students students = new Students(); Courses courses = new Courses(); Grades grades = new Grades(); int n=0; while(scanner.hasNext()) { String line = scanner.nextLine(); if (line.equals("end")) { break; } String[] parts = line.split("\\s+"); if (parts[0].charAt(0) > 58) { int Flag = 0; if(parts[1].equals("实验")&&parts[2].equals("实验")&&parts[0].length()<= 10){ Flag = -2; String course_Name = parts[0]; String course_Nature = parts[1]; String course_Way = parts[2]; Course course = courses.addCourse(course_Name, course_Nature, course_Way, Flag); Course course_1 = courses.addCourse1(course_Name, course_Nature, course_Way, Flag); } if (parts[0].length() <= 10&&parts[1].equals("选修")&&(parts[2].equals("考试")||parts[2].equals("考察"))) { Flag = 1; String course_Name = parts[0]; String course_Nature = parts[1]; String course_Way = parts[2]; Course course = courses.addCourse(course_Name, course_Nature, course_Way, Flag); Course course_1 = courses.addCourse1(course_Name, course_Nature, course_Way, Flag); } if(parts[0].length()<=10&&parts[1].equals("必修")&&parts[2].equals("考试")){ Flag = 1; String course_Name = parts[0]; String course_Nature = parts[1]; String course_Way = parts[2]; Course course = courses.addCourse(course_Name, course_Nature, course_Way, Flag); Course course_1 = courses.addCourse1(course_Name, course_Nature, course_Way, Flag); } if(!parts[1].equals("实验")){ Flag = -3; if(parts[2].equals("实验")){ System.out.println(parts[0] + " : course type & access mode mismatch"); String course_Name = parts[0]; String course_Nature = parts[1]; String course_Way = parts[2]; Course course = courses.addCourse(course_Name, course_Nature, course_Way, Flag); Course course_1 = courses.addCourse1(course_Name, course_Nature, course_Way, Flag); } } if (parts[0].charAt(0) > 58 && parts.length == 3 && parts[1].equals("必修") && parts[2].equals("考察")) { Flag = -3; System.out.println(parts[0] + " : course type & access mode mismatch"); // System.out.println(parts[0] + " does not exist");//修改后的值 String course_Name = parts[0]; String course_Nature = parts[1]; String course_Way = parts[2]; Course course = courses.addCourse(course_Name, course_Nature, course_Way, Flag); Course course_1 = courses.addCourse1(course_Name, course_Nature, course_Way, Flag); }//不匹配 if(Flag==0){ System.out.println("wrong format"); String course_Name = parts[0]; String course_Nature = parts[1]; String course_Way = parts[2]; Course course = courses.addCourse(course_Name, course_Nature, course_Way, Flag); Course course_1 = courses.addCourse1(course_Name, course_Nature, course_Way, Flag); } } if(parts[0].charAt(0)<58&&parts.length >5){ n = Integer.parseInt(parts[3]); String number = parts[0]; String name = parts[1]; String student_course_Name = parts[2]; int Flag = 1; int Flag1 = 0; students.addStudent(number, name); for(Course course_1 : courses.Course_s){ if(course_1.flag==-2&&course_1.Course_Name.equals(parts[2])) {//课程信息正确 if (n <= 9 && n >= 4){ if (parts.length == (n + 4)) { int Grade_zong = 0; Flag = 0; Flag1 = 0; for (int i = 0; i < n; i++) { if (Integer.parseInt(parts[4 + i]) >= 0 && Integer.parseInt(parts[4 + i]) <= 100) { Grade_zong += Integer.parseInt(parts[4 + i]); } if (Integer.parseInt(parts[4 + i]) > 100 || Integer.parseInt(parts[4 + i]) < 0) { Flag1 = -3; } } if (Flag != -3) { int Grade1 = Grade_zong / n; Flag = 4;//实验课完全正确的情况 Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, Grade1, 0, Flag, courses, students); } if (Flag == -3) { Flag = 6;//课程正确,学生信息错误(课程构建对象,学生不构建对象) Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, 0, 0, Flag, courses, students); } } if (parts.length != (n + 4)) {//学生成绩数目不匹配 int count = 0; Flag = 4;//课程正确,学生信息不正确(课程构建对象,学生构建对象) System.out.println(number + " " + name + " " + " : access mode mismatch"); for (int k = 0; k < (parts.length - 4); k++) { if (Integer.parseInt(parts[4 + k]) > 100 || Integer.parseInt(parts[4 + k]) < 0) { Flag1 = -3;//错误格式 // 课程正确,学生信息错误(课程构建对象,学生不构建对象) count++; Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, 0, 0, 6, courses, students); break; } } if (count == 0) { Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, 0, 0, 4, courses, students); } } } if(n > 9 || n < 4){ Flag1 = -3;//学生信息错误,但是课程信息正确(课程要建立对象,学生不构建对象) Choose_course choose_course = grades.addChoose_course(number,name,student_course_Name, 0, 0, 6, courses, students); } } if((course_1.flag==-3||course_1.flag==0)&&course_1.Course_Name.equals(parts[2])){ if(n>9||n<4){ Flag1 = -3; } } if((course_1.flag==-3||course_1.flag==0)&&course_1.Course_Name.equals(parts[2])){ for(int m=0;m<n;m++){ if(Integer.parseInt(parts[4+m])>100||Integer.parseInt(parts[4+m])<0){ Flag1 = -3; } } } if((course_1.flag==0||course_1.flag==-3)&&course_1.Course_Name.equals(parts[2])&&Flag1!=-3){//课程信息错误(课程不构建对象) System.out.println(student_course_Name+" "+"does not exist"); } } if(Flag1 ==-3){ System.out.println("wrong format"); } } if (parts[0].charAt(0) < 58 && parts.length == 4 && Integer.parseInt(parts[3]) <= 100 && Integer.parseInt(parts[3]) >= 0) {//格式正确 String number = parts[0]; String name = parts[1]; String student_course_Name = parts[2]; int Grade1 = Integer.parseInt(parts[3]); int Grade2 = 0; int Flag = 1; students.addStudent(number, name); for (Course course_1 : courses.Course_s) { if (course_1.Course_Name.equals(student_course_Name) && course_1.Course_Nature.equals("选修") && course_1.Course_Way.equals("考察")) { Flag = 1; course_1.flag = 1;//完全正确的(分割成4份) Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, Grade1, Grade2, Flag, courses, students); } if (course_1.Course_Name.equals(student_course_Name) && course_1.Course_Way.equals("考试")) { Flag = 2; course_1.flag = 1;//成绩数目与考试方式不匹配 System.out.println(number + " " + name + " : access mode mismatch"); Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, 0, 0, Flag, courses, students); } if (course_1.Course_Name.equals(student_course_Name) && course_1.Course_Way.equals("考察") && course_1.Course_Nature.equals("必修")) { Flag = 3; course_1.flag = 1;//错误考核方式与考试不对应 Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, 0, 0, Flag, courses, students); } } } if (parts[0].charAt(0) < 58 && parts.length == 5 && Integer.parseInt(parts[3]) <= 100 && Integer.parseInt(parts[3]) >= 0 && Integer.parseInt(parts[4]) <= 100 && Integer.parseInt(parts[4]) >= 0) { String number = parts[0]; String name = parts[1]; String student_course_Name = parts[2]; int Flag = 1; int Grade1 = Integer.parseInt(parts[3]); int Grade2 = Integer.parseInt(parts[4]); students.addStudent(number, name); for (Course course_1 : courses.Course_s) { if (course_1.Course_Name.equals(student_course_Name) && course_1.Course_Way.equals("考试")) { Flag = 1; course_1.flag = 1;//完全正确的(分割成5份) Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, Grade1, Grade2, Flag, courses, students); } if (course_1.Course_Name.equals(student_course_Name) && course_1.Course_Way.equals("考察") && course_1.Course_Nature.equals("选修")) { Flag = 2; course_1.flag = 1;//成绩数目与考试方式不匹配 System.out.println(number + " " + name + " : access mode mismatch"); Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, 0, 0, Flag, courses, students); } if (course_1.Course_Name.equals(student_course_Name) && course_1.Course_Way.equals("考察") && course_1.Course_Nature.equals("必修")) { Flag = 3; course_1.flag = 1;//错误考核方式与考试不对应 Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, 0, 0, Flag, courses, students); } } } if (parts[0].charAt(0) < 58 && parts.length == 4) { String number = parts[0]; String name = parts[1]; String student_course_Name = parts[2]; if (parts[0].length() != 8 || parts[1].length() > 10 || parts[2].length() > 10 || Integer.parseInt(parts[3]) > 100 || Integer.parseInt(parts[3]) < 0) { System.out.println("wrong format");//格式错误 for(Course course_1 : courses.Course_s){//课程成立,学生不成立 if((course_1.flag==-2||course_1.flag==1)&&parts[2].equals(course_1.Course_Name)){ Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, 0, 0, 6, courses, students); } } } } if (parts[0].charAt(0) < 58 && parts.length == 5) { String number = parts[0]; String name = parts[1]; String student_course_Name = parts[2]; if (parts[0].length() != 8 || parts[1].length() > 10 || parts[2].length() > 10 || Integer.parseInt(parts[3]) > 100 || Integer.parseInt(parts[3]) < 0 || Integer.parseInt(parts[4]) > 100 || Integer.parseInt(parts[4]) < 0) { System.out.println("wrong format");//格式错误 for(Course course_1 : courses.Course_s){//课程成立,学生不成立 if((course_1.flag==-2||course_1.flag==1)&&parts[2].equals(course_1.Course_Name)){ Choose_course choose_course = grades.addChoose_course(number, name, student_course_Name, 0, 0, 6, courses, students); } } } } } //学生输出 Map<String,StudentGrade>studentGrades = new HashMap<>(); for (Choose_course choose_course : grades.Choose_courses) { if(choose_course.Flag!=6) { int flag = choose_course.Flag; String number = choose_course.number; String name = choose_course.name; StudentGrade studentGrade = studentGrades.getOrDefault(number, new StudentGrade(number, name, flag)); studentGrade.add(choose_course.Full_Grade); studentGrades.put(number, studentGrade); } } List<StudentGrade> studentGradeList = new ArrayList<>(studentGrades.values()); for (StudentGrade studentGrade : studentGradeList) { studentGrade.calculateAverage(); } Collections.sort(studentGradeList, (c1, c2) -> c1.number.compareTo(c2.number)); for (StudentGrade studentGrade : studentGradeList) { if(studentGrade.getFlag()==1&&studentGrade.getAverage()!=0) {System.out.println( studentGrade.getNumber()+" "+studentGrade.getName()+" "+studentGrade.getAverage());} if(studentGrade.getFlag()==4&&studentGrade.getAverage()!=0) {System.out.println( studentGrade.getNumber()+" "+studentGrade.getName()+" "+studentGrade.getAverage());} if(studentGrade.getAverage()==0){System.out.println( studentGrade.getNumber()+" "+studentGrade.getName()+" did not take any exams");} } //课目成绩排序 Map<String,CourseGrade>courseGrades = new HashMap<>(); int count1 = 0;int count2 = 0; for (Choose_course choose_course : grades.Choose_courses) { if(choose_course.Flag==6){count1++;} if(choose_course.Flag!=6){count2++;} String name = choose_course.Course_Name; CourseGrade courseGrade = courseGrades.getOrDefault(name, new CourseGrade(name)); courseGrade.add(choose_course.Grade1, choose_course.Grade2, choose_course.Full_Grade, choose_course.Flag); courseGrades.put(name, courseGrade); } List<CourseGrade> courseGradeList = new ArrayList<>(courseGrades.values()); for (CourseGrade courseGrade : courseGradeList) { courseGrade.calculateAverage(); } Collator collator = Collator.getInstance(Locale.CHINA); Collections.sort(courseGradeList, (o1, o2) -> collator.compare(o1.courseName, o2.courseName)); for (CourseGrade courseGrade : courseGradeList) { if(courseGrade.getAverage2()!=0&& courseGrade.getFlag()==1&&courseGrade.getAverage()!=0) { System.out.println(courseGrade.courseName+" "+courseGrade.getAverage1() + " " + courseGrade.getAverage2() + " " + courseGrade.getAverage()); } if(courseGrade.getAverage2()==0&&courseGrade.getFlag()==1&&courseGrade.getAverage()!=0) { System.out.println(courseGrade.courseName+" "+courseGrade.getAverage1() + " " + courseGrade.getAverage()); } if(courseGrade.getAverage2()==0&&(courseGrade.getFlag()==4||courseGrade.getFlag()==6)&&courseGrade.getAverage()!=0) { System.out.println(courseGrade.courseName+" "+ courseGrade.getAverage()); } if(count2==0&&count1!=0){ System.out.println(courseGrade.courseName+ " " + "has no grades yet"); } } for(Course course:courses.Course_s){ if(course.flag==0){ System.out.println(course.Course_Name+" "+"has no grades yet"); } } //求班级的平均分,输出班级 Map<String, Classroom> classrooms = new HashMap<>(); // 遍历Choose_courses,将学生按班级分组,并累计每个班级的总成绩和班级人数 for (Choose_course choose_course : grades.Choose_courses) { if(choose_course.Flag!=6) { String number = choose_course.number; String classroomNum = number.substring(0, 6); Classroom classroom = classrooms.getOrDefault(classroomNum, new Classroom(classroomNum)); classroom.add(choose_course.Full_Grade); classrooms.put(classroomNum, classroom); } } // 计算每个班级的平均分,并将平均分存储到班级对象中 List<Classroom> classroomList = new ArrayList<>(classrooms.values()); for (Classroom classroom : classroomList) { classroom.calculateAverage(); } // 按平均分排序, Collections.sort(classroomList, (c1, c2) -> c1.getClassroomNum().compareTo(c2.getClassroomNum())); for (Classroom classroom : classroomList) { if(classroom.getAverage()!=0){ System.out.println( classroom.getClassroomNum()+" "+classroom.getAverage());} if(classroom.getAverage()==0){ System.out.println( classroom.getClassroomNum()+" has no grades yet"); } } } }
当时没有可虑好如何进行优化,导致代码过长,还有好多重复的代码。
-
成绩分析 3:(还是直接上代码)
import java.text.Collator; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int findanexit=0; int u; grade gra_gra=new grade(null); String five="^(\\d{8})\\s+([\\u4e00-\\u9fa5a-zA-Z0-9_]{1,10})\\s+([\\u4e00-\\u9fa5a-zA-Z0-9_]{1,10})\\s+([0-9]|[1-9][0-9]|100)\\s+([0-9]|[1-9][0-9]|100)$"; String four="^(\\d{8})\\s+([\\u4e00-\\u9fa5a-zA-Z0-9_]{1,10})\\s+([\\u4e00-\\u9fa5a-zA-Z0-9_]{1,10})\\s+([0-9]|[1-9][0-9]|100)$"; Chosecoure date = new Chosecoure(); int cont = 0; int sin=0; int run; while (in.hasNextLine()) { run=0; String a = in.nextLine().trim(); String[] b = a.split(" "); int length = b.length; int iq = 0; if (a.equals("end")) break; else { if (length == 3) { int su=0; float []s = new float[2]; s[0]=1; Course c = new Course(b[0],b[1],b[2],1,s); IfWeightRight(date, b, iq, su, c); } else if (length == 6) { int su=0; float []s = new float[2]; s[0]=Float.parseFloat(b[4]); s[1]=Float.parseFloat(b[5]); Course c = new Course(b[0], b[1], b[2],2,s); IfWeightRight(date, b, iq, su, c); } else if(length>6&&(b[2].equals("实验")||b[2].equals("考察")||b[2].equals("考试"))) { int su=1; float[] s = new float[length - 4]; for (int i = 0; i < length - 4; i++) { s[i] = Float.parseFloat(b[i+4]); } Course c = new Course(b[0], b[1], b[2], Integer.parseInt(b[3]), s); if (c.Right()) { if (c.IfMatch(su)) if(length - 4 == Integer.parseInt(b[3])){ ifWeightRight(date, b, iq, c); } else if(!c.IfMatch(su)){ System.out.println(b[0]+" : course type & access mode mismatch"); } else { System.out.println(b[0]+" : number of scores does not match"); } } else{ System.out.println("wrong format"); } } date.course.forEach(Course::decide_mark); for(Course e: date.course){ if(e.name.equals(b[2])) run=e.mark; } if ((length == 4 || length == 5)&&run!=3) { if (length == 4) { if (!a.matches(four)) { System.out.println("wrong format"); }else{ int []s = new int[9]; s[0]=-1; s[1]=Integer.parseInt(b[3]); Score sco = new Score(b[0], b[1],b[2],s); Student stu = new Student(b[0], b[1]); date.student.add(stu); date.grades.add(new grade(gra_gra.get_gradenumber(b[0]))); for(Course e: date.course) { if (e.name.equals(b[2]) && e.mark == 1) sin = 1; if(e.name.equals(b[2])) { sco.get_weight(e.weight); sco.get_size(e.size); } } if (sco.HaveThisCourse(b[2], date.course) && sco.NumberMatchMode(b[2], date.course)) { if (cont == 0&&sin==1) { sco.dailyScore = 0; sco.decide_course(date.course); date.score.add(sco); sco.dailyScore = -1; cont++; } for (Score e : date.score) { if (!e.number.equals(b[0]) || !e.name.equals(b[1]) || !e.course.name.equals(b[2]) || e.dailyScore != -1 || e.examScore != Integer.parseInt(b[3]) || sin != 1) { sco.dailyScore = 0; sco.decide_course(date.course); date.score.add(sco); findanexit = getFindanexit(findanexit, gra_gra, date, b); } break; } } else println1(date, b, sco); } } if (length == 5) { if (!a.matches(five)) { System.out.println("wrong format"); } else{ u=0; int []s=new int [9]; s[0]= Integer.parseInt(b[3]); s[1]= Integer.parseInt(b[4]); Score sco = new Score(b[0],b[1],b[2],s); Student stu = new Student(b[0], b[1]); date.student.add(stu); date.grades.add(new grade(gra_gra.get_gradenumber(b[0]))); for(Course e: date.course){ if(e.name.equals(b[2])&&e.mark==2) sin=2; if(e.name.equals(b[2])) { sco.get_weight(e.weight); sco.get_size(e.size); } } if (sco.HaveThisCourse(b[2], date.course) && sco.NumberMatchMode(b[2], date.course) && a.matches(five)&&sin==2)//判断数据是否合法 { if (cont == 0) { sco.decide_course(date.course); date.score.add(sco); cont++; } for (Score e : date.score) { if (e.number.equals(b[0]) && e.name.equals(b[1]) && e.course.name.equals(b[2]) && e.dailyScore == Integer.parseInt(b[3]) && e.examScore == Integer.parseInt(b[4])) { u=1; } } if(u == 0 && sco.HaveThisCourse(b[2], date.course) && sco.NumberMatchMode(b[2], date.course) && a.matches(five)) { date.score.add(sco); findanexit = getFindanexit(findanexit, gra_gra, date, b); } } else { println1(date, b, sco); } } } } if ((length > 5||run==3)&&!b[2].equals("实验")&&!b[2].equals("考察")&&!b[2].equals("考试")) { int []s = new int[9]; for(int i=0;i<9;i++) s[i]=-1; for(int i=0;i<length-3;i++) { s[i]=Integer. parseInt(b[i+3]); } Student stu = new Student(b[0], b[1]); Score sco = new Score(b[0], b[1], b[2],s); date.student.add(stu); date.grades.add(new grade(gra_gra.get_gradenumber(b[0]))); sco.get_length(length); for(Course e: date.course){ if(e.name.equals(b[2])&&e.mark==3) sin=3; if(e.name.equals(b[2])) { sco.get_weight(e.weight); sco.get_size(e.size); } } if (sco.HaveThisCourse(b[2], date.course) && sco.NumberMatchMode(b[2], date.course) && sco.IfRight()&&sin==3) { sco.decide_course(date.course); date.score.add(sco); findanexit = getFindanexit(findanexit, gra_gra, date, b); cont++; } else if (!sco.HaveThisCourse(b[2], date.course) && !sco.NumberMatchMode(b[2], date.course)&& sco.IfRight()) { System.out.println(b[2] + " does not exist"); } else if (!sco.HaveThisCourse(b[2], date.course)) { System.out.println(b[2] + " does not exist"); } else if (!sco.NumberMatchMode(b[2], date.course)&&sco.IfRight()) { System.out.println(b[0] + " " + b[1] + " : access mode mismatch"); } else if(!sco.IfRight()&&sco.HaveThisCourse(b[2], date.course)) { System.out.println("wrong format"); } } } } for (Student e : date.student) { Student.getStudenttotol(e, date.score); } int flag; int flag1; String num = "000"; Collections.sort(date.student); for (Student q : date.student) { flag = 0; if(!num.equals(q.number)) flag = 1; num = q.number; if(flag == 1) { if (q.JudgeEmpty()) System.out.println(q.number + " " + q.name + " " + (int)q.student_score); else System.out.println(q.number+" "+q.name+" "+"did not take any exams"); } } date.Course_and_Score(); Collections.sort(date.grades); Collections.sort(date.course); date.output(); date.grade_score(date.score); String num1="000"; for(grade e : date.grades){ flag1=0; if(!num1.equals(e.gradenumber)) { flag1=1; num1=e.gradenumber; } if(flag1==1) { if(e.size!=0) System.out.println(e.gradenumber+" "+ e.grade_score); } } } public static void println1(Chosecoure date, String[] b, Score sco) { if (!sco.HaveThisCourse(b[2], date.course) && !sco.NumberMatchMode(b[2], date.course)) { System.out.println(b[2] + " does not exist"); } else if (!sco.HaveThisCourse(b[2], date.course)) { System.out.println(b[2] + " does not exist"); } else if (!sco.NumberMatchMode(b[2], date.course)) { System.out.println(b[0] + " " + b[1] + " : access mode mismatch"); } } public static int getFindanexit(int findanexit, grade gra_gra, Chosecoure date, String[] b) { for (grade m : date.grades) { if (m.gradenumber.equals(gra_gra.get_gradenumber(b[0]))) { findanexit = 1; break; } else findanexit = 0; } if (findanexit == 0) date.grades.add(new grade(gra_gra.get_gradenumber(b[0]))); return findanexit; } public static void ifWeightRight(Chosecoure date, String[] b, int iq, Course c) { if (c.IfWeightEqual1()) { for (Course e : date.course) { if (e.name.equals(b[0])) { iq = 1; break; } else iq = 0; } if (iq == 0) date.course.add(c); } else{ System.out.println(b[0] + " : weight value error"); } } public static void IfWeightRight(Chosecoure date, String[] b, int iq, int a, Course c) { if (c.IfMatch(a)) { ifWeightRight(date, b, iq, c); } else { System.out.println(b[0]+" : course type & access mode mismatch"); } } } class Course implements Comparable<Course>{ String name,nature,assessment; int size,course_score=0,course_dailyscore=0,course_finalscore=0,found=0,mark=0; float []weight; float am=0; Course(String name, String nature, String assessment, int size, float[] s) { this.assessment=assessment; this.nature=nature; this.name=name; this.size=size; this.weight=s; } boolean IfMatch(int a) { return nature.equals("必修") && assessment.equals("考试") && a == 0 || nature.equals("实验") && assessment.equals("实验") && a == 1 || nature.equals("选修") && assessment.equals("考试") && a == 0 || nature.equals("选修") && assessment.equals("考察") && a == 0; } public int compareTo(Course o) { try { Comparator<Object> compator = Collator.getInstance(Locale.CHINA); if (compator.compare(this.name, o.name) < 0) { return -1; } else if (compator.compare(this.name, o.name) > 0) { return 1; } } catch (Exception ignored) { } return 0; } @Override public String toString() { return name; } void decide_mark(){ if(nature.equals("必修")||assessment.equals("考试")) this.mark=2; if(assessment.equals("考察")) this.mark=1; if(assessment.equals("实验")&&nature.equals("实验")) this.mark=3; } boolean IfWeightEqual1(){ for(int i=0;i<size;i++) { am+=weight[i]; } return Math.abs(am - 1) < 0.0001; } boolean Right(){ return size >= 4 && size <= 9; } } class Score{ String number,name; Course course = new Course(null,null,null,0,null); int dailyScore,examScore,flag=-1,size=0,mark=0,length; int[] Grade; float [] weight=new float[9]; void get_size(int size){ this.size=size; } void get_weight(float[] weight){ this.weight=weight; } void get_length(int length){ this.length=length; } Score(String number, String name , String coursename, int[] grade) { this.number=number; this.course.name=coursename; this.name=name; this.Grade=grade; for(int i=0;i<9;i++) { if(grade[i]==-1) this.Grade[i]=0; } } void decide_course(List<Course> course){ course.forEach((e)->{ if(e.name.equals(this.course.name)) { this.course=e; } }); } boolean IfRight(){ if(size<4||size>9) return false; for(int i=0;i<9;i++) { if(Grade[i]<0||Grade[i]>100) return false; } return true; } int getFinalScore() { float am=0; if(course.assessment.equals("考察")&&course.nature.equals("选修")) return examScore; else if(course.nature.equals("必修")&&!course.assessment.equals("实验")) { return (int)(examScore*weight[0]+dailyScore*weight[1]); } else if(course.assessment.equals("考试")&&course.nature.equals("选修")) { return (int)(examScore*weight[0]+dailyScore*weight[1]); } else if(course.assessment.equals("实验") && course.nature.equals("实验")){ for(int q=0;q<size;q++) { am+=Grade[q]*weight[q]; } return (int)(am); } else return 0; } boolean HaveThisCourse(String name,List<Course> course){ for (Course e : course) { if (name.equals(e.name)) { return true; } } return false; } boolean NumberMatchMode(String name,List<Course> course){ boolean b = true; for (Course e : course) { if(e.name.equals(name)) { b = (!e.assessment.equals("考察") || flag != 1 || mark == 2) && (!e.assessment.equals("考试") || flag != 0 || mark == 1) && (!e.nature.equals("必修") || flag != 0 || mark == 1) && (!e.nature.equals("实验") || length - 3 == size); break; } } return b; } } class Chosecoure { List<Score> score = new ArrayList<>(); List<Course> course = new ArrayList<>(); List<Student> student = new ArrayList<>(); List<grade> grades=new ArrayList<>(); grade gardes=new grade(null); int m = 0; void Course_and_Score() { for (Course f : course) { m = 0; for (Score e : score) { if (e.course.name.equals(f.name)) { f.course_score += e.getFinalScore(); f.course_dailyscore += e.dailyScore; f.course_finalscore += e.examScore; m++; } } if (m != 0) { f.course_score = f.course_score / m; f.course_finalscore = f.course_finalscore / m; f.course_dailyscore = f.course_dailyscore / m; } else { System.out.println(f.name + " has no grades yet"); f.found=1; } } } void grade_score(List<Score> e) { grades.forEach((q)->{ e.forEach((p)->{ if(q.gradenumber.equals(gardes.get_gradenumber(p.number))) { q.grade_score+=p.getFinalScore(); q.size++; } }); if(q.size != 0) q.grade_score/=q.size; if(q.size==0) System.out.println(q.gradenumber+" has no grades yet"); }); } void output() { course.forEach((e) -> { { if ((e.assessment.equals("考试") || e.nature.equals("必修"))&&e.found==0) System.out.println(e.name + " " + e.course_dailyscore + " " + e.course_finalscore + " " + e.course_score); if (e.assessment.equals("考察")&&e.found==0) { System.out.println(e.name + " " + e.course_finalscore + " " + e.course_finalscore); } if(e.assessment.equals("实验")&&e.nature.equals("实验")&&e.found==0) { System.out.println(e.name+ " " + e.course_score); } } }); } } class Student implements Comparable<Student>{ String number,name; float student_score = 0; int student_score_size=0; Student(String number,String name) { this.number=number; this.name=name; } boolean JudgeEmpty(){ return student_score_size != 0; } static void getStudenttotol(Student student, List<Score> e){ e.forEach((f)->{ if(f.number.equals(student.number)) { student.student_score +=f.getFinalScore(); student.student_score_size++; } }); if(student.student_score_size != 0) student.student_score = student.student_score / student.student_score_size; } public int compareTo(Student o) { return this.number.compareTo(o.number); } public String toString() { return number; } } class grade implements Comparable<grade>{ String gradenumber; grade(String gradenumber){ this.gradenumber=gradenumber; } int grade_score = 0,size=0; String get_gradenumber(String number){ this.gradenumber=number.substring(0,6); return gradenumber; } public int compareTo(grade o) { return this.gradenumber.compareTo(o.gradenumber); } public String toString() { return gradenumber; } }
这次没有按上次的代码思路写,因为如果是按之前的思路写会变得特别复杂。成绩分析3加了许许多多的内容比如说,实验课可以选择输入几个,但是必须相加等于1考试课也可以靠自己输入,并且可以选择输入的值,但是在其他的方面,还是和之前的差不多,所以我就,把之前的成绩录入的代码做了调整,其余的思路还是那样。
踩坑心得:
1.由于限制条件没用到正则表达式导致会有特殊情况没考虑到从而导致非零返回:
后来将代码改成正则表达式测试点就过了
2.还有一个难受的地方就是有的例子会出现多种错误,而错误提示便是难点,我让我的几个高分室友跑几个相同的测试点,跑出来得结果还是有差异的,所以我在后面改错误报错顺序就调了好久。
3.还有种情况就是输出结果和例子一样但多了一个回车就报错了,其他测试点也是多个回车但不会报错,后来改着改着就消失了,现在也不太清楚。
主要困难及改进建议
通过这最后三次的的大作业来看,我认为成绩分析系列题还是有难度的,这三次作业的话,每一次都特别的做的特别的时间之长,才勉强拿到分数
,差不多基本到了后面一道题基本要花一天甚至两天的时间才有可能写完,并且还可能得不到全部的分数,另外就是我觉得在一些算法之类的题,也有一些难度,这也可能是做题少的原因吧!然后就我个人代码改进建议的话,我觉得我的代码行数还是有一点多的,这其中可能就是有一些地方做的不太周到,还可以减少更多的if else嵌套句的循环之类的,另外一点就是要多多的写累,这样的话可以做到自己代码更加的可持续性和可维护性,希望在下次大作业的时候可能更好的提前着手思考写什么样的框架,因为框架就像是一座房子的基石,基石搭不好,再多的努力也是白费,所以一定要先想好具体的框架,这样可以使自己在之后的代码之路上走的更远更轻松。
总结
历经了两三天之久,终于终于终于来到了最后的总结环节,可把我累坏了,接下来就让我对这最后一次博客进行一个总结,首先,最后的博客是进行在最后的三次大作业的总结上的,也就是说,以后再也没有博客的总结了,如果让我比较这个博客和大作业的话,我觉得这个博客总结会比较更加的累,因为他需要打的字非常非常的多,需要了解一些新的东西,也非常非常非常的多,但是意义上来讲,这个博客要比大作业的意义更加的好。
1:在最后的几周的时间里,主要大作业上的话就是对成绩分析进行不断的改进和整合,然后还有实验课的话,就是对农夫过河不断的利用新的方法,比如说继承或者接口来改进之前的代码,让游戏都变得更加完善,后来又让我们用fx可视化,这些的话是自己在课余时间学到的东西,当然也不排除一直在学习通上听网课里面讲的这些重要的知识点,然后就是在上课的时候学到的东西上课,我觉得老师偏重于讲解怎样设计类画出类图理解清楚关系,而不是一味的只会让学生们写代码,注重提高学生们的思维通过课上的学习,我不再是就只会单纯的敲写代码,而是更多的会理解的要首先在一开始就要有一个清晰的框架,然后有了大框架之后去写代码,才会更加的流畅和顺利。
2:在一方面的话,如果是自己,哪一方面没有学习好,或者需要更加进一步探究的话,我认为我在继承组合和聚集以及依赖这几个关系当中模糊不清,不能理解他们的真正界限,我觉得这是我接下来需要改进和完善的地方,其他方面的话还是要多多自己下功夫才行,但是以后的路也要自己一个人走了,希望大家可以多花一点时间在java学习上。
3:随着敲代码的次数增加会发现自己的心越来越平静,之前会烦躁,但后来发现那样根本没用,于是心就逐渐平静,这是代码对我自己性格的影响吧。思维的方面做事情更加的明确了,知道每一步应该做啥,打游戏思路也更加清晰。
标签:题目,String,Course,int,PTA,course,parts,name From: https://www.cnblogs.com/mcgo/p/17498861.html