一、作业概述
前言:本阶段作业量是说不上大,不过很少有像第一次作业那么简单了。这阶段主打的是课程成绩统计程序,其它题目大都大同小异,大部分是针对排序的。课程成绩统计程序主要考查的是数据的增与查及其如何高效,还有类与类间的继承、组合和关联关系。除此之外,本阶段还考察了对Comparator类和Hashmap类的运用。下面是这阶段题目的详细分析。
二、源码分析
第七次作业:
本次作业是对第四次作业的与第六次作业不同的迭代。较之第四次作业,本次作业新增了特色菜与口味度及衍生出的其它错误。在编写程序时,空指针问题仍是我的主要问题,这可能也是大部分人的问题,下面我就不赘述了。
新增功能:
1、菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+口味类型+英文空格+基础价格+"T"
例如:麻婆豆腐 川菜 9 T
菜价的计算方法:
周一至周五 7折, 周末全价。
特色菜的口味类型:川菜、晋菜、浙菜
川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣;
晋菜增加酸度值,酸度0-4级;对应酸度水平为:不酸、微酸、稍酸、酸、很酸;
浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜;
例如:麻婆豆腐 川菜 9 T
输入订单记录时如果是特色菜,添加口味度(辣/酸/甜度)值,格式为:序号+英文空格+菜名+英文空格+口味度值+英文空格+份额+英文空格+份数
例如:1 麻婆豆腐 4 1 9
单条信息在处理时,如果口味度超过正常范围,输出"spicy/acidity/sweetness num out of range : "+口味度值,spicy/acidity/sweetness(辣度/酸度/甜度)根据菜品类型择一输出,例如:
acidity num out of range : 5
输出一桌的信息时,按辣、酸、甜度的顺序依次输出本桌菜各种口味的口味度水平,如果没有某个类型的菜,对应的口味(辣/酸/甜)度不输出,只输出已点的菜的口味度。口味度水平由口味度平均值确定,口味度平均值只综合对应口味菜系的菜计算,不做所有菜的平均。比如,某桌菜点了3份川菜,辣度分别是1、3、5;还有4份晋菜,酸度分别是,1、1、2、2,辣度平均值为3、酸度平均值四舍五入为2,甜度没有,不输出。
一桌信息的输出格式:table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格+"川菜"+数量+辣度+英文空格+"晋菜"+数量+酸度+英文空格+"浙菜"+数量+甜度。
如果整桌菜没有特色菜,则只输出table的基本信息,格式如下,注意最后加一个英文空格:
table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格
例如:table 1: 60 36 川菜 2 爆辣 浙菜 1 微甜
计算口味度时要累计本桌各类菜系所有记录的口味度总和(每条记录的口味度乘以菜的份数),再除以对应菜系菜的总份数,最后四舍五入。
注:本题要考虑代点菜的情况,当前桌点的菜要加上被其他桌代点的菜综合计算口味度平均值。
2、考虑客户订多桌菜的情况,输入时桌号时,增加用户的信息:
格式:table+英文空格+桌号+英文空格+":"+英文空格+客户姓名+英文空格+手机号+日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)
例如:table 1 : tom 13670008181 2023/5/1 21/30/00
约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。
输出结果时,先按要求输出每一桌的信息,最后按字母顺序依次输出每位客户需要支付的金额。不考虑各桌时间段的问题,同一个客户的所有table金额都要累加。
输出用户支付金额格式:
用户姓名+英文空格+手机号+英文空格+支付金额
3、最后按拼音顺序输出每位客户(不考虑客户同名或拼音相同的情况)的支付金额,格式: 用户姓名+英文空格+手机号+英文空格+支付总金额,按输入顺序排列。
1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.Calendar; 4 import java.util.Date; 5 import java.util.Scanner; 6 7 public class Main {//主类 8 public static void main(String[] args) { 9 Scanner input = new Scanner(System.in); 10 Menu menu = new Menu(); 11 Table[] tables = new Table[10]; 12 int[] index1 = new int[10]; 13 int[] index2 = new int[10]; 14 int[] index3 = new int[10]; 15 String[] temp0 = {"0", "0"}; 16 int cnt; 17 int num = 0; 18 int t = 0; 19 String[] spicylever = {"不辣", "微辣", "稍辣", "辣", "很辣", "爆辣"}; 20 String[] aciditylever = {"不酸", "微酸", "稍酸", "酸", "很酸"}; 21 String[] sweetnesslever = {"不甜", "微甜", "稍甜", "甜"}; 22 int[] month_max = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 23 while (true) { 24 int k = 0; 25 26 String inform = input.nextLine(); 27 String[] temp = inform.split(" "); 28 cnt = inform.length() - temp.length; 29 if (temp[0].equals("end")) { 30 break; 31 } 32 int length = temp.length; 33 if (length == 2) { 34 int i = 1; 35 int j = 0; 36 if (temp[1].equals("delete")) { 37 for (; i < tables[num].order.i ; i++) { 38 if (Integer.parseInt(temp[0]) == tables[num].order.records[i].orderNum) { 39 j = 1; 40 break; 41 } 42 } 43 44 if (temp[0].equals(temp0[0]) && temp0[1].equals("delete")) { 45 tables[num].order.delARecordByOrderNum(Integer.parseInt(temp[0])); 46 temp0 = temp; 47 continue; 48 49 } 50 51 if(j == 0){ 52 System.out.println("delete error"); 53 } 54 if(j == 1){ 55 tables[num].order.records[Integer.parseInt(temp[0])].num = 0; 56 if(tables[num].order.records[Integer.parseInt(temp[0])].d.dish == "川菜"){ 57 tables[num].index1 --; 58 tables[num].spicy -= tables[num].order.records[Integer.parseInt(temp[0])].lever; 59 } 60 if(tables[num].order.records[Integer.parseInt(temp[0])].d.dish == "晋菜"){ 61 tables[num].index2 --; 62 tables[num].acidity -= tables[num].order.records[Integer.parseInt(temp[0])].lever; 63 } 64 if(tables[num].order.records[Integer.parseInt(temp[0])].d.dish == "浙菜"){ 65 tables[num].index3 --; 66 tables[num].sweetness -= tables[num].order.records[Integer.parseInt(temp[0])].lever; 67 } 68 System.out.println(temp[0] + " delete"); 69 } 70 } else { 71 72 /*if (temp[1].matches("[0-9]+") && temp[1].charAt(0) != 0) { 73 if (temp0.length == 2 || temp0.length == 3) { 74 75 if (Integer.parseInt((temp[1])) <= 0 || Integer.parseInt(temp[1]) >= 300) { 76 System.out.println(temp[0] + " price out of range " + temp[1]); 77 }*/ 78 menu.addDish(temp[0], Integer.parseInt(temp[1])); 79 /* } else { 80 System.out.println("invalid dish"); 81 } 82 } else { 83 System.out.println("wrong format"); 84 } 85 86 }*/ 87 } 88 } 89 /* if (length == 3) { 90 91 92 }*/ 93 else if (length == 4) { 94 if (temp[3].equals("T")) { 95 /*if (temp[2].matches("[0-9]+") && temp[2].charAt(0) != 0) { 96 97 if (Integer.parseInt(temp[2]) <= 0 || Integer.parseInt(temp[2]) >= 300) { 98 System.out.println(temp[0] + " price out of range " + temp[2]); 99 }*/ 100 menu.addspecialDish(temp[0], Integer.parseInt(temp[2]), 'T', temp[1]); 101 /*} else { 102 System.out.println("wrong format"); 103 }*/ 104 } 105 /*if (temp[0].equals("table")) { 106 if (temp[1].matches("[0-9]+") && temp[2].matches("[\\d]{4}/(\\d|\\d\\d)/(\\d|\\d\\d)") && temp[3].matches("[\\d]{2}/[\\d]{2}/[\\d]{2}")) { 107 if (temp[1].charAt(0) != 0) { 108 num++; 109 tables[num] = new Table(); 110 tables[num].getDate(temp); 111 tables[num].getTime(temp); 112 if (tables[num].weekday >= 1 && tables[num].weekday <= 5) { 113 if (tables[num].hours >= 17 && tables[num].hours < 20 || tables[num].hours == 20 && tables[num].minutes <= 30 || tables[num].hours >= 11 && tables[num].hours < 14 || tables[num].hours == 14 && tables[num].minutes <= 30 || tables[num].hours == 10 && tables[num].minutes >= 30) { 114 115 } else { 116 System.out.println("table " + tables[num].tablenum + " out of opening hours"); 117 118 } 119 } 120 if (tables[num].weekday >= 6 && tables[num].weekday <= 7) { 121 if (tables[num].hours > 10 && tables[num].hours < 21 || tables[num].hours == 9 && tables[num].minutes >= 30 || tables[num].hours == 21 && tables[num].minutes <= 30) { 122 123 } else { 124 System.out.println("table " + tables[num].tablenum + " out of opening hours"); 125 } 126 } 127 if (tables[num].year >= 2022 && tables[num].year <= 2023 && tables[num].month >= 1 && tables[num].month <= 12 && tables[num].day > 0 && tables[num].day <= month_max[tables[num].month]) { 128 if (tables[num].tablenum < 1 || tables[num].tablenum > 55) { 129 System.out.println(temp[1] + " table num out of range"); 130 } else { 131 System.out.println(temp[0] + " " + temp[1] + ": "); 132 } 133 }*/ /*else { 134 System.out.println("not a valid time period"); 135 } 136 } 137 } else { 138 System.out.println("wrong format"); 139 k = 2; 140 break; 141 } 142 }*/ 143 else { 144 /*if (temp[2].matches("\\d") && temp[3].matches("[0-9]+") && temp[2].charAt(0) != 0 && temp[3].charAt(0) != 0) { 145 if (tables[num].order.i >= 2 && temp0[0].matches("[\\d]+") && Integer.parseInt(temp[0]) <= tables[num].order.records[tables[num].order.i-2].orderNum) { 146 System.out.println("record serial number sequence error"); 147 t = 1; 148 k = 1; 149 continue; 150 }*/ 151 if (menu.searthDish(temp[1]) == null) { 152 System.out.println(temp[1] + " does not exist"); 153 154 k = 1; 155 continue; 156 } 157 158 159 /* if (!temp[2].matches("1|2|3")) { 160 System.out.println(temp[0] + " portion out of range " + temp[2]); 161 k = 1; 162 t = 1; 163 continue; 164 } 165 166 if (Integer.parseInt(temp[3]) > 15) { 167 System.out.println(temp[0] + " num out of range " + temp[3]); 168 k = 1; 169 t = 1; 170 continue; 171 }*/ 172 tables[num].order.addARecord(Integer.parseInt(temp[0]), temp[1], Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), menu); 173 // if (t == 0) { 174 System.out.println(temp[0] + " " + temp[1] + " " + tables[num].order.findRecordByNum(Integer.parseInt(temp[0])).getPrice()); 175 /*} 176 } else { 177 System.out.println("wrong format"); 178 k = 1; 179 } 180 */ 181 } 182 } else if (length == 5) { 183 // if (temp[2].matches("\\d") && temp[3].matches("\\d") && temp[4].matches("[0-9]+") && temp[3].charAt(0) != 0 && temp[4].charAt(0) != 0) { 184 /*if (tables[num].order.i >= 2 && temp0[0].matches("[\\d]+") && Integer.parseInt(temp[0]) <= tables[num].order.records[tables[num].order.i-2].orderNum) { 185 System.out.println("record serial number sequence error"); 186 t = 1; 187 k = 1; 188 continue; 189 } 190 if (menu.searthDish(temp[1]) == null) { 191 System.out.println(temp[1] + " does not exist"); 192 t = 1; 193 k = 1; 194 continue; 195 } 196 197 198 if (!temp[3].matches("1|2|3")) { 199 System.out.println(temp[0] + " portion out of range " + temp[3]); 200 k = 1; 201 t = 1; 202 continue; 203 } 204 205 if (Integer.parseInt(temp[4]) > 15) { 206 System.out.println(temp[0] + " num out of range " + temp[4]); 207 k = 1; 208 t = 1; 209 continue; 210 }*/ 211 if (!temp[1].matches("\\d")) { 212 if (menu.searthDish(temp[1]) == null) { 213 System.out.println(temp[1] + " does not exist"); 214 215 k = 1; 216 continue; 217 } 218 219 switch (menu.searthDish(temp[1]).dish) { 220 case "川菜": 221 if (Integer.parseInt(temp[2]) < 0 || Integer.parseInt(temp[2]) > 5) { 222 System.out.println("spicy num out of range :" + temp[2]); 223 k = 1; 224 225 break; 226 } 227 index1[num] += Integer.parseInt(temp[4]); 228 tables[num].index1++; 229 tables[num].spicy += Integer.parseInt(temp[2]); 230 break; 231 case "晋菜": 232 if (Integer.parseInt(temp[2]) < 0 || Integer.parseInt(temp[2]) > 4) { 233 System.out.println("acidity num out of range :" + temp[2]); 234 k = 1; 235 break; 236 } 237 index2[num] += Integer.parseInt(temp[4]); 238 tables[num].index2++; 239 tables[num].acidity += Integer.parseInt(temp[2]); 240 break; 241 case "浙菜": 242 if (Integer.parseInt(temp[2]) < 0 || Integer.parseInt(temp[2]) > 3) { 243 System.out.println("sweetness num out of range :" + temp[2]); 244 k = 1; 245 break; 246 } 247 index3[num] += Integer.parseInt(temp[4]); 248 tables[num].index3++; 249 tables[num].sweetness += Integer.parseInt(temp[2]); 250 } 251 if (k == 0) { 252 253 tables[num].order.addASpecialRecord(Integer.parseInt(temp[0]), temp[1], Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), Integer.parseInt(temp[4]), menu); 254 /* if (t == 0) {*/ 255 System.out.println(temp[0] + " " + temp[1] + " " + tables[num].order.findRecordByNum(Integer.parseInt(temp[0])).getPrice()); 256 } else { 257 continue; 258 } 259 } 260 /*} else { 261 System.out.println("wrong format"); 262 k = 1; 263 }*/ 264 /*if(temp[1].matches("\\d")) { 265 if (!temp[0].matches("[\\d]+")) { 266 System.out.println("wrong format"); 267 continue; 268 } 269 if (temp[3].matches("\\d") && temp[4].matches("[0-9]+") && temp[3].charAt(0) != 0 && temp[4].charAt(0) != 0) { 270 if (temp0[0].matches("[\\d]+") && Integer.parseInt(temp[0]) <= Integer.parseInt(temp0[0])) { 271 System.out.println("record serial number sequence error"); 272 t = 1; 273 k = 1; 274 continue; 275 }*/ 276 else { 277 if (menu.searthDish(temp[2]) == null) { 278 System.out.println(temp[2] + " does not exist"); 279 280 k = 1; 281 continue; 282 } 283 284 285 /*if (!temp[3].matches("1|2|3")) { 286 System.out.println(temp[1] + " portion out of range " + temp[3]); 287 k = 1; 288 t = 1; 289 continue; 290 } 291 292 if (Integer.parseInt(temp[4]) > 15) { 293 System.out.println(temp[1] + " num out of range " + temp[4]); 294 k = 1; 295 t = 1; 296 continue; 297 }*/ 298 tables[num].order.addARecord(Integer.parseInt(temp[1]), temp[2], Integer.parseInt(temp[3]), Integer.parseInt(temp[4]), menu); 299 // if (t == 0) { 300 System.out.println(temp[1] + " table " + tables[num].tablenum + " pay for table " + temp[0] + " " + tables[num].order.findRecordByNum(Integer.parseInt(temp[0])).getPrice()); 301 }/*else { 302 System.out.println("wrong format"); 303 k = 1; 304 } 305 } else { 306 System.out.println("wrong format"); 307 }*/ 308 // } 309 } else if (length == 6) { 310 /*if (!temp[0].matches("[\\d]+")) { 311 System.out.println("wrong format"); 312 continue; 313 }*/ 314 // if (temp[3].matches("\\d") && temp[4].matches("\\d") && temp[5].matches("[0-9]+") && temp[4].charAt(0) != 0 && temp[5].charAt(0) != 0) { 315 /*if (temp0[0].matches("[\\d]+") && Integer.parseInt(temp[0]) <= Integer.parseInt(temp0[0])) { 316 System.out.println("record serial number sequence error"); 317 t = 1; 318 k = 1; 319 continue; 320 }*/ 321 if (menu.searthDish(temp[2]) == null) { 322 System.out.println(temp[2] + " does not exist"); 323 324 k = 1; 325 continue; 326 } 327 /* 328 329 if (!temp[4].matches("1|2|3")) { 330 System.out.println(temp[1] + " portion out of range " + temp[3]); 331 k = 1; 332 t = 1; 333 continue; 334 } 335 336 if (Integer.parseInt(temp[5]) > 15) { 337 System.out.println(temp[1] + " num out of range " + temp[5]); 338 k = 1; 339 t = 1; 340 continue; 341 }*/ 342 switch (menu.searthDish(temp[2]).dish) { 343 case "川菜": 344 if (Integer.parseInt(temp[3]) < 0 || Integer.parseInt(temp[3]) > 5) { 345 System.out.println("spicy num out of range :" + temp[3]); 346 k = 1; 347 break; 348 } 349 index1[Integer.parseInt(temp[0])] += Integer.parseInt(temp[5]); 350 break; 351 case "晋菜": 352 if (Integer.parseInt(temp[3]) < 0 || Integer.parseInt(temp[3]) > 4) { 353 System.out.println("acidity num out of range :" + temp[3]); 354 k = 1; 355 break; 356 } 357 index2[Integer.parseInt(temp[0])] += Integer.parseInt(temp[5]); 358 break; 359 case "浙菜": 360 if (Integer.parseInt(temp[3]) < 0 || Integer.parseInt(temp[3]) > 3) { 361 System.out.println("sweetness num out of range :" + temp[3]); 362 k = 1; 363 break; 364 } 365 index3[Integer.parseInt(temp[0])] += Integer.parseInt(temp[5]); 366 } 367 tables[num].order.addASpecialRecord(Integer.parseInt(temp[1]), temp[2], Integer.parseInt(temp[3]), Integer.parseInt(temp[4]), Integer.parseInt(temp[5]), menu); 368 // if (t == 0) { 369 System.out.println(temp[1] + " table " + tables[num].tablenum + " pay for table " + temp[0] + " " + tables[num].order.findRecordByNum(Integer.parseInt(temp[0])).getPrice()); 370 /*else { 371 System.out.println("wrong format"); 372 k = 1; 373 } 374 } else { 375 System.out.println("wrong format"); 376 }*/ 377 } else if (length == 7) { 378 // if (temp[0].equals("table")) { 379 if (temp[1].matches("[0-9]+") && temp[5].matches("[\\d]{4}/(\\d|\\d\\d)/(\\d|\\d\\d)") && temp[6].matches("(\\d|\\d\\d)/(\\d|\\d\\d)/(\\d|\\d\\d)")) { 380 //// if (temp[1].charAt(0) != 0) { 381 num++; 382 tables[num] = new Table(); 383 tables[num].getDate(temp); 384 tables[num].getTime(temp); 385 tables[num].name = temp[3]; 386 tables[num].phone = temp[4]; 387 if (temp[4].matches("(180|181|189|133|135|136)[0-9]{8}")) { 388 389 if (tables[num].weekday >= 1 && tables[num].weekday <= 5) { 390 if (tables[num].hours >= 17 && tables[num].hours < 20 || tables[num].hours == 20 && tables[num].minutes <= 30 || tables[num].hours >= 11 && tables[num].hours < 14 || tables[num].hours == 14 && tables[num].minutes <= 30 || tables[num].hours == 10 && tables[num].minutes >= 30) { 391 392 } else { 393 System.out.println("table " + tables[num].tablenum + " out of opening hours"); 394 num--; 395 break; 396 } 397 } 398 if (tables[num].weekday >= 6 && tables[num].weekday <= 7) { 399 if (tables[num].hours >= 10 && tables[num].hours < 21 || tables[num].hours == 9 && tables[num].minutes >= 30 || tables[num].hours == 21 && tables[num].minutes <= 30) { 400 401 } else { 402 num--; 403 System.out.println("table " + tables[num].tablenum + " out of opening hours"); 404 break; 405 } 406 } 407 408 System.out.println(temp[0] + " " + temp[1] + ": "); 409 } else { 410 num--; 411 System.out.println("wrong format"); 412 t = 1; 413 break; 414 415 } 416 } else { 417 System.out.println("wrong format"); 418 t = 1; 419 break; 420 } 421 /*if (tables[num].year >= 2022 && tables[num].year <= 2023 && tables[num].month >= 1 && tables[num].month <= 12 && tables[num].day > 0 && tables[num].day <= month_max[tables[num].month]) { 422 if (tables[num].tablenum < 1 || tables[num].tablenum > 55) { 423 System.out.println(temp[1] + " table num out of range"); 424 } else { 425 System.out.println(temp[0] + " " + temp[1] + ": "); 426 } 427 } else { 428 System.out.println("not a valid time period"); 429 } 430 } 431 } else { 432 System.out.println("wrong format"); 433 k = 2; 434 break; 435 }*/ 436 } 437 else { 438 System.out.println("wrong format"); 439 t = 1; 440 continue; 441 442 } 443 444 temp0 = temp; 445 446 } 447 if (t == 0) { 448 int[] sum = new int[10]; 449 450 for (int i = 1; i <= num; i++) { 451 // if (k == 1) { 452 // System.out.println("table " + tables[num].tablenum + ":" + " 0 0"); 453 // } 454 455 456 sum[i] = tables[i].order.getTotalPrice0(tables[i]); 457 if (tables[i].index1 == 0 && tables[i].index2 == 0 && tables[i].index3 == 0) { 458 System.out.println("table " + tables[i].tablenum + ":" + " " + sum[i] + " " + tables[i].order.getTotalPrice0(tables[i])); 459 460 } 461 if (tables[i].index1 != 0 && tables[i].index2 == 0 && tables[i].index3 == 0) { 462 System.out.println("table " + tables[i].tablenum + ":" + " " + tables[i].order.getTotalPrice() + " " + tables[i].order.getTotalPrice0(tables[i]) + " 川菜 " + index1[i] + " " + spicylever[(int)Math.round(tables[i].spicy * 1.0 / tables[i].index1)]); 463 464 } 465 if (tables[i].index1 == 0 && tables[i].index2 != 0 && tables[i].index3 == 0) { 466 System.out.println("table " + tables[i].tablenum + ":" + " " + tables[i].order.getTotalPrice() + " " + tables[i].order.getTotalPrice0(tables[i]) + " 晋菜 " + index2[i] + " " + aciditylever[(int)Math.round(tables[i].acidity * 1.0 / tables[i].index2)]); 467 468 } 469 if (tables[i].index1 == 0 && tables[i].index2 == 0 && tables[i].index3 != 0) { 470 System.out.println("table " + tables[i].tablenum + ":" + " " + tables[i].order.getTotalPrice() + " " + tables[i].order.getTotalPrice0(tables[i]) + " 浙菜 " + index3[i] + " " + sweetnesslever[(int)Math.round(tables[i].sweetness * 1.0 / tables[i].index3)]); 471 472 } 473 if (tables[i].index1 != 0 && tables[i].index2 != 0 && tables[i].index3 == 0) { 474 475 System.out.println("table " + tables[i].tablenum + ":" + " " + tables[i].order.getTotalPrice() + " " + tables[i].order.getTotalPrice0(tables[i]) + " 川菜 " + index1[i] + " " + spicylever[(int)Math.round(tables[i].spicy * 1.0 / tables[i].index1)] + " 晋菜 " + index2[i] + " " + aciditylever[(int)Math.round(tables[i].acidity * 1.0 / tables[i].index2)]); 476 477 } 478 if (tables[i].index1 != 0 && tables[i].index2 == 0 && tables[i].index3 != 0) { 479 System.out.println("table " + tables[i].tablenum + ":" + " " + tables[i].order.getTotalPrice() + " " + tables[i].order.getTotalPrice0(tables[i]) + " 川菜 " + index1[i] + " " + spicylever[(int)Math.round(tables[i].spicy * 1.0 / tables[i].index1)] + " 浙菜 " + index3[i] + " " + sweetnesslever[(int)Math.round(tables[i].sweetness * 1.0 / tables[i].index3)]); 480 481 } 482 if (tables[i].index1 == 0 && tables[i].index2 != 0 && tables[i].index3 != 0) { 483 484 System.out.println("table " + tables[i].tablenum + "nde:" + " " + tables[i].order.getTotalPrice() + " " + tables[i].order.getTotalPrice0(tables[i]) + " 晋菜 " + index2[i] + " " + aciditylever[(int) Math.round(tables[i].acidity * 1.0 / tables[i].index2)] + " 浙菜 " + index3[i] + " " + sweetnesslever[(int)Math.round(tables[i].sweetness * 1.0 / tables[i].index3)]); 485 486 } 487 if (tables[i].index1 != 0 && tables[i].index2 != 0 && tables[i].index3 != 0) { 488 489 System.out.println("table " + tables[i].tablenum + ":" + " " + tables[i].order.getTotalPrice() + " " + tables[i].order.getTotalPrice0(tables[i]) + " 川菜 " + index1[i] + " " + spicylever[(int)Math.round(tables[i].spicy * 1.0 / tables[i].index1)] + " 晋菜 " + (int)Math.round(tables[i].acidity * 1.0 / tables[i].index2) + " " + aciditylever[(int)Math.round(tables[i].acidity * 1.0 / tables[i].index2)] + " 浙菜 " + (int)Math.round(tables[i].sweetness * 1.0 / tables[i].index3) + " " + sweetnesslever[Math.round(tables[i].sweetness / tables[i].index3)]); 490 491 } 492 493 } 494 for (int i = 1; i <= num - 1; i++) { 495 Table table0 = new Table(); 496 for (int j = i + 1; j <= num; j++) { 497 if (tables[i].name.compareTo(tables[j].name) > 0) { 498 table0 = tables[i]; 499 tables[i] = tables[j]; 500 tables[j] = table0; 501 } 502 503 } 504 } 505 for (int i = 1; i <= num - 1; i++) { 506 for (int j = i + 1; j <= num; j++) { 507 if (tables[i].name.compareTo(tables[j].name) == 0) { 508 sum[i] += sum[j]; 509 tables[j].tablenum = 0; 510 511 } 512 } 513 } 514 for (int i = 1; i <= num; i++) { 515 if (tables[i].tablenum != 0) { 516 System.out.println(tables[i].name + " " + tables[i].phone + " " + sum[i]); 517 } 518 } 519 } 520 } 521 522 } 523 class Dish { 524 String name;//菜品名称 525 int unit_price = 0; //单价 526 char isSpecial;//是否为特色菜 527 String dish;//菜品 528 public Dish() { 529 530 // TODO 自动生成的构造函数存根 531 } 532 533 public Dish(String name, int unit_price) { 534 this.name = name; 535 this.unit_price = unit_price; 536 } 537 538 int getPrice(int portion) {//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) } 539 int price = 0; 540 if(portion == 1) 541 price = this.unit_price; 542 if(portion == 2) 543 price = (int) Math.round(this.unit_price * 1.5); 544 if(portion == 3) 545 price = this.unit_price * 2; 546 547 return price; 548 } 549 550 } 551 class Menu { 552 Dish[] dishes = new Dish[10];//菜品数组,保存所有菜品信息 553 int i = 0; 554 555 public Menu() { 556 557 // TODO 自动生成的构造函数存根 558 } 559 560 public Menu(Dish[] dishes) { 561 562 this.dishes = dishes; 563 } 564 565 Dish searthDish(String dishName) {//根据菜名在菜谱中查找菜品信息,返回Dish对象。 566 int j; 567 int k = 0; 568 for(j = 0; dishes[j] != null; j ++) { 569 if(dishes[j].name.equals(dishName)) { 570 k = 1; 571 break; 572 } 573 } 574 if(k == 1) { 575 return dishes[j]; 576 } 577 else { 578 return null; 579 } 580 } 581 582 Dish addDish(String dishName,int unit_price) {//添加一道菜品信息 583 if(searthDish(dishName) != null){ 584 i --; 585 searthDish(dishName).unit_price = unit_price; 586 return searthDish(dishName); 587 } 588 else { 589 dishes[i] = new Dish(dishName, unit_price); 590 i++; 591 return dishes[i]; 592 } 593 } 594 595 Dish addspecialDish(String dishName,int unit_price, char a, String dish) {//添加一道菜品信息 596 if(searthDish(dishName) != null){ 597 i --; 598 searthDish(dishName).unit_price = unit_price; 599 return searthDish(dishName); 600 } 601 else { 602 dishes[i] = new Dish(dishName, unit_price); 603 dishes[i].dish = dish; 604 dishes[i].isSpecial = 'T'; 605 i++; 606 return dishes[i]; 607 } 608 } 609 610 } 611 class Record { 612 613 int orderNum;//序号\\ 614 Dish d = new Dish();//菜品\\ 615 int lever; 616 int portion;//份额(1/2/3代表小/中/大份)\\ 617 int num;//份数 618 619 public Record() { 620 621 // TODO 自动生成的构造函数存根 622 } 623 624 public Record(int orderNum, String dishName, int portion, int num, Menu m) { 625 this.orderNum = orderNum; 626 this.d = m.searthDish(dishName); 627 this.portion = portion; 628 this.num = num; 629 } 630 631 int getPrice() {//计价,计算本条记录的价格\\ 632 return d.getPrice(this.portion) * this.num; 633 } 634 635 double getPrice0(Table table ) {//计价,计算本条记录的价格\\ 636 double price = 0; 637 if(this.d.isSpecial == 'T'){ 638 if(table.weekday >= 1 && table.weekday <= 5){ 639 price = d.getPrice(this.portion) * this.num * 0.7; 640 } 641 else { 642 price = d.getPrice(this.portion) * this.num; 643 } 644 }else { 645 if(table.weekday >= 1 && table.weekday <= 5){ 646 if(table.hours >= 17 && table.hours <= 20 || table.hours == 20 && table.minutes <= 30){ 647 price = d.getPrice(this.portion) * this.num * 0.8; 648 } 649 else if (table.hours >= 11 && table.hours <= 14 || table.hours == 14 && table.minutes <= 30 || table.hours == 10 && table.minutes >= 30){ 650 price = d.getPrice(this.portion) * this.num * 0.6; 651 } 652 } 653 else { 654 price = d.getPrice(this.portion) * this.num; 655 } 656 } 657 return price; 658 } 659 660 } 661 class Order { 662 663 Record[] records = new Record[10];//保存订单上每一道的记录 664 int j = 1; 665 int i = 1; 666 667 public Order() { 668 669 // TODO 自动生成的构造函数存根 670 } 671 672 public Order(Record[] records) { 673 674 this.records = records; 675 } 676 677 int getTotalPrice() {//计算订单的总价 678 679 int sum = 0; 680 681 for(int j = 1; j < i; j ++) { 682 683 sum += this.records[j].getPrice(); 684 } 685 return sum; 686 } 687 int getTotalPrice0(Table table) {//计算订单的总价 688 double sum = 0; 689 for(int j = 1; j < i; j ++) { 690 691 sum += this.records[j].getPrice0(table); 692 } 693 return (int) Math.round(sum); 694 } 695 Record addARecord(int orderNum,String dishName, int portion,int num, Menu menu) {//添加一条菜品信息到订单中。 696 records[i] = new Record(orderNum, dishName, portion, num, menu); 697 i++; 698 return records[i]; 699 700 } 701 702 Record addASpecialRecord(int orderNum,String dishName, int lever, int portion,int num, Menu menu) {//添加一条菜品信息到订单中。 703 records[i] = new Record(orderNum, dishName, portion, num, menu); 704 records[i].lever = lever; 705 i++; 706 return records[i]; 707 708 } 709 710 void delARecordByOrderNum(int orderNum) {//根据序号删除一条记录 711 712 System.out.println("deduplication" + " " + orderNum); 713 } 714 Record findRecordByNum(int ordernumber) {//根据序号查找一条记录 715 int k = 0; 716 int i; 717 for (i = 1;records[i] != null; i++) { 718 if (this.records[i].orderNum == ordernumber) { 719 k = 1; 720 break; 721 } 722 } 723 if (k == 1) { 724 return this.records[i]; 725 } 726 else { 727 return null; 728 } 729 } 730 } 731 732 class Table { 733 int tablenum; 734 int year; 735 int month; 736 int day; 737 int hours; 738 int minutes; 739 int seconds; 740 int weekday; 741 String name; 742 String phone; 743 Order order = new Order(); 744 int index1 = 0; 745 int index2 = 0; 746 int index3 = 0; 747 int spicy = 0; 748 int acidity = 0; 749 int sweetness = 0; 750 public Table() { 751 752 // TODO 自动生成的构造函数存根 753 } 754 755 public Table(int tablenum, int year, int month, int day, int hours, int minutes, int seconds, int weekday) { 756 this.tablenum = tablenum; 757 this.year = year; 758 this.month = month; 759 this.day = day; 760 this.hours = hours; 761 this.minutes = minutes; 762 this.seconds = seconds; 763 this.weekday = weekday; 764 } 765 766 void tableIdentify() { 767 768 } 769 770 void getDate(String[] temp) { 771 String[] temp1 = temp[5].split("/"); 772 this.tablenum = Integer.parseInt(temp[1]); 773 this.year = Integer.parseInt(temp1[0]); 774 this.month = Integer.parseInt(temp1[1]); 775 this.day = Integer.parseInt(temp1[2]); 776 Date date = null; 777 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 778 try { 779 date = format.parse(this.year + "-" + this.month + "-" + this.day); 780 } catch (ParseException e) { 781 e.printStackTrace(); 782 } 783 Calendar calendar = Calendar.getInstance(); 784 calendar.setTime(date); 785 int index = calendar.get(Calendar.DAY_OF_WEEK); 786 if(index == 1){ 787 this.weekday = 7; 788 } 789 else { 790 this.weekday = index - 1; 791 } 792 } 793 794 void getTime(String[] temp) { 795 String[] temp2 = temp[6].split("/"); 796 this.hours = Integer.parseInt(temp2[0]); 797 this.minutes = Integer.parseInt(temp2[1]); 798 this.seconds = Integer.parseInt(temp2[2]); 799 } 800 801 public boolean isExist(int num, String[] temp){ 802 int k = 0; 803 for(int i = 1; i < num; i ++){ 804 if(this.tablenum == Integer.parseInt(temp[1])){ 805 k = 1; 806 break; 807 } 808 } 809 if(k == 1){ 810 return true; 811 } 812 else { 813 return false; 814 } 815 } 816 int getPrice(int totalprice) { 817 818 if(this.weekday >= 1 && this.weekday <= 5){ 819 if(this.hours >= 17 && this.hours <= 20 || this.hours == 20 && this.minutes <= 30){ 820 totalprice = (int)Math.round(totalprice * 0.8); 821 } 822 else if (this.hours >= 11 && this.hours <= 14 || this.hours == 14 && this.minutes <= 30 || this.hours == 10 && this.minutes >= 30){ 823 totalprice = (int)Math.round(totalprice * 0.6); 824 } 825 } 826 return totalprice; 827 } 828 }

第八次作业(课程成绩统计程序-1)
本次作业分输入与输出两部分,要求通过输入的数据来求得平均分并输出。此次作业的难点在于输入,因为输入数据时需要区分输入的是课程信息还是成绩信息,而成绩信息随课程性质又会有所不同,除此之外,输入的信息是否有误也需要判断,因此,输入是难点。对此我采取的办法是对信息进行逐行输入,然后将其以空格进行分割,因此正确的输入信息都有自己对应的长度,通过对长度的确认来确定输入的是什么信息。这是我学到的一个小技巧。在本次作业中,我首次碰到了代码长度超限的问题,说明我写的代码过多。对代码长度的精简应当也算是提高了效率且优化了代码。因为谁也不想对着超长代码来工作。因此,如何将代码写得尽可能精简又能发挥原有功能且让他人明白易懂也是一个值得考虑的问题。不过,由于时间关系,本次作业中我只是采用了最简单粗暴的方法,那就是把必要的代码删除,能删则删,勉强达到了题目的要求,更改明的改动我在下次迭代中会展示出来。本次作业存在许多信息约束,对此我采用了正则表达式。在学会正则表达式后,随着使用的越来越多,我愈发觉得正则表达式是一个十分便利的工具。它能对数据按自己想要的形式来进行约束,但我也只是学会点皮毛罢了,只会运用匹配与分割。除此之外,正则表达式还能通过对模式的设定来对数据进行操作,对次,我还没有试过。在本次作业中的排序我用的是双循环冒泡排序法,虽然时间效率是比较高,但随着学习的继续,我越发觉得它并不是很优,首先有比它时间效率更高的,其次它需要写的代码过长。因此它并不是现今的最好选择,对此的优化我仍是在之后的题目中有所展示,敬请期待。
某高校课程从性质上分为:必修课、选修课,从考核方式上分为:考试、考察。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重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 import java.util.*; 2 public class Main { 3 public static void main(String[] args) { 4 Controller controller = new Controller(); 5 controller.play(); 6 } 7 public static class Class { 8 private Student[] stu = new Student[10]; 9 int i = 0; 10 public Class() { 11 } 12 public Student addStudent(String[] temp, Course[] courses){ 13 i ++; 14 stu[i] = new Student(temp[0], temp[1]); 15 if (searchCourse(temp[2], courses) == null) { 16 System.out.println(temp[2] + " does not exist"); 17 System.out.println(temp[0] + " " + temp[1] + " did not take any exams"); 18 } 19 else { 20 if (searchCourse(temp[2], courses).exam.equals("考试")) { 21 if (temp.length == 4) 22 System.out.println(temp[0] + " " + temp[1] + ": access mode mismatch"); 23 if (temp.length == 3 || temp.length == 4) 24 System.out.println(temp[0] + " " + temp[1] + " did not take any exams"); 25 else { 26 stu[i].inform.name = temp[2]; 27 stu[i].inform.finalGrade = Integer.parseInt(temp[4]); 28 stu[i].inform.dailyGrade = Integer.parseInt(temp[3]); 29 return stu[i]; 30 } 31 } else { 32 if (temp.length == 5) { 33 System.out.println(temp[0] + " " + temp[1] + ": access mode mismatch"); 34 System.out.println(temp[0] + " " + temp[1] + " did not take any exams"); 35 } 36 else { 37 stu[i].inform.name = temp[2]; 38 stu[i].inform.finalGrade = Integer.parseInt(temp[3]); 39 return stu[i]; 40 } 41 } 42 } 43 return null; 44 } 45 public void sort(){ 46 Student stu; 47 for (int k = 1; k <= this.i - 1; k++) { 48 for (int t = k + 1; t <= this.i; t++) { 49 if (Integer.parseInt(this.stu[k].ID) > Integer.parseInt(this.stu[t].ID)) { 50 stu = this.stu[k]; 51 this.stu[k] = this.stu[t]; 52 this.stu[t] = stu; 53 } 54 } 55 } 56 } 57 public static Course searchCourse(String courseName, Course[] courses) { 58 for (int i = 1; i <= courses.length; i++) { 59 if (courses[i] == null) 60 break; 61 if (courseName.equals(courses[i].name)) { 62 return courses[i]; 63 } 64 } 65 return null; 66 } 67 public int gradeAverage() { 68 int sum1 = 0; 69 double sum2 = 0; 70 double index = 0; 71 for (int i = 1; i <= this.i; i++) { 72 if (stu[i].inform.dailyGrade != -1) { 73 sum1 += stu[i].inform.finalGrade * 0.7; 74 sum2 += stu[i].inform.dailyGrade * 0.3; 75 } else { 76 sum1 += stu[i].inform.finalGrade; 77 } 78 index++; 79 } 80 return (int) ((sum1 + sum2) / index); 81 } 82 public boolean existScore() { 83 for (int i = 1; i <= this.i; i++) { 84 if (stu[i].inform.finalGrade != -1) { 85 return true; 86 } 87 } 88 return false; 89 } 90 } 91 public static class Course { 92 private String name; 93 private String attribute; 94 private String exam; 95 public Course(String name, String attribute, String exam) { 96 this.name = name; 97 this.attribute = attribute; 98 this.exam = exam; 99 } 100 } 101 public static class Student { 102 private String ID; 103 private String name; 104 private Information inform = new Information(); 105 public Student(String ID, String name) { 106 this.ID = ID; 107 this.name = name; 108 } 109 } 110 public static class Information { 111 private String name; 112 private int dailyGrade = -1; 113 private int finalGrade = -1; 114 public Information() { 115 } 116 } 117 public static class Controller{ 118 int i = 0; 119 int j = 0; 120 Class[] classes = new Class[5]; 121 Course[] courses = new Course[5]; 122 public void play(){ 123 Scanner input = new Scanner(System.in); 124 while (true) { 125 String str = input.nextLine(); 126 if (str.equals("end")){ 127 break; 128 } 129 String[] temp = str.split(" "); 130 if (temp.length == 2){ 131 if (temp[1].equals("必修")) { 132 i++; 133 courses[i] = new Course(temp[0], temp[1], "必修"); 134 } 135 if (temp[1].equals("选修")) 136 output(); 137 } 138 if(temp.length == 3) { 139 if (temp[0].matches(".{1,10}") && temp[1].matches("必修|选修") && temp[2].matches("考试|考察")) { 140 if (Class.searchCourse(temp[0], courses) != null) 141 continue; 142 if (temp[1].equals("必修") && temp[2].equals("考察")) 143 System.out.println(temp[0] + " : course type & access mode mismatch"); 144 else { 145 i++; 146 courses[i] = new Course(temp[0], temp[1], temp[2]); 147 } 148 } 149 else 150 output(); 151 } 152 if (temp.length == 4){ 153 if (j == 0) { 154 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100")) { 155 addClass(); 156 classes[j].addStudent(temp, courses); 157 } 158 else 159 output(); 160 } 161 else { 162 if (searchClass(temp[0].substring(0,6)) != -1) { 163 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100")) { 164 classes[searchClass(temp[0].substring(0, 6))].addStudent(temp, courses); 165 } 166 else 167 output(); 168 } else { 169 addClass(); 170 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100")) { 171 classes[j].addStudent(temp, courses); 172 } 173 else 174 output(); 175 } 176 } 177 } 178 if(temp.length == 5) { 179 if (j == 0) { 180 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100") && temp[4].matches("[0-9]|[1-9][0-9]|100")) { 181 addClass(); 182 classes[j].addStudent(temp, courses); 183 } 184 else 185 output(); 186 } else { 187 if (searchClass(temp[0].substring(0,6)) != -1) { 188 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100") && temp[4].matches("[0-9]|[1-9][0-9]|100")) { 189 classes[searchClass(temp[0].substring(0, 6))].addStudent(temp, courses); 190 } 191 else 192 output(); 193 } else { 194 addClass(); 195 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100") && temp[4].matches("[0-9]|[1-9][0-9]|100")) { 196 classes[j].addStudent(temp, courses); 197 } 198 else 199 output(); 200 } 201 } 202 } 203 }Course tre; 204 if (i != 0) { 205 for (int i = 1; i <= this.i - 1; i++) { 206 for (int j = i; j <= this.i; j++) { 207 if (courses[i].name.compareToIgnoreCase(courses[j].name) == 1) { 208 tre = courses[i]; 209 courses[i] = courses[j]; 210 courses[i] = tre; 211 } 212 } 213 } 214 } 215 Class cla; 216 if (j != 0) { 217 for (int i = 1; i <= this.j - 1; i++) { 218 for (int j = i; j <= this.j; j++) { 219 if (classes[i].stu[1].ID.substring(0, 6).compareToIgnoreCase(classes[j].stu[1].ID.substring(0, 6)) == 1) { 220 cla = classes[i]; 221 classes[i] = classes[j]; 222 classes[j] = cla; 223 } 224 } 225 } 226 } 227 for (int j = 1; j <= this.j; j ++){ 228 classes[j].sort(); 229 } 230 for (int k = 1; k <= this.j; k ++){ 231 int index = 1; 232 double sum1 = 0; 233 double sum2 = 0; 234 int index1 = 0; 235 for (int t = 1; t <= classes[k].i + 1; t ++){ 236 if (classes[k].i == 1 && classes[k].stu[1].inform.finalGrade == -1) 237 break; 238 if(t == classes[k].i + 1){ 239 if (index1 == 0) { 240 } 241 else { 242 System.out.println(classes[k].stu[index].ID + " " + classes[k].stu[index].name + " " + (int) ((sum1 + sum2) / index1)); 243 } 244 break; 245 } 246 if(classes[k].stu[t].ID.equals(classes[k].stu[index].ID) && classes[k].stu[t].inform.dailyGrade != -1) { 247 sum1 += classes[k].stu[t].inform.dailyGrade * 0.3; 248 sum2 += classes[k].stu[t].inform.finalGrade * 0.7; 249 index1++; 250 } 251 else if (classes[k].stu[t].ID.equals(classes[k].stu[index].ID) && classes[k].stu[t].inform.finalGrade != -1){ 252 sum2 += classes[k].stu[t].inform.finalGrade; 253 index1 ++; 254 } 255 else { 256 System.out.println(classes[k].stu[index].ID + " " + classes[k].stu[index].name + " " + (int) ((sum1 + sum2) / index1)); 257 index = t; 258 sum1 = 0; 259 sum2 = 0; 260 index1 = 0; 261 t --; 262 } 263 } 264 } 265 for (int i = 1; i <= this.i; i ++) { 266 if (isExistCourseScore(courses[i].name)) { 267 if (courses[i].exam.equals("考试")) { 268 System.out.println(courses[i].name + " " + dailyGradeAverage(courses[i].name) + " " + finalGradeAverage(courses[i].name) + " " + courseAverage(courses[i].name, courses[i])); 269 } 270 if (courses[i].exam.equals("考察")) 271 System.out.println(courses[i].name + " " + finalGradeAverage(courses[i].name) + " " + courseAverage(courses[i].name, courses[i])); 272 } 273 else { 274 System.out.println(courses[i].name + " has no grades yet"); 275 } 276 } 277 for (int i = 1; i <= this.j; i ++) { 278 if (classes[i].existScore()) 279 System.out.println(classes[i].stu[1].ID.substring(0, 6) + " " + classes[i].gradeAverage()); 280 else 281 System.out.println(classes[i].stu[1].ID.substring(0, 6) + " has no grades yet"); 282 } 283 } 284 public Class addClass(){ 285 j ++; 286 classes[j] = new Class(); 287 return classes[j]; 288 } 289 public void output(){ 290 System.out.println("wrong format"); 291 } 292 public int searchClass(String className){ 293 int index = -1; 294 for (int i = 1; i <= this.j; i ++){ 295 if(classes[i].stu[1].ID.substring(0, 6).equals(className)) { 296 index = i; 297 break; 298 } 299 } 300 return index; 301 } 302 public boolean isExistCourseScore(String courseName){ 303 for (int i = 1; i <= this.j; i ++){ 304 for (int j = 1; j <= classes[i].i; j ++) { 305 if (classes[i].stu[i].inform.finalGrade != -1) { 306 if (classes[i].stu[j].inform.name.equals(courseName)) { 307 return true; 308 } 309 } 310 } 311 } 312 return false; 313 } 314 public int dailyGradeAverage(String courseName){ 315 int sum = 0; 316 int index = 0; 317 for (int i = 1; i <= this.j; i ++) { 318 for (int j = 1; j <= classes[i].i; j ++) { 319 if (classes[i].stu[j].inform.dailyGrade != -1 && classes[i].stu[j].inform.name.equals(courseName)) { 320 sum += classes[i].stu[j].inform.dailyGrade; 321 index++; 322 } 323 } 324 } 325 if(index == 0) 326 return 0; 327 else 328 return sum / index; 329 } 330 public int finalGradeAverage(String name){ 331 int sum = 0; 332 int index = 0; 333 for (int i = 1; i <= this.j; i ++) { 334 for (int j = 1; j <= classes[i].i; j ++) { 335 if (classes[i].stu[j].inform.finalGrade != -1 && classes[i].stu[j].inform.name.equals(name)) 336 { 337 sum += classes[i].stu[j].inform.finalGrade; 338 index++; 339 } 340 } 341 } 342 if(index == 0) 343 return 0; 344 else 345 return sum / index; 346 } 347 public int courseAverage(String courseName, Course course){ 348 int sum1 = 0; 349 int sum2 = 0; 350 int index = 0; 351 if(course.exam.equals("考试")){ 352 for (int i = 1; i <= this.j; i ++) { 353 for (int j = 1; j <= classes[i].i; j ++) { 354 if (classes[i].stu[j].inform.name.equals(courseName) && classes[i].stu[j].inform.dailyGrade != -1) { 355 sum1 += classes[i].stu[j].inform.finalGrade; 356 sum2 += classes[i].stu[j].inform.dailyGrade; 357 index++; 358 } 359 } 360 } 361 if (index == 0) 362 return 0; 363 else 364 return (int) ((sum1 * 0.7 + sum2 * 0.3) / index); 365 } 366 else { 367 for (int i = 1; i <= this.j; i++) { 368 for (int j = 1; j <= classes[i].i; j ++) { 369 if (classes[i].stu[j].inform.name.equals(courseName) && classes[i].stu[j].inform.finalGrade != -1) { 370 sum1 += classes[i].stu[j].inform.finalGrade; 371 index++; 372 } 373 } 374 } 375 if (index == 0) 376 return 0; 377 else 378 return sum1 / index; 379 } 380 } 381 } 382 }

