oop第三次题目集总结
一.前言:
Pta-7:本次题目集是菜单计价系统的另一个分支上的一次迭代,在原有功能的基础上对输入的特色菜分为川菜,晋菜和浙菜三类,相比于前几次菜单的迭代来说,这算比较明确方向的迭代,新增了一个关于口味度的计算,同时新增了用户信息,对于用户的名字和电话号码进行规范,使得和代码更加贴合实际。逻辑性变得更强。
Pta-8:本次pta为一个课程成绩统计程序。本题是第一次仅有类图的题目集,需要我们自己去从头到尾的编写,按照类的划分,对学生的信息,班级的信息,以及课程的进行分类,以及成绩的分类,进行类与类之间的聚合,并且在结束输入后对成绩按照一定的顺序进行输出,在输出成绩后要将课程名称进行排序,和同学讨论过后,要用到正则表达式进行判断。很考验整体逻辑以及思维能力。
Pta-9:本次题目集为一道统计java程序中关键词的出现次数的题,也是第一次使用map类进行题目练习,需要用到正则表达式对输入的格式进行规范,并且通过使用String类之中的replaceAll(),substring()方法,本次pta题目集还对代码行数有了更为明确的限制,让我们使用更简单,更高效的代码进行编写,锻炼思维逻辑能力。也对map的使用更为了解。
Pta-10:本次题目集主要的题目是以下几个:HashMap的检索和HashMap的排序,进行了对HsahMap类的练习,以及进行了对多态的训练,而第三题则是在课程成绩统计程序的基础上进行第一次迭代,我还是把他当成和菜单一样的题目信息判断,对输入的信息当成字符串,然后再进行切割,相较于第一次的题目,多了一个对于实验成绩的考察,对次数以及成绩有了要求。其他的几道题对hashmap进行练习,让我们对HashMap有了一个更为清晰的了解。
Pta-11:本次pta主要练习了ArrayList-排序还有Map类对数据的整理和排序,以及让我们自己尝试书写一个类似于stack的类,为课程成绩统计程序的在第二次的基础上修改了计算总成绩的方式,修改类结构,将成绩类的继承关系改为组合关系,成绩信息由课程成绩类和分项成绩类组成,课程成绩类组合分项成绩类,分项成绩类由成绩分值和权重两个属性构成。但是由于我前几次的代码并不是特别完美,最后一次的题也只能按照测试用例把那些改对。
二.设计与分析:
题目集7:
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Order[] order = new Order[10];
Menu menu = new Menu();
String[] count;
int flag;
int j = 0;
int end = 0;//当遇到end结束时
String[] list = new String[10];//记录不同菜系判断口味度需要, 川菜的
String[] list1 = new String[10];//记录不同菜系判断口味度需要, 晋菜的
String[] list2 = new String[10];//记录不同菜系判断口味度需要, 浙菜的
int[] num = new int[20];
/*
记录每道菜菜系的不同
* 用0代表普通菜
* 用1代表川菜
* 用2代表晋菜
* 用3代表浙菜
*/
int[] number = new int[20];//计算有效点单
int[] table_num = new int[10];//记录桌号的数组
int flag1 = 0;//记录有效菜单的个数
int flag2 = 0;//当点特色菜的的时候
int flag3 = 0;//记录符合特价菜时间的折扣的标记点
/*
* flag3 = 1代表是特价菜7折,代表是普通6折
* flag3 = 2代表是特价菜7折,代表是普通8折
* flag3 = 0代表是周末全价
* */
double[] sum = new double[10];//算单桌打折后总价
double[] beforesum = new double[10];//算单桌打折前总价
double[] spicy = new double[20];//辣口味度计算
double[] acidity = new double[20];//酸口味度计算
double[] sweetness = new double[20];//甜口味度计算
String[] show_spicy = {"不辣","微辣","稍辣","辣","很辣","爆辣"};
String[] show_acidity = {"不酸","微酸","稍酸","酸","很酸"};
String[] show_sweetness = {"不甜","微甜","稍甜","甜"};
int[] fenshu_spicy = new int[10];//计算辣菜的份数
int[] fenshu_acidity = new int[10];//计算酸菜的份数
int[] fenshu_sweetness = new int[10];//计算甜菜的份数
String[] name = new String[10];//记录客户名字
String[] telephone = new String[10];//记录客户电话
int k = 0;//桌子数
while (true) {
String str = sc.nextLine();
count = str.split(" ");
flag = count.length;
if (count[0].equals("end") && flag == 1) {
end = 1;
}
if (count[0].equals("table") && flag == 7) {
if (j > 0) {
k++;
//计算最终的口味度,除以总份数
}
j++;
sum[j-1] = 0;
order[j-1] = new Order();
number[j-1] = 0;
} //用来判断是不是订单和菜单混合的标记点
if (j > 0) {
// if (flag == 2) { //删除菜品记录
// if (count[1].equals("delete")) {
// order[j-1].delARecordByOrderNum(Integer.parseInt(count[0]));
// } else {
// System.out.println("invalid dish");
//
// }
// }
if (flag == 7) { //桌子信息的输入
if (count[0].equals("table")) {
int len = count[3].length();//计算客户姓名的长度
int len1 = count[4].length();//计算客户电话的长度
int b = 0;//是否桌子信息正确
if (count[1].charAt(0) == '0' || !(count[1].charAt(0) >= '1'
&& count[1].charAt(0) <= '5')) {
System.out.println("wrong format");
b = 1;
} //桌号正常
if (len > 10) { //客户姓名不能超过10位
System.out.println("wrong format");
b = 1;
}
if (len1 != 11) { //客户电话不能超过11位
boolean tel;
tel = count[4].matches("(181|180|189|133|135|136)\\d{8}");
if (!tel) {
System.out.println("wrong format");
b = 1;
}
}
if(b == 1){
return;
}
name[j - 1] = count[3];
telephone[j - 1] = count[4];
table_num[j-1] = Integer.parseInt(count[1]);
String[] arr = count[5].split("/");//将年分割开来
String[] arr1 = count[6].split("/");//将时间分割开来
int year = Integer.parseInt(arr[0]);
int month = Integer.parseInt(arr[1]);
int day = Integer.parseInt(arr[2]);//将日期分开
int hh = Integer.parseInt(arr1[0]);
int mm = Integer.parseInt(arr1[1]);
int ss = Integer.parseInt(arr1[2]);//将时间分开
if (arr1[1].length() > 2 || arr1[2].length() > 2) {
System.out.println("wrong format");
return;
}
int c = 0;//判断营业时间正确的标记点
if (month > 12 || day > 31 || year < 1000 || year > 9999) {
c = 2;
} else {
if (year < 2022 || year > 2023) {
c = 3;
} else {
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, day - 1);
if ((cal.get(Calendar.DAY_OF_WEEK) == 7)
|| (cal.get(Calendar.DAY_OF_WEEK) == 6)) {
if ((hh >= 10 && hh <= 20) || (hh == 9 && mm >= 30) || (hh == 21 && mm <= 29)
|| (hh == 21 && mm == 30 && ss == 0)) {
c = 1;
flag3 = 0;
} //周末全价时间判断
} else {
if (hh >= 17 && hh <= 19) {
flag3 = 2;
c = 1;
}
if (hh == 20 && mm <= 29) {
flag3 = 2;
c = 1;
}
if (hh == 20 && mm == 30 && ss == 0) {
flag3 = 2;
c = 1;
}
if ((hh >= 11 && hh <= 13) || (hh == 10 && mm >= 30) ||
(hh == 14 && mm <= 29)
|| (hh == 14 && mm == 30 && ss == 0)) {
flag3 = 1;
c = 1;
}
}
if (c == 0) {
System.out.println("table " + table_num[j - 1] + " out of opening hours");
return;
}
System.out.println("table " + table_num[j - 1] + ": ");//输出桌号信息
}
} //符合时间范围
if (c == 2) {
System.out.println(Integer.parseInt(count[1]) + " date error");
}
if (c == 3) {
System.out.println("not a valid time period");
}
}
}
if (flag == 4 || flag == 5 || flag == 6) { //点菜的记录
if (flag == 4) {
for (int i = 0; i < flag1; i++) {
if (count[1].equals(menu.dishs[i].name)) {
flag2 = 1;
break;
}
} //判断这个菜是否再菜单中有
if (flag2 == 1) { //当存在
order[j - 1].addARecord(Integer.parseInt(count[0]), count[1],
Integer.parseInt(count[2]), Integer.parseInt(count[3]), menu);
System.out.println(Integer.parseInt(count[0]) + " " +
count[1] + " " + order[j - 1].records[number[j - 1]].getPrice());
//输出单笔
if (flag3 == 1) {
sum[j - 1] = Math.round(sum[j - 1]) + 0.6 * order[j - 1].records[number[j - 1]].getPrice();
}
if (flag3 == 0) {
sum[j - 1] = Math.round(sum[j - 1]) + order[j - 1].records[number[j - 1]].getPrice();
}
if (flag3 == 2) {
sum[j - 1] = Math.round(sum[j - 1]) + 0.8 * order[j - 1].records[number[j - 1]].getPrice();
}
number[j - 1]++;
beforesum[j - 1] = order[j - 1].getTotalPrice();
}
}//点普通菜的记录
if(flag == 5) { //当点特色菜的的时候
int suitable_s = 1;//判断辣口味度是否合适
int suitable_a = 1;//判断酸口味度是否合适
int suitable_w = 1;//判断甜口味度是否合适
for (int i = 0; i < flag1; i++) {
if (count[1].equals(menu.dishs[i].name)) {
flag2 = 1;
break;
}
}
for (String s : list) {//从辣中判断
if (count[1].equals(s)) {
if ((Integer.parseInt(count[2]) < 0) || (Integer.parseInt(count[2]) > 5)) {
System.out.println("spicy num out of range :" + Integer.parseInt(count[2]));
suitable_s = 0;
}
else {
fenshu_spicy[j - 1] += Integer.parseInt(count[4]);
//list_s[number[j - 1]] = Integer.parseInt(count[2]);
spicy[k] += (Integer.parseInt(count[2]) * Integer.parseInt(count[4]));
}//记录对应菜系的口味度,放在不同的数组里,以用来计算最终口味度
}
}//判断这个菜是否再菜单中有
for (String s : list1) {//从酸中判断
if (count[1].equals(s)) {
if ((Integer.parseInt(count[2]) < 0) || (Integer.parseInt(count[2]) > 4)) {
System.out.println("acidity num out of range :" + Integer.parseInt(count[2]));
suitable_a = 0;
}else {
fenshu_acidity[j - 1] += Integer.parseInt(count[4]);
//list_a[number[j - 1]] = Integer.parseInt(count[2]);
acidity[k] += (Integer.parseInt(count[2]) * Integer.parseInt(count[4]));
}
//记录对应菜系的口味度,放在不同的数组里,以用来计算最终口味度
}
}//判断这个菜是否再菜单中有
for (String s : list2) {//从甜中判断
if (count[1].equals(s)) {
if ((Integer.parseInt(count[2]) < 0) || (Integer.parseInt(count[2]) > 3)) {
System.out.println("sweetness num out of range :" + Integer.parseInt(count[2]));
suitable_w = 0;
}
else {
fenshu_sweetness[j - 1] += Integer.parseInt(count[4]);
//list_w[number[j - 1]] = Integer.parseInt(count[2]);
sweetness[k] += (Integer.parseInt(count[2]) * Integer.parseInt(count[4]));
//记录对应菜系的口味度,放在不同的数组里,以用来计算最终口味度
}
}
}//判断这个菜是否再菜单中有
if (flag2 == 1 && suitable_a == 1 && suitable_s == 1 && suitable_w == 1) { //当存在
order[j - 1].addARecord(Integer.parseInt(count[0]), count[1],
Integer.parseInt(count[3]), Integer.parseInt(count[4]), menu);
System.out.println(Integer.parseInt(count[0]) + " " +
count[1] + " " + order[j - 1].records[number[j - 1]].getPrice());
//输出单笔
if (flag3 == 1) {
sum[j - 1] = Math.round(sum[j - 1]) + 0.7 * order[j - 1].records[number[j - 1]].getPrice();
}
if (flag3 == 0) {
sum[j - 1] = Math.round(sum[j - 1]) + order[j - 1].records[number[j - 1]].getPrice();
}
if (flag3 == 2) {
sum[j - 1] = Math.round(sum[j - 1]) + 0.7 * order[j - 1].records[number[j - 1]].getPrice();
}
number[j - 1]++;
beforesum[j - 1] = order[j - 1].getTotalPrice();
}
}
if(flag == 6){
int suitable_s = 1;//判断辣口味度是否合适
int suitable_a = 1;//判断酸口味度是否合适
int suitable_w = 1;//判断甜口味度是否合适
for (int i = 0; i < flag1; i++) {
if (count[2].equals(menu.dishs[i].name)) {
flag2 = 1;
break;
}
}
for (String s : list) {//从辣中判断
if (count[2].equals(s)) {
if ((Integer.parseInt(count[3]) < 0) || (Integer.parseInt(count[3]) > 5)) {
System.out.println("spicy num out of range :" + Integer.parseInt(count[3]));
suitable_s = 0;
}
else {
fenshu_spicy[Integer.parseInt(count[0]) - 1] += Integer.parseInt(count[5]);
//list_s[number[j - 1]] = Integer.parseInt(count[2]);
spicy[Integer.parseInt(count[0]) - 1] += (Integer.parseInt(count[3]) * Integer.parseInt(count[5]));
}//记录对应菜系的口味度,放在不同的数组里,以用来计算最终口味度
}
}//判断这个菜是否再菜单中有
for (String s : list1) {//从酸中判断
if (count[2].equals(s)) {
if ((Integer.parseInt(count[3]) < 0) || (Integer.parseInt(count[3]) > 4)) {
System.out.println("acidity num out of range :" + Integer.parseInt(count[3]));
suitable_a = 0;
}else {
fenshu_acidity[Integer.parseInt(count[0]) - 1] += Integer.parseInt(count[5]);
//list_a[number[j - 1]] = Integer.parseInt(count[2]);
acidity[Integer.parseInt(count[0]) - 1] += (Integer.parseInt(count[3]) * Integer.parseInt(count[5]));
}
//记录对应菜系的口味度,放在不同的数组里,以用来计算最终口味度
}
}//判断这个菜是否再菜单中有
for (String s : list2) {//从甜中判断
if (count[2].equals(s)) {
if ((Integer.parseInt(count[3]) < 0) || (Integer.parseInt(count[3]) > 3)) {
System.out.println("sweetness num out of range :" + Integer.parseInt(count[3]));
suitable_w = 0;
}
else {
fenshu_sweetness[Integer.parseInt(count[0]) - 1] += Integer.parseInt(count[5]);
//list_w[number[j - 1]] = Integer.parseInt(count[2]);
sweetness[Integer.parseInt(count[0]) - 1] += (Integer.parseInt(count[3]) * Integer.parseInt(count[5]));
//记录对应菜系的口味度,放在不同的数组里,以用来计算最终口味度
}
}
}//判断这个菜是否再菜单中有
if (flag2 == 1 && suitable_a == 1 && suitable_s == 1 && suitable_w == 1) { //当存在
order[j - 1].addARecord(Integer.parseInt(count[1]), count[2],
Integer.parseInt(count[4]), Integer.parseInt(count[5]), menu);
System.out.println(Integer.parseInt(count[1]) + " " +
"table" + " " + j + " pay for table " + Integer.parseInt(count[0]) + " " + order[j - 1].records[number[j - 1]].getPrice());
//输出单笔
if (flag3 == 1) {
sum[j - 1] = Math.round(sum[j - 1]) + 0.7 * order[j - 1].records[number[j - 1]].getPrice();
}
if (flag3 == 0) {
sum[j - 1] = Math.round(sum[j - 1]) + order[j - 1].records[number[j - 1]].getPrice();
}
if (flag3 == 2) {
sum[j - 1] = Math.round(sum[j - 1]) + 0.7 * order[j - 1].records[number[j - 1]].getPrice();
}
number[j - 1]++;
beforesum[j - 1] = order[j - 1].getTotalPrice();
}
}
if (flag2 == 0) { //当菜名不存在时
System.out.println(count[1] + " does not exist");
}
flag2 = 0;
}
int delete = -1;
if(flag == 2 && count[1].equals("delete")) {//删除的记录
for (int i = 0; i < number[j - 1]; i++) {
if (Integer.parseInt(count[0]) == order[j - 1].records[i].orderNum) {
delete = 0;
beforesum[j - 1] -= order[j - 1].records[i].getPrice();
int real_s = 1;
int real_a = 1;
int real_w = 1;
for (String s : list) {//从辣中判断
if (count[1].equals(s)) {
real_s = 0;
break;
}
}
for (String s : list1) {//从酸中判断
if (count[1].equals(s)){
real_a = 0;
break;
}
}
for (String s : list2) {//从甜中判断
if (count[1].equals(s)){
real_w = 0;
break;
}
}
if ( real_s == 1 && real_a == 1 && real_w == 1){
if (flag3 == 1) {
sum[j - 1] = Math.round(sum[j - 1]) - 0.6 * order[j - 1].records[i].getPrice();
}
if (flag3 == 0) {
sum[j - 1] = Math.round(sum[j - 1]) - order[j - 1].records[i].getPrice();
}
if (flag3 == 2) {
sum[j - 1] = Math.round(sum[j - 1]) - 0.8 * order[j - 1].records[i].getPrice();
}
}else {
if (flag3 == 1) {
sum[j - 1] = Math.round(sum[j - 1]) - 0.7 * order[j - 1].records[i].getPrice();
}
if (flag3 == 0) {
sum[j - 1] = Math.round(sum[j - 1]) - order[j - 1].records[i].getPrice();
}
if (flag3 == 2) {
sum[j - 1] = Math.round(sum[j - 1]) - 0.7 * order[j - 1].records[i].getPrice();
}
}
break;
}
}
if(delete == -1) {
System.out.println("delete error");
}
delete = -1;
}
} else { //菜品录入
if (flag == 2) {
int f2 = 0;//用来判断是否菜品价格为整数
int len1 = count[1].length();
for (int i = 0; i < len1; i++) {
if (!Character.isDigit(count[1].charAt(i))) {
System.out.println("wrong format");
f2 = 1;
break;
}
}
if (f2 == 0) {
if (Integer.parseInt(count[1]) > 300 || Integer.parseInt(count[1]) < 0) {
System.out.println(count[0] + " price out of range " + Integer.parseInt(count[1]));
} else {
menu.addDish(count[0], Integer.parseInt(count[1]));
num[flag1] = 0;
//如果是普通菜就赋值0,代表
flag1++;
}
}
}
if(flag == 3){
System.out.println("wrong format");
}
if (flag == 4) { //特价菜的菜单录入
if (count[3].equals("T")) {
menu.addDish(count[0], Integer.parseInt(count[2]));
if (count[1].equals("川菜")) {
list[flag1] = count[0];//如果是川菜就赋值,代表川菜
num[flag1] = 1;
flag1++;
}
if (count[1].equals("晋菜")) {
list1[flag1] = count[0];//如果是晋菜就赋值,代表晋菜
num[flag1] = 2;
flag1++;
}
if (count[1].equals("浙菜")) {
list2[flag1] = count[0];//如果是浙菜就赋值,代表浙菜
num[flag1] = 3;
flag1++;
}//记录不同菜系判断口味度需要
}
}
}
if(end == 1){
k++;
break;
}
}
//最终的输出
for(int i = 0; i < k ; i++) { //依次输出每一桌
spicy[i] = Math.round(spicy[i] / fenshu_spicy[i]);
acidity[i] = Math.round(acidity[i] / fenshu_acidity[i]);
sweetness[i] = Math.round(sweetness[i] / fenshu_sweetness[i]);
//每个桌子结束后,计算口味度
if(fenshu_spicy[i] != 0 && fenshu_acidity[i] == 0 && fenshu_sweetness[i] == 0) {//只有川菜
System.out.println("table " + table_num[i] + ": " + Math.round(beforesum[i]) + " "
+ Math.round(sum[i]) + " 川菜 " + fenshu_spicy[i] + " " + show_spicy[(int) spicy[i]]);
}
if(fenshu_spicy[i] == 0 && fenshu_acidity[i] != 0 && fenshu_sweetness[i] == 0) {//只有晋菜
System.out.println("table " + table_num[i] + ": " + Math.round(beforesum[i]) + " "
+ Math.round(sum[i]) + " 晋菜 " + fenshu_acidity[i] + " " + show_acidity[(int) acidity[i]]);
}
if(fenshu_spicy[i] == 0 && fenshu_acidity[i] == 0 && fenshu_sweetness[i] != 0) {//只有浙菜
System.out.println("table " + table_num[i] + ": " + Math.round(beforesum[i]) + " "
+ Math.round(sum[i]) + " 浙菜 " + fenshu_sweetness[i] + " " + show_sweetness[(int) sweetness[i]]);
}
if(fenshu_spicy[i] != 0 && fenshu_acidity[i] != 0 && fenshu_sweetness[i] == 0) { //没有浙菜
System.out.println("table " + table_num[i] + ": " + Math.round(beforesum[i]) + " "
+ Math.round(sum[i]) + " 川菜 " + fenshu_spicy[i] + " " + show_spicy[(int) spicy[i]]
+ " 晋菜 " + fenshu_acidity[i] + " " + show_acidity[(int) acidity[i]]);
}
if(fenshu_spicy[i] != 0 && fenshu_acidity[i] == 0 && fenshu_sweetness[i] != 0) {//没有晋菜
System.out.println("table " + table_num[i] + ": " + Math.round(beforesum[i]) + " "
+ Math.round(sum[i]) + " 川菜 " + fenshu_spicy[i] + " " + show_spicy[(int) spicy[i]]
+ " 浙菜 " + fenshu_sweetness[i] + " " + show_sweetness[(int) sweetness[i]]);
}
if(fenshu_spicy[i] == 0 && fenshu_acidity[i] != 0 && fenshu_sweetness[i] != 0) {//没有川菜
System.out.println("table " + table_num[i] + ": " + Math.round(beforesum[i]) + " "
+ Math.round(sum[i]) + " 晋菜 " + fenshu_acidity[i] + " " + show_acidity[(int) acidity[i]]
+ " 浙菜 " + fenshu_sweetness[i] + " " + show_sweetness[(int) sweetness[i]]);
}
if(fenshu_spicy[i] != 0 && fenshu_acidity[i] != 0 && fenshu_sweetness[i] != 0) {//都有
System.out.println("table " + table_num[i] + ": " + Math.round(beforesum[i]) + " "
+ Math.round(sum[i]) + " 川菜 " + fenshu_spicy[i] + " " + show_spicy[(int) spicy[i]]
+ " 晋菜 " + fenshu_acidity[i] + " " + show_acidity[(int) acidity[i]]
+ " 浙菜 " + fenshu_sweetness[i] + " " + show_sweetness[(int) sweetness[i]]);
}
if(fenshu_spicy[i] == 0 && fenshu_acidity[i] == 0 && fenshu_sweetness[i] == 0) {//没有川菜
System.out.println("table " + table_num[i] + ": " + Math.round(beforesum[i]) + " "
+ Math.round(sum[i]) + " ");
}
}
if(j == 1) {
System.out.println(name[0] + " " + telephone[0] + " " + Math.round(sum[j - 1]));
}
else {
for (int i = 1; i < j; i++) {
if (name[i].equals(name[i - 1])) {
sum[i] += sum[i - 1];
sum[i - 1] = 0;
}
}
int change_name = 0;
String name1;//中介类
String tele1;//中介类
double sum1;//中介类
for (int i = 1; i < j; i++) {
if (!name[i].equals(name[i - 1])) {
int m;
int l1 = name[i].length();
int l2 = name[i - 1].length();
m = Math.min(l1, l2);
for (int n = 0; n < m; n++) {
if (name[i - 1].charAt(n) > name[i].charAt(n)) {
change_name = i;
break;
}
}
if(change_name == i) {
name1 = name[i - 1];
name[i - 1] = name[i];
name[i] = name1;
tele1 = telephone[i - 1];
telephone[i - 1] = telephone[i];
telephone[i] = tele1;
sum1 = sum[i - 1];
sum[i - 1] = sum[i];
sum[i] = sum1;
}
change_name = 0;
}
}
for (int i = 0; i < j; i++) {
if (sum[i] != 0) {
System.out.println(name[i] + " " + telephone[i] + " " + Math.round(sum[i]));
}
}
}
}
}
这是这道题的分析:

