BLOG-3
一、前言
通过第六到八次题目集,让我对JAVA语言的应用能够更加熟练。第六次题目集只有一题,但难度偏难,考察的知识点的综合运用,需要花费比较多的精力才能拿到满分;第七次题目集题量适中,但是难度也适中,考察我们对容器-HashMap和多态的理解及应用,需要费些功夫方能拿到高分;第三次题目集题量适中,难度有高有低,更像是第七次题目集的Pro-max版本,也考察了新的知识点,让我们对面多态有了更深的理解,还考察了我们对接口和容器-ArrayList以及方法覆盖的理解及应用,突出自主学习能力。
二、设计与分析
(1)第六次题目集:
本次题目集难度比较高,本次题目集主要考察对Java语言知识的综合运用及掌握程度,以及通过相关的开发工具debug功能能够熟练的运用。
某高校课程从性质上分为:必修课、选修课,从考核方式上分为:考试、考察。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重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)成绩平均分只取整数部分,小数部分丢弃
参考类图:

输入样例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 张三 java 20
end
输出样例4:
在这里给出相应的输出。例如:
20201103 张三 : access mode mismatch
20201103 张三 did not take any exams
java has no grades yet
202011 has no grades yet
输入样例5:
单门课程,单个学生,课程类型与考核类型不匹配。例如:
java 必修 考察
20201103 张三 java 40
end
输出样例5:
在这里给出相应的输出。例如:
java : course type & access mode mismatch
java does not exist
20201103 张三 did not take any exams
202011 has no grades yet
输入样例6:
单门课程,多个学生。例如:
java 选修 考察
20201103 李四 java 60
20201104 王五 java 60
20201101 张三 java 40
end
输出样例6:
在这里给出相应的输出。例如:
20201101 张三 40
20201103 李四 60
20201104 王五 60
java 53 53
202011 53
输入样例7:
单门课程,单个学生,课程类型与考核类型不匹配。例如:
形式与政治 必修 考试
数据库 选修 考试
java 选修 考察
数据结构 选修 考察
20201103 李四 数据结构 70
20201103 李四 形式与政治 80 90
20201103 李四 java 60
20201103 李四 数据库 70 78
end
输出样例7:
在这里给出相应的输出。例如:
20201103 李四 73
java 60 60
数据结构 70 70
数据库 70 78 75
形式与政治 80 90 87
202011 73
输入样例8:
单门课程,单个学生,成绩越界。例如:
数据结构 选修 考察
20201103 李四 数据结构 101
end
输出样例8:
在这里给出相应的输出。例如:
wrong format
数据结构 has no grades yet
输入样例9:
多门课程,多个学生,多个成绩。例如:
形式与政治 必修 考试
数据库 选修 考试
java 选修 考察
数据结构 选修 考察
20201205 李四 数据结构 70
20201103 李四 形式与政治 80 90
20201102 王五 java 60
20201211 张三 数据库 70 78
end
输出样例9:
在这里给出相应的输出。例如:
20201102 王五 60
20201103 李四 87
20201205 李四 70
20201211 张三 75
java 60 60
数据结构 70 70
数据库 70 78 75
形式与政治 80 90 87
202011 73
202012 72
1 package pta6;
2
3 import java.util.ArrayList;
4 import java.util.Scanner;
5
6 public class stu {
7 public static void main(String[] args) {
8 Scanner s = new Scanner(System.in);
9 String s_record = s.nextLine();
10 ParseInput handle=new ParseInput();
11 while (!s_record.equals("end")) {
12 handle.parseInput(s_record);//解析用户输入的每一行数据
13 s_record = s.nextLine();
14 }
15 handle.showStudents();
16 handle.showCourses();
17 handle.showClasses();
18 }
19 }
20 class InputMatching {
21 static String stuNumMatching = "[0-9]{8}";//8个0-9的数字
22 static String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
23 static String scoreMatching = "([1-9]?[0-9]|100)";
24 static String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
25 static String courseTypeMatching = "(选修|必修)";
26 static String checkcourseTypeMatching = "(考试|考察)";
27 //courseInput用于定义课程信息模式(正则表达式)
28 static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkcourseTypeMatching;
29 //scoreInput用于定义成绩信息模式(正则表达式)
30 static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
31 scoreMatching + "(scoreMatching)?";
32 public static int matchingInput(String s) {
33 if (matchingCourse(s)) {
34 return 1;
35 }
36 if (matchingScore(s)) {
37 return 2;
38 }
39 return 0;
40 }
41
42 private static boolean matchingCourse(String s) {
43 return s.matches(courseInput);
44 }
45
46 private static boolean matchingScore(String s) {
47 //System.out.println(match);
48 return s.matches(scoreInput);
49 }
50 }
51 class ParseInput {
52
53 ArrayList<Course> list1 = new ArrayList<>();
54 ArrayList<Student> list2 = new ArrayList<>();
55 ArrayList list3 = new ArrayList<>();
56 ArrayList list4 = new ArrayList<>();
57 ArrayList list5 = new ArrayList<>();
58 public ParseInput() {
59 }
60 public ParseInput(ArrayList<Course> list1) {
61 this.list1 = list1;
62 }
63 public void parseInput(String sRecord) {
64 String[] s = sRecord.split(" ");
65 if (s[0].equals("形式与政治") || s[0].equals("java") || s[0].equals("数据结构") || s[0].equals("数据库")) {
66 Course course = new Course();
67 course.setMingCheng(s[0]);
68 for (int i = 1; i < s.length; i++) {
69 if (i == 1) {
70 course.setXingZhi(s[1]);
71 } else if (i == 2) {
72 course.setKaoHeFangShi(s[i]);
73 }
74 }
75 list1.add(course);
76 } else {
77 Student stu = new Student();
78 stu.setId(s[0]);
79 for (int i = 1; i < s.length; i++) {
80 if (i == 1) {
81 stu.setName(s[i]);
82 } else if (i == 2) {
83 stu.setCourse(s[i]);
84 } else if (i == 3) {
85 stu.setScore1(Integer.parseInt(s[i]));
86 } else if (i == 4) {
87 stu.setScore2(Integer.parseInt(s[i]));
88 }
89 }
90 list2.add(stu);
91 }
92 }
93 public void showStudents() {
94 ArrayList<Student> list = new ArrayList<>();
95 for (int i = 0; i < list2.size(); i++) {
96 if (list2.get(i).score2 == 0) {
97 list2.get(i).score3 = list2.get(i).score1;
98 } else {
99 list2.get(i).score3 = (int) (list2.get(i).score1 * 0.3 + list2.get(i).score2 * 0.7);
100 }
101 }
102 for (int i = 0; i < list2.size(); i++) {
103 Student stu = list2.get(i);
104 if (i == 0) {
105 list.add(stu);
106 } else {
107 for (int i1 = 0; i1 < list.size(); i1++) {
108 if (list.get(i1).getId().equals(stu.getId())) {
109 list.get(i1).score3 += stu.score3;
110 list.get(i1).i += 1;
111 break;
112 } else if (i1 == list.size() - 1) {
113 list.add(stu);
114 break;
115 }
116 }
117 }
118 }
119 int[] arr = new int[list.size()];
120 int[] arr2 = new int[list.size()];
121 int[] arr3 = new int[list.size()];
122
123 String[] ss = new String[list.size()];
124 for (int i = 0; i < list.size(); i++) {
125 ss[i] = list.get(i).id;
126 arr2[i] = Integer.parseInt(ss[i]);
127 arr3[i] = Integer.parseInt(ss[i]);
128
129 }
130 for (int i = 0; i < list.size(); i++) {
131 for (int j = 0; j < list.size() - 1 - i; j++) {
132 if (arr2[j] > arr2[j+1]) {
133 int temp = arr2[j];
134 arr2[j] = arr2[j + 1];
135 arr2[j + 1] = temp;
136 }
137 }
138 }
139 for (int i = 0; i < list.size(); i++) {
140 for (int j = 0; j < list.size(); j++) {
141 if (arr2[i] == arr3[j]) {
142 arr[i] = j;
143 break;
144 }
145 }
146 }
147
148 for (int i = 0; i < list.size(); i++) {
149 Student stu = list.get(arr[i]);
150 System.out.println(stu.id + " " + stu.name + " " + stu.score3 / stu.i);
151 list4.add(Integer.parseInt(stu.id)/100);
152 list5.add(stu.score3 / stu.i);
153 }
154 }
155 public void showCourses() {
156 for (int i = 0; i < 4; i++) {
157 if (i == 0) {
158 int score1 = 0, score2 = 0, score3;
159 int j = 0;
160 for (int i1 = 0; i1 < list2.size(); i1++) {
161 if (list2.get(i1).course.equals("java")) {
162 score1 += list2.get(i1).score1;
163 score2 += list2.get(i1).score2;
164 j += 1;
165 }
166 }
167 if (j != 0) {
168 if (score2 == 0) {
169 System.out.println("java " + score1 / j + " " + score1 / j);
170 } else {
171 System.out.println("java " + score1 / j + " " + score2 / j + " " + (int)((score1 / j) * 0.3 + (score2 / j) * 0.7));
172 }
173 }
174 } else if (i == 1) {
175 int score1 = 0, score2 = 0, score3;
176 int j = 0;
177 for (int i1 = 0; i1 < list2.size(); i1++) {
178 if (list2.get(i1).course.equals("数据结构")) {
179 score1 += list2.get(i1).score1;
180 score2 += list2.get(i1).score2;
181 j += 1;
182 }
183 }
184 if (j != 0) {
185 if (score2 == 0) {
186 System.out.println("数据结构 " + score1 / j + " " + score1 / j);
187 } else {
188 System.out.println("数据结构 " + score1 / j + " " + score2 / j + " " + (int)((score1 / j) * 0.3 + (score2 / j) * 0.7));
189 }
190 }
191 } else if (i == 2) {
192 int score1 = 0, score2 = 0, score3;
193 int j = 0;
194 for (int i1 = 0; i1 < list2.size(); i1++) {
195 if (list2.get(i1).course.equals("数据库")) {
196 score1 += list2.get(i1).score1;
197 score2 += list2.get(i1).score2;
198 j += 1;
199 }
200 }
201 if (j != 0) {
202 if (score2 == 0) {
203 System.out.println("数据库 " + score1 / j + " " + score1 / j);
204 } else {
205 System.out.println("数据库 " + score1 / j + " " + score2 / j + " " + (int)((score1 / j) * 0.3 + (score2 / j) * 0.7));
206 }
207 }
208 } else if (i == 3) {
209 int score1 = 0, score2 = 0, score3;
210 int j = 0;
211 for (int i1 = 0; i1 < list2.size(); i1++) {
212 if (list2.get(i1).course.equals("形式与政治")) {
213 score1 += list2.get(i1).score1;
214 score2 += list2.get(i1).score2;
215 j += 1;
216 }
217 }
218 if (j != 0) {
219 if (score2 == 0) {
220 System.out.println("形式与政治 " + score1 / j + " " + score1 / j);
221 } else {
222 System.out.println("形式与政治 " + score1 / j + " " + score2 / j + " " + (int)((score1 / j) * 0.3 + (score2 / j) * 0.7));
223 }
224 }
225 }
226 }
227 }
228 public void showClasses() {
229 for (int i = 0; i < list4.size(); i++) {
230 int o = 0;
231 if (i==0) {
232 list3.add(list4.get(i));
233 } else {
234 }
235
236 }
237 }
238 public ArrayList<Course> getList1() {
239 return list1;
240 }
241 public void setList1(ArrayList<Course> list1) {
242 this.list1 = list1;
243 }
244 }
245 class Course {
246 String mingCheng;
247 String xingZhi;
248 String kaoHeFangShi;
249 public Course() {
250 }
251 public Course(String mingCheng, String xingZhi, String kaoHeFangShi) {
252 this.mingCheng = mingCheng;
253 this.xingZhi = xingZhi;
254 this.kaoHeFangShi =