第九次作业(统计Java程序中关键词的出现次数)
本题需要将输入的一段代码中的各个关键字的次数进行统计。对此,我使用了Map类,它十分适用于这种情况。首先,我将53个关键字直接作为Map的关键词并使所有值全部为零。对于输入的数据,我先删去了无用的数据,然后进行分割,最后使用Map的containsKey方法来判断子字符串是否为Map中的一个关键字,以此决定关键字对应的值是否加一。
1 import java.util.*; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 String str = "abstract、assert、boolean、break、byte、case、catch、char、class、const、continue、default、do、double、else、enum、extends、false、final、finally、float、for、goto、if、implements、import、instanceof、int、interface、long、native、new、null、package、private、protected、public、return、short、static、strictfp、super、switch、synchronized、this、throw、throws、transient、true、try、void、volatile、while"; 7 String[] temp = str.split("、"); 8 TreeSet<String> set = new TreeSet<>(List.of(temp)); 9 TreeMap<String, Integer> map = new TreeMap<>(); 10 int flag = -1; 11 int j = 0; 12 13 for (int i = 0; i < temp.length; i++) 14 map.put(temp[i], 0); 15 ArrayList<String> list = new ArrayList<>(); 16 list.add(input.nextLine()); 17 if(list.get(0).equals("exit")) { 18 System.out.println("Wrong Format"); 19 return; 20 } 21 22 while (!list.get(j).equals("exit")) { 23 24 while (list.get(j).contains("\"")){ 25 int index1 = list.get(j).indexOf("\""); 26 int index2 = list.get(j).indexOf("\"", index1 + 1); 27 list.set(j, list.get(j).substring(0, index1) + " " + list.get(j).substring(index2 + 1)); 28 } 29 if (list.get(j).contains("//")){ 30 int index1 = list.get(j).indexOf("//"); 31 list.set(j, list.get(j).substring(0, index1)); 32 } 33 String[] temp1 = list.get(j).split("[ ,{}.();:\\]\\[]"); 34 for (int i = 0; i < temp1.length; i++) { 35 if (temp1[i].matches("/\\*.*")) { 36 flag = 1; 37 break; 38 } 39 if (temp1[temp1.length - 1].matches(".*\\*/")){ 40 flag = -1; 41 break; 42 } 43 if (flag == 1) 44 break; 45 if (map.containsKey(temp1[i])) 46 map.put(temp1[i], map.get(temp1[i]) + 1); 47 } 48 list.add(input.nextLine()); 49 j ++; 50 } 51 52 for (int i = 0; i < temp.length; i++) { 53 if (map.get(temp[i]) != 0) 54 System.out.println(map.get(temp[i]) + "\t" + temp[i]); 55 else 56 map.remove(temp[i]); 57 } 58 } 59 }