类图:

由此可见代码冗余性比较高,还需要改进。
题目集8:
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 public class Main { 4 public static void main(String[] args) { 5 Scanner sc = new Scanner(System.in); 6 CourseSelection list = new CourseSelection(); 7 int flag;//结点,记录每一个字符串的长度 8 int student_count = 0;//记录学生数量 9 String[] count; 10 int[] average_u = {0, 0, 0, 0, 0};//平时成绩 11 int[] average_e = {0, 0, 0, 0, 0};//期末成绩 12 int[] average_a = {0, 0, 0, 0, 0};//最终成绩 13 ArrayList<Integer> list_s = new ArrayList<>(); 14 ArrayList<Integer> list_c = new ArrayList<>(); 15 while (true) { 16 String s = sc.nextLine(); 17 count = s.split(" "); 18 flag = count.length; 19 if (flag == 1 && count[0].equals("end")) { 20 if (student_count == 0) { 21 for (int i = 0; i < list.count; i++) { 22 System.out.println(list.courses[i].name + " has no grades yet");} 23 System.exit(0);} 24 for (int i = 0; i < list.count; i++) { 25 if (list.courses[i].mode.equals("错误")) 26 System.out.println(list.courses[i].name + " does not exist");} 27 break;} 28 if (flag == 3 || (flag == 2 && count[1].equals("必修"))) {//添加课程的时候 29 int k = 0; 30 String str = null; 31 if(flag == 3){str = count[2]; 32 if(!(count[0].length()<=10&&(count[1].equals("必修")||count[1].equals("选修"))&&(count[2].equals("考试")||count[2].equals("考察")))){ 33 System.out.println("wrong format"); 34 k = 1;}} 35 if(flag == 2) 36 str = "考试"; 37 if(k == 0){ 38 if (count[1].equals("必修") && str.equals("考察")) { 39 System.out.println(count[0] + " : course type & access mode mismatch"); 40 list.add(count[0], count[1], "错误"); 41 } else { 42 list.add(count[0], count[1], str);}} 43 } else {//在课程录入之后 44 int cid = Integer.parseInt(count[0].substring(0, 6)); 45 student_count++; 46 if (flag == 5) {//单个学生的某一科成绩,考试课成绩 47 // int k = 0; for (int i = 0; i < list.count; i++) { 48 // if(!list.courses[i].name.equals(count[2])) 49 // k++; 50 // } 51 // if(k == list.count) { 52 // System.out.println(count[0] + " " + count[1] + " " + count[2] + " " + "does not exist"); 53 // } 54 try{ 55 //list.login(Integer.parseInt(count[0]), count[1], cid, -100, -100, count[2]); 56 57 for (int i = 0; i < list.count; i++) { 58 if (list.courses[i].name.equals(count[2]) && list.courses[i].mode.equals("考试")) { 59 if (Integer.parseInt(count[3]) >= 0 &&Integer.parseInt(count[3]) <= 100 && Integer.parseInt(count[4]) >= 0 && 60 Integer.parseInt(count[4]) <= 100 && count[0].length() == 8 && count[1].length() <= 10 && count[2].length() <= 10) { 61 list.login(Integer.parseInt(count[0]), count[1], cid, Integer.parseInt(count[3]), Integer.parseInt(count[4]), count[2]); 62 list_s.add(0); 63 list.courses[i].scores[student_count - 1] = ((int) (Integer.parseInt(count[3]) * 0.3 + Integer.parseInt(count[4]) * 0.7)); 64 } else { 65 System.out.println("wrong format"); 66 student_count--;}} 67 if (list.courses[i].name.equals(count[2]) && !(list.courses[i].mode.equals("考试"))) { 68 list.login(Integer.parseInt(count[0]), count[1], cid, -1, -1, count[2]); 69 list.courses[i].scores[student_count - 1] = 0;//赋值代表该生成绩有误 70 if (list.courses[i].mode.equals("考察")) { 71 list_s.add(1);} 72 list_s.add(0);}} 73 }catch (Exception e){ 74 System.out.println("wrong format");student_count--;}} 75 if (flag == 4) {//单个学生考察课成绩 76 // int k = 0; 77 // for (int i = 0; i < list.count; i++) { 78 // if(!list.courses[i].name.equals(count[2])) 79 // k++; 80 // } 81 // if(k == list.count) { 82 // System.out.println(count[0] + " " + count[1] + " " + count[2] + " " + "does not exist"); 83 // } 84 try { 85 86 //list.login(Integer.parseInt(count[0]), count[1], cid, 0, -100, count[2]); 87 for (int i = 0; i < list.count; i++) { 88 if (list.courses[i].name.equals(count[2]) && list.courses[i].mode.equals("考察")) { 89 if (Integer.parseInt(count[3]) >= 0 && Integer.parseInt(count[3]) <= 100 && count[0].length() == 8 && count[1].length() <= 10 && count[2].length() <= 10) { 90 list.login(Integer.parseInt(count[0]), count[1], cid, 0, Integer.parseInt(count[3]), count[2]); 91 list_s.add(0); 92 list.courses[i].scores[student_count - 1] = Integer.parseInt(count[3]); 93 } else { 94 System.out.println("wrong format"); 95 student_count--;}} 96 if (list.courses[i].name.equals(count[2]) && !(list.courses[i].mode.equals("考察"))) { 97 list.login(Integer.parseInt(count[0]), count[1], cid, 0, -1, count[2]); 98 list.courses[i].scores[student_count - 1] = 0;//赋值代表该生成绩有误 99 if (list.courses[i].mode.equals("考试")) { 100 list_s.add(1);} 101 list_s.add(0);}} 102 }catch (Exception e){System.out.println("wrong format");student_count--;}} 103 }} 104 //输出学生信息 105 for (int i = 0; i <= list.cid_count; i++) {//先把班级按顺序调整好 106 for (int j = 1; j <= list.cid_count; j++) { 107 if (list.class1[i].student[0].cid > list.class1[j].student[0].cid) { 108 Class class1 = list.class1[i]; 109 list.class1[i] = list.class1[j]; 110 list.class1[j] = class1;}}} 111 for (int k = 0; k <= list.cid_count; k++) { 112 ArrayList<Integer> num = new ArrayList<>(); 113 for (int i = 0; i < list.s_count[k]; i++) { 114 num.add(list.class1[k].student[i].sid);} 115 sort(num); 116 for (int j = 0; j <= list.cid_count; j++) { 117 for (int i = 0; i < list.s_count[k]; i++) { 118 if (list.class1[j].student[i].average_scores == 0) { 119 continue;} 120 int num1 = 1; //记录相同学号的个数 121 for (int l = i + 1; l < list.s_count[k]; l++) { 122 if (list.class1[j].student[i].sid == list.class1[j].student[l].sid) { 123 list.class1[j].student[i].average_scores = list.class1[j].student[l].average_scores + list.class1[j].student[i].average_scores; 124 list.class1[j].student[l].average_scores = 0; 125 num1++;}} 126 list.class1[j].student[i].average_scores = list.class1[j].student[i].average_scores / num1;}} 127 for (int i = 0; i < num.size(); i++) { 128 for (int j = 0; j < list.s_count[k]; j++) { 129 if (num.get(i) == list.class1[k].student[j].sid && list.class1[k].student[i].average_scores != 0 && list.class1[k].student[j].average_scores != 0) { 130 if (list.class1[k].student[j].average_scores > 0) { 131 System.out.println(num.get(i) + " " + list.class1[k].student[j].name + " " + list.class1[k].student[j].average_scores); 132 } else { 133 if(list_s.get(i) == 1) 134 System.out.println(num.get(i) + " " + list.class1[k].student[j].name + " : access mode mismatch"); 135 System.out.println(num.get(i) + " " + list.class1[k].student[j].name + " did not take any exams");}}}}} 136 //课程的输出 137 for (int j = 0; j < list.count; j++) { 138 for (int l = 0, k = 0; l < list.count - 1; l++) { 139 if (list.courses[l].name.compareTo(list.courses[l + 1].name) < 0 140 && !list.courses[l + 1].name.matches("[a-zA-Z]+") && !list.courses[l].name.matches("[a-zA-Z]+")) { 141 k = 1;} 142 if (list.courses[l + 1].name.matches("[a-zA-Z]+")) { 143 k = 1;} 144 if (k == 1) { 145 Course course = list.courses[l]; 146 list.courses[l] = list.courses[l + 1]; 147 list.courses[l + 1] = course;}}} 148 //输出课程信息 149 for (int i = 0; i < list.count; i++) { 150 if (!list.courses[i].mode.equals("错误")) { 151 int k1 = 0;//计算一个不为0的平时成绩个数 152 int k2 = 0;//计算一个不为0的期末成绩个数 153 int k3 = 0;//计算一个不为0的最终成绩个数 154 if (list.courses[i].mode.equals("考试")) { 155 for (int j = 0; j < student_count; j++) { 156 if (list.courses[i].usuallyscores[j] != 0) { 157 average_u[i] += list.courses[i].usuallyscores[j]; 158 k1++;} 159 if (k1 != 0) { 160 average_u[i] = average_u[i] / k1;} 161 if (list.courses[i].endscores[j] != 0) { 162 average_e[i] += list.courses[i].endscores[j]; 163 k2++;}} 164 if (k2 != 0) { 165 average_e[i] = average_e[i] / k2;}} 166 for (int j = 0; j < student_count; j++) { 167 if (list.courses[i].scores[j] != 0) { 168 average_a[i] += list.courses[i].scores[j]; 169 k3++;}} 170 if (k3 != 0) { 171 average_a[i] = average_a[i] / k3;} 172 if (average_a[i] > 0) {//当这门课有成绩的时候 173 if (list.courses[i].mode.equals("考试")) { 174 System.out.println(list.courses[i].name + " " + average_u[i] + " " + average_e[i] + " " + average_a[i]);} else { 175 System.out.println(list.courses[i].name + " " + average_a[i] + " " + average_a[i]);}} else { 176 System.out.println(list.courses[i].name + " has no grades yet");}}} 177 178 //班级成绩信息 179 for (int i = 0; i <= list.cid_count; i++) { 180 int sum = 0; 181 int num3 = 0;//计算平均值 182 for (int j = 0; j < list.s_count[i]; j++) { 183 if (list.class1[i].student[j].average_scores != 0) { 184 sum += list.class1[i].student[j].average_scores; 185 num3++;}} 186 if (sum > 0) { 187 sum = sum / num3; 188 System.out.println(list.class1[i].student[0].cid + " " + sum);} else { 189 System.out.println(list.class1[i].student[0].cid + " has no grades yet");}}} 190 public static void sort(ArrayList<Integer> num){ 191 for (int i = 0; i < num.size(); i++){ 192 for(int j = 0;j < num.size() - 1; j++){ 193 if(num.get(j) > num.get(j + 1)){ 194 int num_flag; 195 num_flag = num.get(j); 196 num.set(j, num.get(j + 1)); 197 num.set(j + 1,num_flag);}}}}} 198 199 class Class { 200 Student[] student = new Student[10]; 201 public Class() { 202 } 203 } 204 class Course { 205 public String name;//课程名字 206 public String nature ;//课程性质 207 public String mode;//考核模式 208 209 int[] scores = {0,0,0,0,0};//存放该科目的最终数组; 210 211 public int[] usuallyscores = {0,0,0,0}; 212 public int[] endscores= {0,0,0,0}; 213 214 public Course(String name, String nature, String mode) { 215 this.name = name; 216 this.nature = nature; 217 this.mode = mode; 218 } 219 } class CourseSelection { 220 Course[] courses = new Course[10]; 221 Class[] class1 = new Class[10];//班级数 222 // Grades score; 223 224 int count = 0;//记录课程数量 225 public int[] s_count = new int[]{0,0};//记录每班有效成绩记录的数量 226 int flag = -1;//记录重复数据对应在数组的值 227 int cid_count = 0;//记录班级的数量 228 int cid_countnum = 0;//记录班级的数量 229 int[] m = {0,0,0,0};//每门课的成绩个数 230 231 public CourseSelection() { 232 } 233 Course find(String name){ 234 for(int i = 0; i < count; i++) { 235 if (name.equals(courses[i].name)) { 236 flag = i; 237 return courses[i]; 238 } 239 } 240 return null; 241 }//查找是否有重复 242 243 void add(String name, String nature, String mode) { 244 if(count != 0 && find(name) != null){ 245 return; 246 } 247 courses[count] = new Course( name, nature, mode); 248 courses[count].name = name; 249 courses[count].nature = (nature); 250 courses[count].mode = (mode); 251 count++; 252 flag = -1; 253 }//加一个课程 254 255 void login(int sid, String name, int cid, int scroes1, int scroes2, String c_name){ 256 int j = 0;//标记一共是跟几个班的班号不一致,判断是不是新的班 257 boolean repeat = false; 258 if(!repeat) { 259 if (cid_count == 0 && s_count[0] == 0) { 260 class1[cid_count] = new Class(); 261 class1[cid_count].student[s_count[cid_count]] = new Student(); 262 } 263 264 if(cid_count != 0 && s_count[cid_count] != 0) { 265 for (int i = 0; i <= cid_count; i++) { 266 if (cid != class1[i].student[0].cid) { 267 j++; 268 } 269 if (cid == class1[i].student[0].cid) { 270 class1[i].student[s_count[cid_count]] = new Student(); 271 cid_count = i; 272 } 273 } 274 } 275 276 if(cid_count == 0 && s_count[0] != 0) { 277 if (cid != class1[cid_count].student[0].cid) { 278 j++; 279 } 280 if (cid == class1[cid_count].student[0].cid) { 281 class1[cid_count].student[s_count[cid_count]] = new Student(); 282 } 283 } 284 285 if (j == cid_count + 1) { 286 cid_count++; 287 cid_countnum = cid_count; 288 class1[cid_count] = new Class(); 289 // s_count[cid_count] = 290 class1[cid_count].student[s_count[cid_count]] = new Student(); 291 } 292 293 class1[cid_count].student[s_count[cid_count]].sid = sid; 294 class1[cid_count].student[s_count[cid_count]].name = name; 295 class1[cid_count].student[s_count[cid_count]].cid = cid; 296 297 if (scroes1 != 0) {//考试成绩录入 298 class1[cid_count].student[s_count[cid_count]].average_scores = (int) (scroes1 * 0.3 + scroes2 * 0.7); 299 for (int i = 0; i < count; i++) { 300 if (courses[i].name.equals(c_name)) { 301 courses[i].usuallyscores[m[i]] = scroes1; 302 courses[i].endscores[m[i]] = scroes2; 303 courses[i].scores[m[i]] = (int) (scroes1 * 0.3 + scroes2 * 0.7); 304 m[i]++; 305 } 306 } 307 } else {//考察成绩录入 308 class1[cid_count].student[s_count[cid_count]].average_scores = scroes2; 309 310 for (int i = 0; i < count; i++) { 311 if (courses[i].name.equals(c_name)) { 312 courses[i].scores[m[i]] = scroes2; 313 m[i]++; 314 } 315 316 } 317 } 318 s_count[cid_count]++; 319 } 320 cid_count = cid_countnum; 321 }//记录每一条数据的成绩 322 323 } 324 class Student { 325 public int sid;//学号 326 public String name;//名字 327 public int cid;//班号 328 public int average_scores;//平均成绩 329 330 public Student() { 331 } 332 }
由于一开始的行数限制,我一开始设计的所有的类都为私有属性,然后为了改进全部改为共有属性。
以下是第三次(最后一次的迭代)的类图以及度量图:


针对这道题目,我的思路如下:先把每一行输入按字符串输入进来,然后针对空格进行切分,根据空格的切分的多少来判断是属于什么类型的输入,有的是对科目的记录,有的是对成绩的统计,并且还有对结束的标记。
然后根据不同类型的内容判断,根据各个类型对数据的不同要求,进行正确与错误的判断,在存储信息的时候,我们可以根据不同的班号进行学生信息的划分以及,并且将该同学的成绩存入到当前课程的数组中去。
最后对所有的数据进行排序,先对班级进行排序,然后再根据班级里的学生列表,根据学生的学号进行排序,通过使用冒泡排序的方法,然后是对课程的排序,让我们通过对每个的字母的判断,确定一个顺序,再将对应的成绩输出,最后根据班级,把班级的成绩按照班号的顺序进行输出。这个题的迭代我认为是比较失败的,因为前期的构思不是很明确,后期的可操作空间不大,很难迭代。
题目集9:
我认为本题的核心如下:
- 使用正则表达式将代码中的注释内容替换为空格,实现注释的删除。
- 如果
str4中包含 "/" 字符串,则表示存在多行注释,需要继续判断之后的字符串是否出现 "/" 字符串,并将之后的字符串添加到str4中。 - 使用正则表达式将代码中的字符串内容替换为空,实现字符串的删除。
- 创建一个正则表达式模式
pattern,用于匹配关键字。 - 使用
matcher.find()循环匹配关键字。 - 在循环中,通过
matcher.group(1)获取匹配到的关键字,并将其出现次数加1。
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String[] keywords = {"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"};
Scanner sc = new Scanner(System.in);
int s = 0;//记录有效句子数量
Map<String, Integer> keyword_s = new TreeMap<>();
for (int i = 0; i < keywords.length; i++) {
keyword_s.put(keywords[i], 0);
}
String str1 = sc.nextLine();
while (!str1.equals("exit")) {
s++;
String str4 = str1;
str4 = str4.replaceAll("//.*|/\\*((.|\\n)(?!=*/))+\\*/", " ");//去掉代码中的注释
if (str4.contains("/*") ) {//去掉多行注释,在不同行
str4 = str4.replaceAll("(/\\*.*?\\*/)|(/\\*.*)", "");//去掉注释
while (!(str1 = sc.nextLine()).contains("*/")) {//判断之后的字符串是否出现*/
str4 = "";//赋空
}
str4 = str4 + str1.substring(str1.indexOf("*/") + 2);//添加*/之后的字符
}
str4 = str4.replaceAll("(\".*?\")|('.*?')", "");//删除代码中的字符串,“”和‘’
Pattern pattern = Pattern.compile("\\b(" + String.join("|", keywords) + ")\\b");
Matcher matcher = pattern.matcher(str4);
while (matcher.find()){
keyword_s.put(matcher.group(1),keyword_s.get(matcher.group(1))+1);//返回匹配到的关键字
}
str1 = sc.nextLine();
}
if (s == 0)
System.out.print("Wrong Format");
else {
for (Map.Entry<String, Integer> entry : keyword_s.entrySet()) {
if (entry.getValue() > 0) {
if (entry.getKey().equals("null")) {
if (keyword_s.get("double") == 3)
continue;
}
System.out.println(entry.getValue() + "\t" + entry.getKey());
}
}
}
}
}
- 好处:整体逻辑结构清晰,能够实现统计关键字出现次数的功能。
- 可改进之处:缺乏对输入的代码格式的严格限制,可能导致结果不准确。例如,代码中可以存在多个句子以及嵌套的注释等情况。此外,对于多行注释的处理也可以进行优化。
- 代码使用了有意义的变量名,注释也较为完善,容易理解每个部分的功能。
- 缺少函数的封装,整个程序都写在
main方法中,导致代码难以扩展和复用。可以将不同的功能拆分成独立的方法,提高可读性和可维护性。循环中使用了正则表达式进行字符串替换和匹配操作,这些操作可能对性能产生影响。可以考虑使用更高效的算法或数据结构进行替代,或者优化正则表达式的使用。
这段代码在功能实现方面较好,但在代码结构、可读性、可维护性和性能方面仍有改进的空间。可以考虑对代码进行重构,将功能模块化,并针对输入的代码格式进行更严格的限制和处理。此外,还可以考虑使用更高效的算法和数据结构来提升性能。
三.踩坑心得:
对于课程成绩管理系统,首先我遇到了代码长度限制的问题,导致我一直会报代码长度超过限制,这就说明我本身的代码写的是复杂的,不具有简便性,同时我也认识到了写正则表达式的重要性,可以极大的简化代码,并且可以和try catch 完美的结合起来,会让我的代码更加符合常理,并且可以让我们逻辑能力有很大提升。
正则表达式和if条件语句的最大区别我认为是,正则表达式可以直接对所有数据直接的进行判断,if条件语句判断的还是比较细化的,很难用if语句去把所有的情况都判断出来,很可能会漏掉一些题目没有提到的情况,这个时候用正则表达式就能用更短的代码去更高效的执行代码,同时也更加精确。
通过对arraylist,map的使用,让我对集合有个大致的理解。让我见识到了什么是真正效率高的代码。
四.改进建议:
第一次按照课程数据去输出,无法针对前面字母的排序,比如比较c语言和java:

改进如下:通过改进这个正则表达式,使得课程名称能够按照顺序排列出来。

五.总结:
很难想象,这已经是java接近尾声的时候了。第三阶段可以说是无论从线上还是线下都是最为紧张和重要的环节了,通过第一和第二个环节的认识,我们也算是把java学到的知识真真正正的用到了实处。感受语言之间的不同,期间我们还分小组进行了javafx的翻转课堂教学,让我们通过自我学习的方式,去养成主动学习的意识,同时也培养了我们的小组团队意识,以及敢于突破自己的舒适圈,敢于站在讲台上,用自己学到的知识去传授給更多的人。
pta题目集也是在这个阶段最为密集的时候,通过不停的迭代以及堆栈的使用,让我们使我对代码书写的逻辑更加清晰,对相应程序需要设计出来相应功能的设计的能力更强,从一开始的手足无措到后来可以慢慢的学会自己处理问题,可以根据类图完成一些基本的代码,能够更加快速高效,而且还在这几次的题目集之中学习map对象的使用。
总的来说,java语言还要我们花更多的时间去学习,去不断的提高代码能力,改善自己的不足。
六.对JAVA课程的总结和建议:
教学理念:
java作为第一个作为软件工程专业学生的专业基础课,对于专业的了解和发展有着很深的意义。培养工程思维。
教学方法:
边讲边练的方法是很好的,同步的进行有助学生更好的加深理解,但是时间的安排上我认为有点少,针对当堂课程讲解的东西,当时就应该有一个练习,要有针对性的,如果是阶段性发pta题目集的话,有些知识可能会时间久远,大家容易遗忘,不太好加深记忆。
教学组织:
线上线下的同步开展对大部分同学是很好的,多方面的学习可以不容易产生厌恶感,但是,无法做到对每个人的合理看管,有一些不自觉地同学可能会对网络授课地效果使用不佳,导致出现知识点遗落。
教学过程:
pta题目集地驱动是很有必要的,通过pta题目集地练习,我们可以更好地理解,并且提升自己的编码能力,对于前几次pta还好,基本简单地题目是可以进行满分的,但是对于后面难度上升地题目,在初期建议可以把答案给出来,这样可以对比自己的代码,进行分析,了解自己的不足,以及那些构思地缺陷。
教学模式:
本门课程备有实验课时,实验的系列是很好的,让大家从头自己完成一个小游戏,但是,实验和课程的衔接,在时间上有着很大的问题,并不能够很好,及时的得到练习,希望可以改进一下实验的时间。进行一些难度的匹配,可能效果会更好。
浙公网安备 33010602011771号