第十次作业(课程成绩统计程序-2)
本次作业是第八次作业的迭代,较之前新增了实验课。在这次作业中,我将原来的代码进行了几点优化,因此差异较大,但整体结构不变。例如,我在创建对象时大都创建的是Map类,这是Map类的关键词唯一的优势,可以避免创建重复的对象,引入重复的信息。此外,Map类属于集合,因此可以用Collections中的sort方法来进行排序,这不仅减少了代码量,同时也提高了排序效率。当然,Map类还自带了许多实用的方法,这在一定程度上也减少了自定义方法的编写,降低代码量。Map类对数据的增、删、查、改也很方便且高效。大大减少了代码的时间复杂度与空间复杂度。
课程成绩统计程序-2在第一次的基础上增加了实验课,以下加粗字体显示为本次新增的内容。
某高校课程从性质上分为:必修课、选修课、实验课,从考核方式上分为:考试、考察、实验。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。
考察的总成绩直接等于期末成绩
实验的总成绩等于课程每次实验成绩的平均分
必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。实验课的成绩必须为实验。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。
课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
课程性质输入项:必修、选修、实验
考核方式输入选项:考试、考察、实验
考试/考查课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
考试/考查课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
实验课程成绩信息包括:学号、姓名、课程名称、实验次数、每次成绩
实验次数至少4次,不超过9次
实验课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+实验次数+英文空格+第一次实验成绩+...+英文空格+最后一次实验成绩
以上信息的相关约束:
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 import java.util.*; 2 public class Main { 3 public static void main(String[] args) { 4 Controller controller = new Controller(); 5 controller.play(); 6 } 7 public static class Class { 8 private HashMap<String, Student> stu = new HashMap<>(); 9 List<Map.Entry<String, Student>> list3; 10 public Class() { 11 } 12 public Student addStudent(String[] temp, HashMap<String, Course> courses){ 13 if (!stu.containsKey(temp[0])) 14 stu.put(temp[0], new Student(temp[0], temp[1], temp[2], -1, -1, -1)); 15 if (!courses.containsKey(temp[2])) { 16 System.out.println(temp[2] + " does not exist"); 17 System.out.println(temp[0] + " " + temp[1] + " did not take any exams"); 18 } 19 else { 20 if (courses.get(temp[2]).exam.equals("考试")) { 21 if (temp.length == 4) 22 System.out.println(temp[0] + " " + temp[1] + " : access mode mismatch"); 23 if (temp.length == 3 || temp.length == 4) 24 System.out.println(temp[0] + " " + temp[1] + " did not take any exams"); 25 else { 26 stu.get(temp[0]).inform = new Information(temp[2], Integer.parseInt(temp[3]), Integer.parseInt(temp[4]), -1); 27 return stu.get(temp[0]); 28 } 29 } else if (courses.get(temp[2]).exam.equals("考察")){ 30 if (temp.length == 5) { 31 System.out.println(temp[0] + " " + temp[1] + " : access mode mismatch"); 32 System.out.println(temp[0] + " " + temp[1] + " did not take any exams"); 33 } 34 else { 35 stu.get(temp[0]).inform = new Information(temp[2], -1, Integer.parseInt(temp[3]), -1); 36 return stu.get(temp[0]); 37 } 38 } 39 else { 40 if (Integer.parseInt(temp[3]) == temp.length - 4) { 41 int sum = 0; 42 for (int j = 0; j < temp.length - 4; j++) 43 sum += Integer.parseInt(temp[j + 4]); 44 stu.get(temp[0]).inform = new Information(temp[2], -1, -1, sum / Integer.parseInt(temp[3])); 45 return stu.get(temp[0]); 46 } 47 else { 48 System.out.println(temp[0] + " " + temp[1] + " : access mode mismatch"); 49 System.out.println(temp[0] + " " + temp[1] + " did not take any exams"); 50 } 51 } 52 } 53 return null; 54 } 55 public void sort(){ 56 list3 = new ArrayList<>(stu.entrySet()); 57 Collections.sort(list3, Comparator.comparing(Map.Entry::getKey)); 58 } 59 public int gradeAverage() { 60 int sum1 = 0; 61 double sum2 = 0; 62 double index = 0; 63 List<Map.Entry<String, Student>> list = new ArrayList<>(stu.entrySet()); 64 for (int i = 0; i < list.size(); i++) { 65 if (list.get(i).getValue().inform.dailyGrade != -1) { 66 sum1 += list.get(i).getValue().inform.finalGrade * 0.7; 67 sum2 += list.get(i).getValue().inform.dailyGrade * 0.3; 68 } else if (list.get(i).getValue().inform.finalGrade != -1){ 69 sum1 += list.get(i).getValue().inform.finalGrade; 70 } 71 else if (list.get(i).getValue().inform.testGrade != -1){ 72 sum1 += list.get(i).getValue().inform.testGrade; 73 } 74 index++; 75 } 76 return (int) ((sum1 + sum2) / index); 77 } 78 public boolean existScore() { 79 List<Map.Entry<String, Student>> list1 = new ArrayList<>(stu.entrySet()); 80 for (int i = 0; i < list1.size(); i++) { 81 if (list1.get(i).getValue().inform.finalGrade != -1 || list1.get(i).getValue().inform.testGrade != -1) { 82 return true; 83 } 84 } 85 return false; 86 } 87 } 88 public static class Course { 89 private String name; 90 private String attribute; 91 private String exam; 92 public Course(String name, String attribute, String exam) { 93 this.name = name; 94 this.attribute = attribute; 95 this.exam = exam; 96 } 97 } 98 public static class Student { 99 private String ID; 100 private String name; 101 private Information inform; 102 public Student(String ID, String name, String courseName, int dailyGrade, int finalGrade, int testGrade) { 103 this.ID = ID; 104 this.name = name; 105 this.inform = new Information(courseName, dailyGrade, finalGrade, testGrade); 106 } 107 } 108 public static class Information { 109 private String name; 110 private int dailyGrade; 111 private int finalGrade; 112 private int testGrade; 113 114 public Information(String name, int dailyGrade, int finalGrade, int testGrade) { 115 this.name = name; 116 this.dailyGrade = dailyGrade; 117 this.finalGrade = finalGrade; 118 this.testGrade = testGrade; 119 } 120 } 121 public static class Controller{ 122 HashMap<String, Class> classes = new HashMap<>(); 123 HashMap<String, Course> courses = new HashMap<>(); 124 List<Map.Entry<String, Course>> list1; 125 List<Map.Entry<String, Class>> list2; 126 public void play(){ 127 Scanner input = new Scanner(System.in); 128 while (true) { 129 String str = input.nextLine(); 130 if (str.equals("end")){ 131 break; 132 } 133 String[] temp = str.split(" "); 134 if (temp.length == 2){ 135 if (temp[1].equals("必修")) { 136 courses.put(temp[0], new Course(temp[0], temp[1], "必修")); 137 } 138 if (temp[1].equals("选修")) 139 output(); 140 } 141 else if(temp.length == 3) { 142 if (temp[0].matches(".{1,10}") && temp[1].matches("必修|选修|实验") && temp[2].matches("考试|考察|实验")) { 143 if (courses.containsKey(temp[0])) 144 continue; 145 if (temp[1].equals("必修") && !temp[2].equals("考试") || temp[1].equals("实验") && !temp[2].equals("实验")) 146 System.out.println(temp[0] + " : course type & access mode mismatch"); 147 else { 148 courses.put(temp[0], new Course(temp[0], temp[1], temp[2])); 149 } 150 } 151 else 152 output(); 153 } 154 else if (temp.length == 4){ 155 if (!classes.containsKey(temp[0].substring(0, 6))) { 156 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100")) { 157 classes.put(temp[0].substring(0, 6), new Class()); 158 classes.get(temp[0].substring(0, 6)).addStudent(temp, courses); 159 } 160 else { 161 output(); 162 } 163 } 164 else { 165 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100")) 166 classes.get(temp[0].substring(0, 6)).addStudent(temp, courses); 167 else { 168 output(); 169 } 170 } 171 } 172 else if(temp.length == 5) { 173 if (!classes.containsKey(temp[0].substring(0, 6))) { 174 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100") && temp[4].matches("[0-9]|[1-9][0-9]|100")) { 175 classes.put(temp[0].substring(0, 6), new Class()); 176 classes.get(temp[0].substring(0, 6)).addStudent(temp, courses); 177 } 178 else { 179 output(); 180 } 181 } else { 182 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100") && temp[4].matches("[0-9]|[1-9][0-9]|100")) { 183 classes.get(temp[0].substring(0, 6)).addStudent(temp, courses); 184 } 185 else { 186 output(); 187 } 188 } 189 } 190 else { 191 int r = 0; 192 if (!classes.containsKey(temp[0].substring(0, 6))) { 193 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[4-9]")) { 194 for (int a = 0; a < temp.length - 4; a++) { 195 if (!temp[a + 4].matches("[0-9]|[1-9][0-9]|100")) { 196 System.out.println("wrong format"); 197 r = 1; 198 break; 199 } 200 } 201 if (r == 0) { 202 classes.put(temp[0].substring(0, 6), new Class()); 203 classes.get(temp[0].substring(0, 6)).addStudent(temp, courses); 204 }} 205 else { 206 output(); 207 } 208 } 209 else { 210 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[4-9]")) { 211 for (int a = 0; a < temp.length - 4; a++) { 212 if (!temp[a + 4].matches("[0-9]|[1-9][0-9]|100")) { 213 output(); 214 r = 1; 215 break; 216 } 217 } 218 if (r == 0) 219 classes.get(temp[0].substring(0, 6)).addStudent(temp, courses); 220 } 221 else { 222 output(); 223 } 224 } 225 } 226 } 227 list1 = new ArrayList<>(courses.entrySet()); 228 Collections.sort(list1, (o1, o2) -> o1.getKey().compareTo(o2.getKey())); 229 list2 = new ArrayList<>(classes.entrySet()); 230 Collections.sort(list2, Comparator.comparing(Map.Entry::getKey)); 231 for (int j = 0; j < list2.size(); j ++){ 232 list2.get(j).getValue().sort(); 233 } 234 for (int k = 0; k < list2.size(); k ++){ 235 int index = 0; 236 double sum1 = 0; 237 double sum2 = 0; 238 int index1 = 0; 239 for (int t = 0; t <= list2.get(k).getValue().list3.size(); t ++){ 240 241 if (list2.get(k).getValue().list3.size() == 1 && list2.get(k).getValue().list3.get(0).getValue().inform.finalGrade == -1 && list2.get(k).getValue().list3.get(0).getValue().inform.testGrade == -1) 242 break; 243 if(t == list2.get(k).getValue().list3.size()){ 244 if (index1 == 0) { 245 } 246 else { 247 System.out.println(list2.get(k).getValue().list3.get(index).getValue().ID + " " + list2.get(k).getValue().list3.get(index).getValue().name + " " + (int) ((sum1 + sum2) / index1)); 248 } 249 break; 250 } 251 if (list2.get(k).getValue().list3.get(t).getValue().inform.finalGrade == -1 && list2.get(k).getValue().list3.get(t).getValue().inform.dailyGrade == -1 && list2.get(k).getValue().list3.get(t).getValue().inform.testGrade == -1) 252 continue; 253 if(list2.get(k).getValue().list3.get(t).getValue().ID.equals(list2.get(k).getValue().list3.get(index).getValue().ID) && list2.get(k).getValue().list3.get(t).getValue().inform.dailyGrade != -1) { 254 sum1 += list2.get(k).getValue().list3.get(t).getValue().inform.dailyGrade * 0.3; 255 sum2 += list2.get(k).getValue().list3.get(t).getValue().inform.finalGrade * 0.7; 256 index1 ++; 257 } 258 else if (list2.get(k).getValue().list3.get(t).getValue().ID.equals(list2.get(k).getValue().list3.get(index).getValue().ID) && list2.get(k).getValue().list3.get(t).getValue().inform.finalGrade != -1){ 259 sum2 += list2.get(k).getValue().list3.get(t).getValue().inform.finalGrade; 260 index1 ++; 261 } 262 else if (list2.get(k).getValue().list3.get(t).getValue().ID.equals(list2.get(k).getValue().list3.get(index).getValue().ID) && list2.get(k).getValue().list3.get(t).getValue().inform.testGrade != -1) { 263 sum2 += list2.get(k).getValue().list3.get(t).getValue().inform.testGrade; 264 index1 ++; 265 } 266 else { 267 System.out.println(list2.get(k).getValue().list3.get(index).getValue().ID + " " + list2.get(k).getValue().list3.get(index).getValue().name + " " + (int) ((sum1 + sum2) / index1)); 268 index = t; 269 sum1 = 0; 270 sum2 = 0; 271 index1 = 0; 272 t --; 273 } 274 } 275 } 276 for (int i = 0; i < list1.size(); i ++) { 277 if (isExistCourseScore(list1.get(i).getValue().name)) { 278 if (list1.get(i).getValue().exam.equals("考试")) { 279 System.out.println(list1.get(i).getValue().name + " " + dailyGradeAverage(list1.get(i).getValue().name) + " " + finalGradeAverage(list1.get(i).getValue().name) + " " + courseAverage(list1.get(i).getValue().name, list1.get(i).getValue())); 280 } 281 if (list1.get(i).getValue().exam.equals("考察")) 282 System.out.println(list1.get(i).getValue().name + " " + finalGradeAverage(list1.get(i).getValue().name) + " " + courseAverage(list1.get(i).getValue().name, list1.get(i).getValue())); 283 if (list1.get(i).getValue().exam.equals("实验")) 284 System.out.println(list1.get(i).getValue().name + " " + courseAverage(list1.get(i).getValue().name, list1.get(i).getValue())); 285 286 } 287 else { 288 System.out.println(list1.get(i).getValue().name + " has no grades yet"); 289 } 290 } 291 for (int i = 0; i < list2.size(); i ++) { 292 if (list2.get(i).getValue().existScore()) 293 System.out.println(list2.get(i).getValue().list3.get(0).getValue().ID.substring(0, 6) + " " + list2.get(i).getValue().gradeAverage()); 294 else 295 System.out.println(list2.get(i).getValue().list3.get(0).getValue().ID.substring(0, 6) + " has no grades yet"); 296 } 297 } 298 public void output(){ 299 System.out.println("wrong format"); 300 } 301 public boolean isExistCourseScore(String courseName){ 302 for (int i = 0; i < list2.size(); i ++){ 303 for (int j = 0; j < list2.get(i).getValue().list3.size(); j ++) { 304 if (list2.get(i).getValue().list3.get(j).getValue().inform.finalGrade != -1 || list2.get(i).getValue().list3.get(j).getValue().inform.testGrade != -1) { 305 if (list2.get(i).getValue().list3.get(j).getValue().inform.name.equals(courseName)) { 306 return true; 307 } 308 } 309 } 310 } 311 return false; 312 } 313 public int dailyGradeAverage(String courseName){ 314 int sum = 0; 315 int index = 0; 316 for (int i = 0; i < list2.size(); i ++){ 317 for (int j = 0; j < list2.get(i).getValue().list3.size(); j ++) { 318 if (list2.get(i).getValue().list3.get(j).getValue().inform.dailyGrade != -1 && list2.get(i).getValue().list3.get(j).getValue().inform.name.equals(courseName)) { 319 sum += list2.get(i).getValue().list3.get(j).getValue().inform.dailyGrade; 320 index++; 321 } 322 } 323 } 324 if(index == 0) 325 return 0; 326 else 327 return sum / index; 328 } 329 public int finalGradeAverage(String name){ 330 int sum = 0; 331 int index = 0; 332 for (int i = 0; i < list2.size(); i ++){ 333 for (int j = 0; j < list2.get(i).getValue().list3.size(); j ++) { 334 if (list2.get(i).getValue().list3.get(j).getValue().inform.finalGrade != -1 && list2.get(i).getValue().list3.get(j).getValue().inform.name.equals(name)) 335 { 336 sum += list2.get(i).getValue().list3.get(j).getValue().inform.finalGrade; 337 index++; 338 } 339 } 340 } 341 if(index == 0) 342 return 0; 343 else 344 return sum / index; 345 } 346 public int courseAverage(String courseName, Course course) { 347 int sum1 = 0; 348 int sum2 = 0; 349 int index = 0; 350 if (course.exam.equals("考试")) { 351 for (int i = 0; i < list2.size(); i ++){ 352 for (int j = 0; j < list2.get(i).getValue().list3.size(); j++) { 353 if (list2.get(i).getValue().list3.get(j).getValue().inform.name.equals(courseName) && list2.get(i).getValue().list3.get(j).getValue().inform.dailyGrade != -1) { 354 sum1 += list2.get(i).getValue().list3.get(j).getValue().inform.finalGrade; 355 sum2 += list2.get(i).getValue().list3.get(j).getValue().inform.dailyGrade; 356 sum1 = (int) (sum1 * 0.7 + sum2 * 0.3); 357 index++; 358 } 359 } 360 } 361 if (index == 0) 362 return 0; 363 else 364 return sum1 / index; 365 } 366 if (course.exam.equals("考察")) { 367 for (int i = 0; i < list2.size(); i ++){ 368 for (int j = 0; j < list2.get(i).getValue().list3.size(); j++) { 369 if (list2.get(i).getValue().list3.get(j).getValue().inform.name.equals(courseName) && list2.get(i).getValue().list3.get(j).getValue().inform.finalGrade != -1) { 370 sum1 += list2.get(i).getValue().list3.get(j).getValue().inform.finalGrade; 371 index++; 372 } 373 } 374 } 375 if (index == 0) 376 return 0; 377 else 378 return sum1 / index; 379 } 380 else { 381 for (int i = 0; i < list2.size(); i ++){ 382 for (int j = 0; j < list2.get(i).getValue().list3.size(); j++) { 383 if (list2.get(i).getValue().list3.get(j).getValue().inform.name.equals(courseName) && list2.get(i).getValue().list3.get(j).getValue().inform.testGrade != -1) { 384 sum1 += list2.get(i).getValue().list3.get(j).getValue().inform.testGrade; 385 index++; 386 } 387 } 388 } 389 if (index == 0) 390 return 0; 391 else 392 return sum1 / index; 393 } 394 } 395 396 } 397 }

第十一次作业(课程成绩统计程序-3)
本次作业较之前使权重值不再固定,能够自己设定,因此平均分的计算方法就改变了。在这次作业中,我在学生类中新增了总成绩属性,简化了之前的平均分计算方法。在此之前,我都是直接将原始数据存储,每次计算平均分都是从平时成绩那开始算起,这样程序运行就比较耗费时间,且增加代码量。因此这次我新增了总成绩属性,在将数据存储后,多一步计算出该学生的总成绩存储起来。在之后计算平均分时,我就直接将相应学生的总成绩相加进行处理就行。在判别成绩是否存在时也可以直接通过判别总成绩是否存在,而不用通过判别平时成绩、期末成绩是否存在。
课程成绩统计程序-3在第二次的基础上修改了计算总成绩的方式,
要求:修改类结构,将成绩类的继承关系改为组合关系,成绩信息由课程成绩类和分项成绩类组成,课程成绩类组合分项成绩类,分项成绩类由成绩分值和权重两个属性构成。
完成课程成绩统计程序-2、3两次程序后,比较继承和组合关系的区别。思考一下哪一种关系运用上更灵活,更能够适应变更。
题目最后的参考类图未做修改,大家根据要求自行调整,以下内容加粗字体显示的内容为本次新增的内容。
某高校课程从性质上分为:必修课、选修课、实验课,从考核方式上分为:考试、考察、实验。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。
考察的总成绩直接等于期末成绩
实验的总成绩等于课程每次实验成绩乘以权重后累加而得。
课程权重值在录入课程信息时输入。(注意:所有分项成绩的权重之和应当等于1)
必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。实验课的成绩必须为实验。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式、分项成绩数量、每个分项成绩的权重。
考试课信息格式:课程名称+英文空格+课程性质+英文空格+考核方式+英文空格+平时成绩的权重+英文空格+期末成绩的权重
考察课信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
实验课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式+英文空格+分项成绩数量n+英文空格+分项成绩1的权重+英文空格+。。。+英文空格+分项成绩n的权重
实验次数至少4次,不超过9次
课程性质输入项:必修、选修、实验
考核方式输入选项:考试、考察、实验
考试/考查课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
考试/考查课程成绩信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
实验课程成绩信息包括:学号、姓名、课程名称、每次成绩{在系列-2的基础上去掉了(实验次数),实验次数要和实验课程信息中输入的分项成绩数量保持一致}
实验课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+第一次实验成绩+...+英文空格+最后一次实验成绩
以上信息的相关约束:
1)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】
2)学号由8位数字组成
3)姓名不超过10个字符
4)课程名称不超过10个字符
5)不特别输入班级信息,班级号是学号的前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)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。
6)如果解析实验课程信息时,输入的分项成绩数量值和分项成绩权重的个数不匹配,输出:课程名称+" : number of scores does not match"
7)如果解析考试课、实验课时,分项成绩权重值的总和不等于1,输出:课程名称+" : weight value error"
信息约束:
1)成绩平均分只取整数部分,小数部分丢弃
1 import java.util.*; 2 public class Main { 3 public static void main(String[] args) { 4 Controller controller = new Controller(); 5 controller.play(); 6 } 7 public static class Class { 8 private HashMap<String, Student> stu = new HashMap<>(); 9 List<Map.Entry<String, Student>> list3; 10 public Class() { 11 } 12 public Student addStudent(String[] temp, HashMap<String, Course> courses){ 13 if (!stu.containsKey(temp[0])) 14 stu.put(temp[0], new Student(temp[0], temp[1], temp[2], -1, -1, -1)); 15 if (!courses.containsKey(temp[2])) { 16 System.out.println(temp[2] + " does not exist"); 17 System.out.println(temp[0] + " " + temp[1] + " did not take any exams"); 18 } 19 else { 20 if (courses.get(temp[2]).exam.equals("考试")) { 21 if (temp.length == 4) 22 System.out.println(temp[0] + " " + temp[1] + " : access mode mismatch"); 23 if (temp.length == 3 || temp.length == 4) 24 System.out.println(temp[0] + " " + temp[1] + " did not take any exams"); 25 else { 26 stu.get(temp[0]).inform = new Information(temp[2], Double.parseDouble(temp[3]) * courses.get(temp[2]).weight[0], Double.parseDouble(temp[4]) * courses.get(temp[2]).weight[1], -1); 27 stu.get(temp[0]).sum = (int) (Integer.parseInt(temp[3]) * courses.get(temp[2]).weight[0] + Integer.parseInt(temp[4]) * courses.get(temp[2]).weight[1]); 28 return stu.get(temp[0]); 29 } 30 } else if (courses.get(temp[2]).exam.equals("考察")){ 31 if (temp.length == 5) { 32 System.out.println(temp[0] + " " + temp[1] + " : access mode mismatch"); 33 System.out.println(temp[0] + " " + temp[1] + " did not take any exams"); 34 } 35 else { 36 stu.get(temp[0]).inform = new Information(temp[2], -1, Double.parseDouble(temp[3]), -1); 37 stu.get(temp[0]).sum = Integer.parseInt(temp[3]); 38 return stu.get(temp[0]); 39 } 40 } 41 else { 42 if (Integer.parseInt(courses.get(temp[2]).sum) == temp.length - 3) { 43 double sum = 0; 44 for (int j = 0; j < temp.length - 3; j++) 45 sum += Double.parseDouble(temp[j + 3]) * courses.get(temp[2]).weight[j]; 46 stu.get(temp[0]).inform = new Information(temp[2], -1, -1, sum); 47 stu.get(temp[0]).sum = (int) sum; 48 return stu.get(temp[0]); 49 } 50 else { 51 System.out.println(temp[0] + " " + temp[1] + " : access mode mismatch"); 52 System.out.println(temp[0] + " " + temp[1] + " did not take any exams"); 53 } 54 } 55 } 56 return null; 57 } 58 59 public boolean existScore() { 60 list3 = new ArrayList<>(stu.entrySet()); 61 for (int i = 0; i < list3.size(); i++) { 62 if (list3.get(i).getValue().inform.finalGrade != -1 || list3.get(i).getValue().inform.testGrade != -1) { 63 return true; 64 } 65 } 66 return false; 67 } 68 } 69 public static class Course { 70 private String name; 71 private String attribute; 72 private String exam; 73 private String sum; 74 private double[] weight; 75 public Course(String[] temp) { 76 this.name = temp[0]; 77 this.attribute = temp[1]; 78 this.exam = temp[2]; 79 this.sum = temp[3]; 80 weight = new double[Integer.parseInt(sum)]; 81 for (int i = 0; i < Integer.parseInt(sum); i ++) 82 this.weight[i] = Double.parseDouble(temp[i + 4]); 83 } 84 public Course(String name, String attribute, String exam) { 85 this.name = name; 86 this.attribute = attribute; 87 this.exam = exam; 88 } 89 public Course(String name, String attribute, String exam, double weight1, double weight2) { 90 this.name = name; 91 this.attribute = attribute; 92 this.exam = exam; 93 weight = new double[2]; 94 this.weight[0] = weight1; 95 this.weight[1] = weight2; 96 } 97 } 98 public static class Student { 99 private String ID; 100 private String name; 101 private Information inform; 102 private int sum = -1; 103 public Student(String ID, String name, String courseName, int dailyGrade, int finalGrade, int testGrade) { 104 this.ID = ID; 105 this.name = name; 106 this.inform = new Information(courseName, dailyGrade, finalGrade, testGrade); 107 } 108 } 109 public static class Information { 110 private String name; 111 private double dailyGrade; 112 private double finalGrade; 113 private double testGrade; 114 115 public Information(String name, double dailyGrade, double finalGrade, double testGrade) { 116 this.name = name; 117 this.dailyGrade = dailyGrade; 118 this.finalGrade = finalGrade; 119 this.testGrade = testGrade; 120 } 121 } 122 public static class Controller{ 123 HashMap<String, Class> classes = new HashMap<>(); 124 HashMap<String, Course> courses = new HashMap<>(); 125 List<Map.Entry<String, Course>> list1; 126 List<Map.Entry<String, Class>> list2; 127 public void play(){ 128 Scanner input = new Scanner(System.in); 129 while (true) { 130 String str = input.nextLine(); 131 if (str.equals("end")){ 132 break; 133 } 134 String[] temp = str.split(" "); 135 136 if(!temp[0].matches("\\d{8}")) { 137 double sum = 0; 138 if (temp[0].matches(".{1,10}") && temp[1].matches("必修|选修|实验") && temp[2].matches("考试|考察|实验")) { 139 if (courses.containsKey(temp[0])) 140 continue; 141 if (temp[1].equals("必修") && !temp[2].equals("考试") || temp[1].equals("实验") && !temp[2].equals("实验")) 142 System.out.println(temp[0] + " : course type & access mode mismatch"); 143 if (temp[1].matches("实验")) { 144 if (!temp[3].matches("[4-9]")) { 145 output(); 146 continue; 147 } 148 if (Integer.parseInt(temp[3]) != temp.length - 4) { 149 System.out.println(temp[0] + " : number of scores does not match"); 150 continue; 151 } 152 } 153 if (temp[2].matches("实验")){ 154 for (int i = 0; i < temp.length - 4; i ++) 155 sum += Double.parseDouble(temp[i + 4]); 156 if ( sum != 1) 157 System.out.println(temp[0] + " : weight value error"); 158 else 159 courses.put(temp[0], new Course(temp)); 160 } 161 if (temp[2].matches("考试")){ 162 for (int i = 0; i < temp.length - 3; i ++) 163 sum += Double.parseDouble(temp[i + 3]); 164 if ( sum != 1) 165 System.out.println(temp[0] + " : weight value error"); 166 else 167 courses.put(temp[0], new Course(temp[0], temp[1], temp[2], Double.parseDouble(temp[3]), Double.parseDouble(temp[4]))); 168 } 169 if (temp[2].matches("考察") && temp.length == 3) 170 courses.put(temp[0], new Course(temp[0], temp[1], temp[2])); 171 } 172 else 173 output(); 174 } 175 else if (temp.length == 4){ 176 if (!classes.containsKey(temp[0].substring(0, 6))) { 177 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100")) { 178 classes.put(temp[0].substring(0, 6), new Class()); 179 classes.get(temp[0].substring(0, 6)).addStudent(temp, courses); 180 } 181 else { 182 output(); 183 } 184 } 185 else { 186 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100")) 187 classes.get(temp[0].substring(0, 6)).addStudent(temp, courses); 188 else { 189 output(); 190 } 191 } 192 } 193 else if(temp.length == 5) { 194 if (!classes.containsKey(temp[0].substring(0, 6))) { 195 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100") && temp[4].matches("[0-9]|[1-9][0-9]|100")) { 196 classes.put(temp[0].substring(0, 6), new Class()); 197 classes.get(temp[0].substring(0, 6)).addStudent(temp, courses); 198 } 199 else { 200 output(); 201 } 202 } else { 203 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}") && temp[3].matches("[0-9]|[1-9][0-9]|100") && temp[4].matches("[0-9]|[1-9][0-9]|100")) { 204 classes.get(temp[0].substring(0, 6)).addStudent(temp, courses); 205 } 206 else { 207 output(); 208 } 209 } 210 } 211 else { 212 int r = 0; 213 if (!classes.containsKey(temp[0].substring(0, 6))) { 214 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}")) { 215 for (int a = 0; a < temp.length - 3; a++) { 216 if (!temp[a + 3].matches("[0-9]|[1-9][0-9]|100")) { 217 System.out.println("wrong format"); 218 r = 1; 219 break; 220 } 221 } 222 if (r == 0) { 223 classes.put(temp[0].substring(0, 6), new Class()); 224 classes.get(temp[0].substring(0, 6)).addStudent(temp, courses); 225 } 226 } else { 227 output(); 228 } 229 } 230 else { 231 if (temp[0].matches("[\\d]{8}") && temp[1].matches(".{1,10}") && temp[2].matches(".{1,10}")) { 232 for (int a = 0; a < temp.length - 3; a++) { 233 if (!temp[a + 3].matches("[0-9]|[1-9][0-9]|100")) { 234 output(); 235 r = 1; 236 break; 237 } 238 } 239 if (r == 0) 240 classes.get(temp[0].substring(0, 6)).addStudent(temp, courses); 241 } 242 else { 243 output(); 244 } 245 } 246 } 247 } 248 list1 = new ArrayList<>(courses.entrySet()); 249 Collections.sort(list1, (o1, o2) -> o1.getKey().compareTo(o2.getKey())); 250 list2 = new ArrayList<>(classes.entrySet()); 251 Collections.sort(list2, Comparator.comparing(Map.Entry::getKey)); 252 for (int j = 0; j < list2.size(); j ++){ 253 list2.get(j).getValue().list3 = new ArrayList<>(list2.get(j).getValue().stu.entrySet()); 254 Collections.sort(list2.get(j).getValue().list3, Comparator.comparing(Map.Entry::getKey)); 255 } 256 for (int k = 0; k < list2.size(); k ++){ 257 int index = 0; 258 for (int t = 0; t < list2.get(k).getValue().list3.size(); t ++){ 259 if (list2.get(k).getValue().list3.get(index).getValue().sum != -1) 260 System.out.println(list2.get(k).getValue().list3.get(index).getValue().ID + " " + list2.get(k).getValue().list3.get(index).getValue().name + " " + list2.get(k).getValue().list3.get(index).getValue().sum); 261 } 262 } 263 for (int i = 0; i < list1.size(); i ++) { 264 if (isExistCourseScore(list1.get(i).getValue().name)) { 265 // if (list1.get(i).getValue().exam.equals("考试")) { 266 System.out.println(list1.get(i).getValue().name + " " + courseAverage(list1.get(i).getValue().name, list1.get(i).getValue())); 267 // } 268 // if (list1.get(i).getValue().exam.equals("考察")) 269 // System.out.println(list1.get(i).getValue().name + " " + courseAverage(list1.get(i).getValue().name, list1.get(i).getValue())); 270 // if (list1.get(i).getValue().exam.equals("实验")) 271 // System.out.println(list1.get(i).getValue().name + " " + courseAverage(list1.get(i).getValue().name, list1.get(i).getValue())); 272 // 273 } 274 else { 275 System.out.println(list1.get(i).getValue().name + " has no grades yet"); 276 } 277 } 278 for (int i = 0; i < list2.size(); i ++) { 279 if (list2.get(i).getValue().existScore()) 280 System.out.println(list2.get(i).getValue().list3.get(0).getValue().ID.substring(0, 6) + " " + gradeAverage(list2.get(i).getValue())); 281 else 282 System.out.println(list2.get(i).getValue().list3.get(0).getValue().ID.substring(0, 6) + " has no grades yet"); 283 } 284 } 285 public void output(){ 286 System.out.println("wrong format"); 287 } 288 289 public boolean isExistCourseScore(String courseName){ 290 for (int i = 0; i < list2.size(); i ++){ 291 for (int j = 0; j < list2.get(i).getValue().list3.size(); j ++) { 292 if (list2.get(i).getValue().list3.get(j).getValue().inform.finalGrade != -1 || list2.get(i).getValue().list3.get(j).getValue().inform.testGrade != -1) { 293 if (list2.get(i).getValue().list3.get(j).getValue().inform.name.equals(courseName)) { 294 return true; 295 } 296 } 297 } 298 } 299 return false; 300 } 301 public int gradeAverage(Class cla) { 302 double sum1 = 0; 303 double index = 0; 304 for (int i = 0; i < cla.list3.size(); i++) { 305 if (cla.list3.get(i).getValue().sum != -1) { 306 sum1 += cla.list3.get(i).getValue().sum; 307 index++; 308 } 309 } 310 return (int) (sum1 / index); 311 } 312 313 public int courseAverage(String courseName, Course course) { 314 double sum1 = 0; 315 int index = 0; 316 if (course.exam.equals("考试")) { 317 for (int i = 0; i < list2.size(); i ++){ 318 for (int j = 0; j < list2.get(i).getValue().list3.size(); j++) { 319 if (list2.get(i).getValue().list3.get(j).getValue().inform.name.equals(courseName) && list2.get(i).getValue().list3.get(j).getValue().sum != -1) { 320 sum1 += list2.get(i).getValue().list3.get(j).getValue().sum; 321 index++; 322 } 323 } 324 } 325 } 326 else if (course.exam.equals("考察")) { 327 for (int i = 0; i < list2.size(); i ++){ 328 for (int j = 0; j < list2.get(i).getValue().list3.size(); j++) { 329 if (list2.get(i).getValue().list3.get(j).getValue().inform.name.equals(courseName) && list2.get(i).getValue().list3.get(j).getValue().sum != -1) { 330 sum1 += list2.get(i).getValue().list3.get(j).getValue().sum; 331 index++; 332 } 333 } 334 } 335 } 336 else { 337 for (int i = 0; i < list2.size(); i ++){ 338 for (int j = 0; j < list2.get(i).getValue().list3.size(); j++) { 339 if (list2.get(i).getValue().list3.get(j).getValue().inform.name.equals(courseName) && list2.get(i).getValue().list3.get(j).getValue().sum != -1) { 340 sum1 += list2.get(i).getValue().list3.get(j).getValue().sum; 341 index++; 342 } 343 } 344 } 345 } 346 if (index == 0) 347 return 0; 348 else 349 return (int) (sum1 / index); 350 } 351 352 } 353 }

三、总结
在这阶段作业中,我对于对象类的属性及自定义方法,及类与类间的关系有了进一步的了解。同时,我也更多的运用了上次的新知识,Set类、Map类以及List类等集合类。因此,我明白了使用它们的便利,例如Set类适用于对重复数据的筛查,Map类适用于对数据的统计,它两都能确保对象的唯一性等等。我明白了在一个程序中,当有一个核心点出错,那么无论其它点是否通过,这些测试点都会报错,在第八次作业时我已经感受过了。当时的我感觉很多测试点本应通过的,然而却并没有,但当我将平均分计算这部分写正确后,测试点由五个变为了十四个。我想实际工作时应该也是如此,在写程序时,首先应保证核心方法,核心逻辑等核心点正确,这样才不至于摸黑走路,处处碰壁。同时,我也找到了我的不足之处。在编写代码时,我常常会对某段不满意的代码做出修改后感觉还是不满意,然后修改回去,如此循环往复几次,以至于浪费了很多时间。此外,我发现每次写较复杂的代码时一定会出现很多次空指针问题。这些都是我要改进的地方。
在写代码时,我已经越来越常用正则表达式以及集合类了,然而我对其也只是略知一二而已,它们还有许多高效且便利的使用方法等着我去发现。因此,接下来我应该深入了解,进一步学习有用的知识。
此外,这次我还学会了对Comparator类的使用。
Comparator 的使用有两种方式:
Collections.sort(list,Comparator<T>);
list.sort(Comparator<T>);
其实主要是看 Comparator 接口的实现,重写里面的 compare 方法。代码如下:
//自定义排序1
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getId() - o2.getId();
}
});
compare(Student o1, Student o2) 方法的返回值跟 Comparable<> 接口中的 compareTo(Student o) 方法 返回值意思相同。另一种写法如下:
//自定义排序2
list.sort(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getId() - o2.getId();
}
});
还可以替换为lambda表达式,使代码更简洁

list就是通常意义上的可扩容数组。
Set是去重的可扩容数组。
Map是由 <K,V>构成的映射关系结构。
如果你只想用来记录数据:用list
如果你想去重,用Set
如果你想定义可以快速查找的结构:用map
Map的特性
# 无序
# Map 提供了三种Collection视图,允许以键集,值集和键-值映射关系集的形式来查看某个映射内容
# Map集合的key和value都可以是任何引用类型的数据。
# Map集合的key不允许重复,value允许重复。key和value之间存在单向一对一关系,即通过指定的键可以找到唯一,确定的value。
# Map集合中获取数据时,只要给出指定的key,就可以取出对应的value


HashMap
继承关系
HashMap 继承于AbstractMap,但没实现NavigableMap接口
因此,HashMap的内容是“键值对,但不保证次序”!
HashMap是什么
# 是一个Map集,使用HashMap要求添加的键类明确定义了hashCode()和equals()
# 底层实现是使用数组+链表+红黑树的方式
# HashMap有两个内部类Node和TreeNode,分别实现了Entry接口
基础的Node类是用来构建链表的,而TreeNode是用来构建红黑树
# HashMap适用于Map中插入、删除和定位元素
# HashMap 是 非线程安全
如何实现(主要靠明确定义了hashCode()和equals())
# 使用HashMap要求添加的键类明确定义了hashCode()和equals()
HashMap 在底层将 key-value 当成一个整体进行处理,这个整体就是一个 Entry 对象。
HashMap 底层采用一个 Entry[] 数组来保存所有的 key-value 对,当需要存储一个 Entry 对象时,会根据 hash 算法来决定其在数组中的存储位置,在根据 equals方法决定其在该数组位置上的链表中的存储位置;当需要取出一个 Entry 时,也会根据 hash算法找到其在数组中的存储位置,再根据 equals 方法从该位置上的链表中取出该 Entry
如何实现查询
根据key获取元素的流程根据源码大致可以归纳为以下几步:
1.根据key计算一个hash值。
2.根据hash值计算数组下标。
3.判断当前数组位置是否有Node,如果没有则直接返回null。
4.如果有Node,判断该Node是否满足要求,如果满足则直接返回该Node的value
5.如果不满足,判断当前的Node是否是TreeNode,如果是,说明该位置存储的是红黑树,用树获取节点的方式去获取指定节点。
6.如果不是TreeNode,说明该位置是链表,遍历链表获取指定节点。
如何实现插入
1.根据键值对的key计算hash值。
2.判断底层数组是否为null,如果为null,先初始化底层数组。
3.根据hash值和数组长度计算数组下标。
4.判断当前位置是否为空,为空则直接生成新节点放入该位置。
5.如果该位置不为空,则查看头结点是否满足key的要求,满足的话修改头结点的value值。
6.如果不满足,判断该节点是否为TreeNode,如果是说明为红黑树,用树的方式添加或修改节点。
7.如果不为TreeNode,则说明为链表,遍历链表判断是否存在指定key的节点,如果存在则修改value,不存在则新生成节点,并挂在链表的尾部。并判断插入新节点后的链表长度是否大于阈值(默认为8),如果是的话,将链表转换为红黑树。
8.判断插入新元素后的HashMap大小是否大于阈值,如果大于阈值对HashMap进行扩容