一、前言
总结三次题目集的知识点、题量、难度等情况
第六次题目集开始了新的迭代系统,万恶的点菜系统终于结束了,取而代之的是课程成绩统计程序,虽说更换了迭代系统但是感觉换汤不换药,很多要用到的知识点和内容和菜单非常类似,甚至是比点菜系统要简单很多(听说是不让平时分那么难看),万事开头难,新一次的作业应该是耗费时间和精力最多的,但是好在老师给出了部分源代码,这次作业我个人感觉是三次里面最难写的一次,用了好几天才弄完整个框架。设计框架时看题目要求就可以想到需要使用继承与组合、Arraylist等知识点,因为需要重新敲整个代码所以只有一题,难度略大。
第七次题目集有4个题目,前面2个是关于hashmap的使用,最后一个是关于多态的复习,都比较简单,第三个就是课程成绩统计程序的迭代,主要有变动的知识点就是正则表达式的使用,正则表达式在处理格式这方面相当好用,非常简洁且高效,新增的实验课的要求可以说很完美的匹配的正则表达式的特性,感觉就是为其而生,4个题目题量一般,难度中等。
第八次题目集有5个题目,第一个是关于arraylist的排序,这个在课程成绩统计程序中也用到了,第三个考察的是array数组的方法运用,第四个题目有点涉及下学期学的数据结构里面的知识相当于一个过渡吧,第五个题目主要考察Object类的内容,第二个就是最后一次迭代,知识点几乎没变就是需要改变框架,题量稍多,难度较大。
二、设计与分析
对题目的提交源码进行分析
第六次题目集
7-1 课程成绩统计程序-1
7-1难度较大,代码如下
import java.text.Collator; import java.util.*; public class Main { static int population = 0; static int courseNum = 0; public static void main(String[] args) { Scanner in = new Scanner(System.in); Stu[] stu = new Stu[100]; for(int q=0;q<100;q++){ stu[q]=new Stu(); } Course[] courses = new Course[100]; for(int p=0;p<100;p++){ courses[p]=new Course(); } Class[] classes = new Class[100]; for(int s=0;s<100;s++){ classes[s]=new Class(); } int i = 0,j = 0; here: while (in.hasNextLine()) { // 输入结束标志是end String line = in.nextLine().trim(); if (line.equals("end")) break; String[] parts = line.split(" "); if(parts.length<2||parts.length>5){ System.out.println("wrong format"); continue; } if(parts.length == 2){ String name = parts[0]; String type = parts[1]; String mode = "考试"; if(name.length()>10){ System.out.println("wrong format"); continue; } for(int f=0;f<courseNum;f++){ if(name.equals(courses[f].name)){ continue here; } } if (!type.equals("必修") && !type.equals("选修")) { System.out.println("wrong format"); continue; } courses[i].name = name; courses[i].type = type; courses[i].mode = mode; courseNum++; i++; } if (parts.length == 3) { // 课程信息 String name = parts[0]; String type = parts[1]; String mode = parts[2]; if(name.length()>10){ System.out.println("wrong format"); continue; } for(int f=0;f<courseNum;f++){ if(name.equals(courses[f].name)){ continue here; } } if (!type.equals("必修") && !type.equals("选修")) { System.out.println("wrong format"); continue; } if (!mode.equals("考试") && !mode.equals("考察")) { System.out.println("wrong format"); continue; } if(type.equals("必修")&&mode.equals("考察")){ System.out.println(name+" "+": course type & access mode mismatch"); continue; } courses[i].name = name; courses[i].type = type; courses[i].mode = mode; courseNum++; i++; } if(parts.length == 5){ // 成绩信息 String id = parts[0]; if(id.length()>8) { System.out.println("wrong format"); continue; } String name = parts[1]; if(name.length()>10){ System.out.println("wrong format"); continue; } String courseName = parts[2]; Integer score1 = null; Integer score2 = null; for(int f=0;f<population;f++){ if(courseName.equals(stu[f].courseName) && id.equals(stu[f].id)&&(stu[f].grade2!=-1)){ continue here; } } try { score1 = Integer.parseInt(parts[3]); score2 = Integer.parseInt(parts[4]); } catch (NumberFormatException e) { System.out.println("wrong format"); continue; } // 判断成绩是否越界 if (score1 < 0 || score1 > 100 || score2 < 0 || score2 > 100) { System.out.println("wrong format"); continue; } stu[j].id = id; classes[Integer.parseInt(id.substring(4,6))].stuNum.add(stu[j]); classes[Integer.parseInt(id.substring(4,6))].classHao=id.substring(0,6); stu[j].name = name; stu[j].courseName = courseName; population++; int k=0; for(int kk=0;kk<courseNum;kk++){ if(courseName.equals(courses[kk].name)){ k++; } } if(k==0){ System.out.println(courseName+" does not exist"); j++; continue ; } for(int y=0;y<courseNum;y++){ if(courseName.equals(courses[y].name) && courses[y].mode.equals("考察")&&courses[y].type.equals("选修")) { System.out.println(stu[j].id+" "+stu[j].name+" "+": access mode mismatch"); j++; continue here; } } stu[j].grade1 = score1; stu[j].grade2 = score2; j++; for(int m = 0;m< courseNum;m++){ if(courses[m].name.equals(courseName)){ courses[m].grade1+=score1; courses[m].grade2+=score2; courses[m].grade3++; courses[m].flag++; } } for(int t=0;t<j-1;t++){ if(stu[j-1].id.equals(stu[t].id)&&stu[t].grade1==-1&&stu[t].grade2==-1){ stu[t].grade1=score1; stu[t].grade2=score2; } } } if(parts.length == 4){ String id = parts[0]; if(id.length()>8){ System.out.println("wrong format"); continue; } String name = parts[1]; if(name.length()>10){ System.out.println("wrong format"); continue; } String courseName = parts[2]; Integer score2 = null; for(int f=0;f<population;f++){ if(courseName.equals(stu[f].courseName) && id.equals(stu[f].id)&&(stu[f].grade2!=-1)){ continue here; } } try { score2 = Integer.parseInt(parts[3]); } catch (NumberFormatException e) { System.out.println("wrong format"); continue; } // 判断成绩是否越界 if (score2 < 0 || score2 > 100 ) { System.out.println("wrong format"); continue; } stu[j].id = id; classes[Integer.parseInt(id.substring(4,6))].stuNum.add(stu[j]); classes[Integer.parseInt(id.substring(4,6))].classHao=id.substring(0,6); stu[j].name = name; stu[j].courseName = courseName; population++; int k=0; for(int kk=0;kk<courseNum;kk++){ if(courseName.equals(courses[kk].name)){ k++; } } if(k==0){ System.out.println(courseName+" does not exist"); j++; continue ; } for(int y=0;y<courseNum;y++){ if(courseName.equals(courses[y].name) && courses[y].mode.equals("考试")&&courses[y].type.equals("必修")) { System.out.println(stu[j].id+" "+stu[j].name+" "+": access mode mismatch"); j++; continue here; } } for(int y=0;y<courseNum;y++){ if((courseName.equals(courses[y].name) && courses[y].mode.equals("考试"))||(courses[y].type.equals("必修")&&courses[y].mode.equals("考察"))) { System.out.println(stu[j].id+" "+stu[j].name+" "+": access mode mismatch"); j++; continue here; } } if(courseNum==0) continue ; stu[j].grade2 = score2;//录入成绩 j++; for(int m = 0;m<courseNum;m++){ if(courses[m].name.equals(courseName)){ courses[m].flag++; courses[m].grade3++; courses[m].grade2+=score2; } } for(int t=0;t<j-1;t++){ if(stu[j-1].id.equals(stu[t].id)&&stu[t].grade2==-1){ stu[t].grade2=score2; } } } } int[] xueHao = new int[population]; for(int m=0 ;m<population;m++){ xueHao[m] = Integer.parseInt(stu[m].id); } Arrays.sort(xueHao); Arrays.sort(stu); int m=0 ; here1: for(;m<population;m++) { for(int c=0;c<m;c++){ if(xueHao[m] == Integer.parseInt(stu[c].id) ) { continue here1; } } int k=0; for (int n = 0; n < population; n++) {//寻找与学号相匹配的学生 if (xueHao[m] == Integer.parseInt(stu[n].id) ) { for(int cc=0;cc<population;cc++) { if (xueHao[m] == Integer.parseInt(stu[cc].id)) { for (int kk = 0; kk < courseNum; kk++) { if (stu[cc].courseName.equals(courses[kk].name)) { k++; } } } } if(k==0){ System.out.println(stu[n].id + " " + stu[n].name + " " + "did not take any exams"); continue here1; } if(courseNum==0)System.out.println(stu[n].id + " " + stu[n].name + " " + "did not take any exams"); else { for (int t = 0; t < courseNum; t++) { if (stu[n].courseName.equals(courses[t].name) && courses[t].mode.equals("考试") && (stu[n].grade1 == -1 || stu[n].grade2 == -1)) { System.out.println(stu[n].id + " " + stu[n].name + " " + "did not take any exams"); continue here1; } else if (stu[n].courseName.equals(courses[t].name) && courses[t].mode.equals("考察") && stu[n].grade2 == -1) { System.out.println(stu[n].id + " " + stu[n].name + " " + "did not take any exams"); continue here1; } else if (stu[n].courseName.equals(courses[t].name) && courses[t].mode.equals("考试") && stu[n].grade1 != -1 && stu[n].grade2 != -1) { System.out.println(stu[n].id + " " + stu[n].name + " " + Average(stu[n].id, stu)); continue here1; } else if (stu[n].courseName.equals(courses[t].name) && courses[t].mode.equals("考察") && stu[n].grade2 != -1) { System.out.println(stu[n].id + " " + stu[n].name + " " + Average(stu[n].id, stu)); continue here1; } } } } } } Arrays.sort(courses); for(int n=0;n<courseNum;n++){ if(courses[n].mode.equals("考试") && (courses[n].grade1 == 0 || courses[n].grade2 == 0)&&courses[n].grade3==-1) { System.out.println(courses[n].name + " " + "has no grades yet"); continue; } else if(courses[n].mode.equals("考察") && courses[n].grade2 == 0&&courses[n].grade3==-1){ System.out.println(courses[n].name+" "+"has no grades yet"); continue; } if(courses[n].type.equals("必修")||courses[n].mode.equals("考试")){ if(courses[n].flag==0) { System.out.println(courses[n].name + " " + "has no grades yet"); continue; } else { System.out.println(courses[n].name + " " + courses[n].grade1 / courses[n].flag + " " + courses[n].grade2 / courses[n].flag + " " + getAverage(courses[n])); continue; } }else if(courses[n].flag==0) { System.out.println(courses[n].name + " " + "has no grades yet"); continue; } else { System.out.println(courses[n].name + " " + courses[n].grade2 / courses[n].flag + " " + getAverage(courses[n])); continue; } } for(int l=0;l<100;l++){ if(classes[l].stuNum.size()==0)continue; else if(ClassAverage(classes[l],stu)==-1) System.out.println(classes[l].classHao+" "+"has no grades yet"); else System.out.println(classes[l].classHao+" "+ClassAverage(classes[l],stu)); } } public static int ClassAverage(Class classes,Stu[] stu){//计算班级平均成绩 int average=0; int num=0; for(int i=0;i<classes.stuNum.size();i++) { if(Average(classes.stuNum.get(i).id,stu)==0){ num++; }else average+= Average(classes.stuNum.get(i).id,stu); } if(classes.stuNum.size()-num==0)return 0; else return average/(classes.stuNum.size()-num); } public static int Average(String id,Stu[] stu){//得到一个学生总成绩平均分 int average=0,flag=0; here3: for(int m=0;m<population;m++) { if (id.equals(stu[m].id)) { for (int i = 0; i < m; i++) { if(stu[m].id.equals(stu[i].id)&&(stu[m]).grade2==(stu[i].grade2)&&(stu[m]).grade1==(stu[i].grade1)){ continue here3; } } average += stu[m].getFinalGrade(); flag++; } } if(flag==0)return 0; else return average/flag; } public static int getAverage(Course course){//得到一门课程所有学生的平均分 if(course.type.equals("必修")||course.mode.equals("考试")){ return (int)((course.grade1*0.3+ course.grade2*0.7)/course.flag); } return course.grade2/course.flag; } } class Stu implements Comparable<Stu>{ String id ; String name ; String courseName ; int grade1 = -1;//平时成绩 int grade2 = -1;//期末成绩 public int getFinalGrade(){//计算学生一门课程总成绩 if(this.grade1==-1){ return this.grade2; }else{ return (int)(this.grade1*0.3+this.grade2*0.7); } } public int compareTo(Stu o){ if(this.id==null) return 0; if(Integer.parseInt(this.id)-Integer.parseInt(o.id)>0){ return 1; }else if(Integer.parseInt(this.id)-Integer.parseInt(o.id)<0){ return -1; }else return 0; } } class Course implements Comparable<Course> { String name ;//课程名字 String type ;//考试类型 String mode ;//考试方法 int flag=0;//有多少学生考试 int grade1 = 0;//平时成绩 int grade2 = 0;//期末成绩 int grade3 = -1; @Override public int compareTo(Course o) { Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA); if(this.name==null) return 0; return compare.compare(this.name,o.name); } } class Class{ String classHao; ArrayList<Stu> stuNum = new ArrayList<Stu>(); }
分析代码可以知道构建框架时可以根据题目所给类图来搭建,可以分别构建课程、学生和班级类,班级中包含学生类,学生类中包含课程类,课程类中包含各种成绩,这样有条不紊的的框架在编写整个代码时有极大的帮助,然后计算分数等的方法既可以在学生类里也可以在Main类中,使用while循环来接收输入的信息,在这之前可以放课程、学生、班级ArrayList全局变量,用于储存放入的信息,值得一提的是Arraylist的排序方法也可以通过Compare接口实现改变排序方法,这点非常非常重要,后面的成绩排序也是用这个接口实现的。大部分方法和菜单一样,看看代码希望对你有所帮助。
第七次题目集
7-1 容器-HashMap-检索
比较简单,应该都会就不多说了。
7-2 容器-HashMap-排序
同上。
7-4 动物发声模拟器(多态)
这个题目和期中考试的多态很像,属于非常基础的题目,就算抽象类的继承并重写其方法。
7-3 课程成绩统计程序-2
代码如下
import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String in = input.nextLine(); ParseInput handle=new ParseInput(); while (!in.equals("end")) { handle.parseInput(in);//解析用户输入的每一行数据 in = input.nextLine(); } handle.showStudents(); handle.showCourses(); handle.showClasses(); } } class InputMatching { static String stuNumMatching = "[0-9]{8}";//8个0-9的数字 static String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符 static String scoreMatching = "([1-9]?[0-9]|100)"; static String scoreMatching1 = "( ([1-9]?[0-9]|100))*"; static String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符 static String courseTypeMatching = "(选修|必修|实验)"; static String checkCourseTypeMatching = "(考试|考察|实验)"; //cousrInput用于定义课程信息模式(正则表达式) static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching; //scoreInput用于定义成绩信息模式(正则表达式) static String scoreInput1 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + " "+scoreMatching; static String scoreInput2 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching; static String scoreInput3 = stuNumMatching + " " + stuNameMatching + " " +courseNameMatching +" " +"[4-9]"+scoreMatching1; public static int matchingInput(String s) { if (matchingCourse(s)) { return 1; } if (matchingScore(s)) { return 2; } return 0; } private static boolean matchingCourse(String s) { return s.matches(courseInput); } private static boolean matchingScore(String s) { //System.out.println(match); return s.matches(scoreInput1)||s.matches(scoreInput2)||s.matches(scoreInput3); } } class ParseInput{ ArrayList<Course> allCourses = new ArrayList<Course>(); ArrayList<Student> allStudents = new ArrayList<Student>(); ArrayList<Class> classes = new ArrayList<Class>(); public void parseInput(String in){ String[] parseInput = in.split(" "); if(InputMatching.matchingInput(in)==1) {//课程信息 String name = parseInput[0]; String type = parseInput[1]; String mode = parseInput[2]; for (Course course : allCourses) //判断之前是否已经录入该课程 if (course.name.equals(name)) { return; } if((type.equals("必修")&&mode.equals("考察"))||(type.equals("必修")&&mode.equals("实验"))||(type.equals("选修")&&mode.equals("实验"))||(type.equals("实验")&&mode.equals("考察"))||(type.equals("实验")&&mode.equals("考试"))){//课程信息不对,不录入该课程 System.out.println(name+" "+": course type & access mode mismatch"); return; } Course cou = new Course(name,type,mode); allCourses.add(cou); } if(InputMatching.matchingInput(in)==2) {//学生信息 if(in.matches(InputMatching.scoreInput1)){//考试 String id = parseInput[0]; String name = parseInput[1]; String courseName = parseInput[2]; String classHao = id.substring(0,6); Integer score1 = null; Integer score2 = null; for (Student stu:allStudents) {//判断这个学生之前是否已经录入过该门课程 if(stu.ID.equals(id)){ for (Course cou:allCourses) { if(cou.name.equals(courseName)&&cou.grade2!=-1){ return; } } } } score1 = Integer.parseInt(parseInput[3]); score2 = Integer.parseInt(parseInput[4]); int flag=-1; for(int i=0;i<allStudents.size();i++){ if(allStudents.get(i).ID.equals(id)){ flag=i; } } if(flag==-1) { Student stu = new Student(); stu.ID = id; stu.name = name; allStudents.add(stu); if (classes.size() == 0) {//将学生加到班级中 Class newClass = new Class(); newClass.classHao = classHao; newClass.stus.add(stu); classes.add(newClass); } int exist=-1; int tem=-1; for (int i = 0; i < classes.size(); i++) { if (classes.get(i).classHao.equals(classHao)) { tem = i; if (classes.get(i).seartStudent(id)) {//若学生已经存在,则不加入班级 exist++; } } } if(exist==-1&&tem==-1){ Class newClass = new Class(); newClass.classHao = classHao; newClass.stus.add(stu); classes.add(newClass); }else if(exist==-1&&tem!=-1){ classes.get(tem).stus.add(stu); }else if(exist!=-1){ } int flag1 = -1;//判断课程是否存在 for (int j = 0; j < allCourses.size(); j++) { if (allCourses.get(j).name.equals(courseName)) { if (allCourses.get(j).type.equals("考察")) {//判断成绩是否匹配 System.out.println(id + " " + name + " " + ": access mode mismatch"); return; } flag1 = j; } } if (flag1 == -1) { System.out.println(courseName + " does not exist"); return; } Course cou = new Course(); for (Course temp : allCourses) { if (temp.name.equals(courseName)) { cou.mode = temp.mode; cou.type = temp.type; } } cou.grade1 = score1; cou.grade2 = score2; cou.name = courseName; stu.courses.add(cou); }else{ int flag1 = -1;//判断课程是否存在 for (int j = 0; j < allCourses.size(); j++) { if (allCourses.get(j).name.equals(courseName)) { if (allCourses.get(j).mode.equals("考察")) {//判断成绩是否匹配 System.out.println(id + " " + name + " " + ": access mode mismatch"); return; } flag1 = j; } } if (flag1 == -1) { System.out.println(courseName + " does not exist"); return; } Course cou = new Course(); for (Course temp : allCourses) { if (temp.name.equals(courseName)) { cou.mode = temp.mode; cou.type = temp.type; } } cou.grade1 = score1; cou.grade2 = score2; cou.name = courseName; allStudents.get(flag).courses.add(cou); } } if(in.matches(InputMatching.scoreInput2)){//考察 String id = parseInput[0]; String name = parseInput[1]; String courseName = parseInput[2]; String classHao = id.substring(0,6); Integer score2 = null; for (Student stu:allStudents) {//判断这个学生之前是否已经录入过该门课程 if(stu.ID.equals(id)){ for (Course cou:allCourses) { if(cou.name.equals(courseName)&&cou.grade2!=-1){ return; } } } } score2 = Integer.parseInt(parseInput[3]); int flag=-1; for(int i=0;i<allStudents.size();i++){ if(allStudents.get(i).ID.equals(id)){ flag=i; } } if(flag==-1) { Student stu = new Student(); stu.ID = id; stu.name = name; allStudents.add(stu); if (classes.size() == 0) {//将学生加到班级中 Class newClass = new Class(); newClass.classHao = classHao; newClass.stus.add(stu); classes.add(newClass); } int exist=-1; int tem=-1; for (int i = 0; i < classes.size(); i++) { if (classes.get(i).classHao.equals(classHao)) { tem = i; if (classes.get(i).seartStudent(id)) {//若学生已经存在,则不加入班级 exist++; } } } if(exist==-1&&tem==-1){ Class newClass = new Class(); newClass.classHao = classHao; newClass.stus.add(stu); classes.add(newClass); }else if(exist==-1&&tem!=-1){ classes.get(tem).stus.add(stu); }else if(exist!=-1){ } int flag1 = -1;//判断课程是否存在 for (int j = 0; j < allCourses.size(); j++) { if (allCourses.get(j).name.equals(courseName)) { if (allCourses.get(j).mode.equals("考试")) {//判断成绩是否匹配 System.out.println(id + " " + name + " " + ": access mode mismatch"); return; } flag1 = j; } } if (flag1 == -1) { System.out.println(courseName + " does not exist"); return; } Course cou = new Course(); for (Course temp : allCourses) { if (temp.name.equals(courseName)) { cou.mode = temp.mode; cou.type = temp.type; } } cou.grade2 = score2; cou.name = courseName; stu.courses.add(cou); }else{ int flag1 = -1;//判断课程是否存在 for (int j = 0; j < allCourses.size(); j++) { if (allCourses.get(j).name.equals(courseName)) { if (allCourses.get(j).type.equals("考试")) {//判断成绩是否匹配 System.out.println(id + " " + name + " " + ": access mode mismatch"); return; } flag1 = j; } } if (flag1 == -1) { System.out.println(courseName + " does not exist"); return; } Course cou = new Course(); for (Course temp : allCourses) { if (temp.name.equals(courseName)) { cou.mode = temp.mode; cou.type = temp.type; } } cou.grade2 = score2; cou.name = courseName; allStudents.get(flag).courses.add(cou); } } } if(in.matches(InputMatching.scoreInput3)){//实验 String id = parseInput[0]; String name = parseInput[1]; String courseName = parseInput[2]; String classHao = id.substring(0,6); Integer ciShu = null; int score3=0; ciShu = Integer.parseInt(parseInput[3]); for (Student stu:allStudents) {//判断这个学生之前是否已经录入过该门课程 if(stu.ID.equals(id)){ for (Course cou:allCourses) { if(cou.name.equals(courseName)&&cou.grade3!=-1){ return; } } } } int flag=-1; for(int i=0;i<allStudents.size();i++){ if(allStudents.get(i).ID.equals(id)){ flag=i; } } if(flag==-1) { Student stu = new Student(); stu.ID = id; stu.name = name; allStudents.add(stu); if (classes.size() == 0) {//将学生加到班级中 Class newClass = new Class(); newClass.classHao = classHao; newClass.stus.add(stu); classes.add(newClass); } int exist=-1; int tem=-1; for (int i = 0; i < classes.size(); i++) { if (classes.get(i).classHao.equals(classHao)) { tem = i; if (classes.get(i).seartStudent(id)) {//若学生已经存在,则不加入班级 exist++; } } } if(exist==-1&&tem==-1){ Class newClass = new Class(); newClass.classHao = classHao; newClass.stus.add(stu); classes.add(newClass); }else if(exist==-1&&tem!=-1){ classes.get(tem).stus.add(stu); }else if(exist!=-1){ } if(ciShu!=parseInput.length-4){//成绩格式不对 System.out.println(id + " " + name + " " + ": access mode mismatch"); return; } for(int i=0;i<parseInput.length-4;i++){ score3+=Integer.parseInt(parseInput[i+4]); } score3=score3/ciShu; int flag1 = -1;//判断课程是否存在 for (int j = 0; j < allCourses.size(); j++) { if (allCourses.get(j).name.equals(courseName)) { if (allCourses.get(j).mode.equals("考试")||allCourses.get(j).mode.equals("考察")) {//判断成绩是否匹配 System.out.println(id + " " + name + " " + ": access mode mismatch"); return; } flag1 = j; } } if (flag1 == -1) { System.out.println(courseName + " does not exist"); return; } Course cou = new Course(); for (Course temp : allCourses) { if (temp.name.equals(courseName)) { cou.mode = temp.mode; cou.type = temp.type; } } cou.grade3 = score3; cou.name = courseName; stu.courses.add(cou); }else{ if(ciShu!=parseInput.length-4){//成绩格式不对 System.out.println(id + " " + name + " " + ": access mode mismatch"); return; } for(int i=0;i<parseInput.length-4;i++){ score3+=Integer.parseInt(parseInput[i+4]); } score3=score3/ciShu; int flag1 = -1;//判断课程是否存在 for (int j = 0; j < allCourses.size(); j++) { if (allCourses.get(j).name.equals(courseName)) { if (allCourses.get(j).type.equals("考试")||allCourses.get(j).mode.equals("考察")) {//判断成绩是否匹配 System.out.println(id + " " + name + " " + ": access mode mismatch"); return; } flag1 = j; } } if (flag1 == -1) { System.out.println(courseName + " does not exist"); return; } Course cou = new Course(); for (Course temp : allCourses) { if (temp.name.equals(courseName)) { cou.mode = temp.mode; cou.type = temp.type; } } cou.grade3 = score3; cou.name = courseName; allStudents.get(flag).courses.add(cou); } } if(InputMatching.matchingInput(in)==0) {//输入格式错误 System.out.println("wrong format"); } } public void showStudents () { Collections.sort(allStudents); here: for (int i = 0; i < allStudents.size(); i++) { for(int j=0;j<i;j++){ if(allStudents.get(i).ID.equals(allStudents.get(j).ID)){ continue here; } } Student stu = allStudents.get(i); //从总选课表 ListChooseCourse 中获取该生的选课记录集合 ArrayList<Course> stuCourseSelects = getStudentSelects(stu.ID); if (stuCourseSelects.size()!=0){ System.out.println(stu.ID + " " + stu.name + " " + getAvgTotalScore(stuCourseSelects)); } else{ System.out.println(stu.ID + " " + stu.name + " did not take any exams"); } } } public void showCourses(){ Collections.sort(allCourses); int allGrade=0; int flag=0; int grade1=0; int grade2=0; int grade3=0; for(int i=0;i<allCourses.size();i++){ for(int j=0;j< allStudents.size();j++){ ArrayList<Course> stuCourseSelects = getStudentSelects(allStudents.get(j).ID); for(int k=0;k<stuCourseSelects.size();k++){ if(allCourses.get(i).name.equals(stuCourseSelects.get(k).name)){ allGrade+=getAScore(stuCourseSelects.get(k)); grade1+=stuCourseSelects.get(k).grade1; grade2+=stuCourseSelects.get(k).grade2; grade3+=stuCourseSelects.get(k).grade3; flag++; } } } if(flag==0){ System.out.println(allCourses.get(i).name+ " " + "has no grades yet"); }else if(allCourses.get(i).mode.equals("考试")){ System.out.println(allCourses.get(i).name + " " + (int) (grade1 / flag) + " " + (int) (grade2 / flag) + " " + (int) (allGrade / flag)); }else if(allCourses.get(i).mode.equals("考察")){ System.out.println(allCourses.get(i).name + " " + (int) (grade2 / flag) + " " + (int) (allGrade / flag)); }else if(allCourses.get(i).mode.equals("实验")){ System.out.println(allCourses.get(i).name + " " + (int) (grade3 / flag)); } allGrade=0; grade1=0; grade2=0; grade3=0; flag=0; } } public void showClasses(){ Collections.sort(classes); for(int i=0;i< classes.size();i++){ int allGrade=0; int weiKao=0;//没有考试的人数 for(int j=0;j<classes.get(i).stus.size();j++){ if(classes.get(i).stus.get(j).courses.size()==0){ weiKao++; }else { allGrade += getAvgTotalScore(getStudentSelects(classes.get(i).stus.get(j).ID)); } } if(classes.get(i).stus.size()==0){ System.out.println(classes.get(i).classHao+" "+"has no grades yet"); }else { if((classes.get(i).stus.size()-weiKao)==0){ System.out.println(classes.get(i).classHao+" "+"has no grades yet"); }else { System.out.println(classes.get(i).classHao + " " + allGrade / (classes.get(i).stus.size() - weiKao)); } } } } public int getAScore(Course temp){//得到一个学生一门课程分数 if(temp.mode.equals("考试")){ return (int)(temp.grade1*0.3+temp.grade2*0.7); }else if(temp.mode.equals("考察")){ return temp.grade2; }else{ return temp.grade3; } } public int getAvgTotalScore(ArrayList<Course> temp){//得到一个学生最终成绩 int allGrade=0; for(int i=0;i<temp.size();i++){ if(temp.get(i).mode.equals("考试")){ allGrade+=(int)(temp.get(i).grade1*0.3+temp.get(i).grade2*0.7); }else if(temp.get(i).mode.equals("考察")){ allGrade+=temp.get(i).grade2; }else if(temp.get(i).mode.equals("实验")){ allGrade+=temp.get(i).grade3; } } return allGrade/temp.size(); } public ArrayList<Course> getStudentSelects(String id){//得到一个学生所上课程 ArrayList<Course> temp = new ArrayList<Course>(); for (int i=0;i<allStudents.size();i++){ if(allStudents.get(i).ID.equals(id)){ for (Course tem:allStudents.get(i).courses) { temp.add(tem); } } } return temp; } } class Course implements Comparable<Course>{ String name; String type;//考试类型 String mode;//考试方法 int grade1;//平时成绩 int grade2=-1;//期末成绩 int grade3=-1;//实验成绩 public Course(String name, String type, String mode) { this.name=name; this.type=type; this.mode=mode; } public Course() { } public boolean seartCourse(ArrayList<Course> allCourses,String courseName) { for (int j = 0; j < allCourses.size(); j++) { if (allCourses.get(j).name.equals(courseName)) { return true; } } return false; } public int compareTo(Course o) { Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA); if(this.name==null) return 0; return compare.compare(this.name,o.name); } } class Student implements Comparable<Student>{ String ID; String name; ArrayList<Course> courses = new ArrayList<Course>(); @Override public int compareTo(Student o){ if(this.ID==null) return 0; if(Integer.parseInt(this.ID)-Integer.parseInt(o.ID)>0){ return 1; }else if(Integer.parseInt(this.ID)-Integer.parseInt(o.ID)<0){ return -1; }else return 0; } } class Class implements Comparable<Class>{ String classHao; ArrayList<Student> stus = new ArrayList<Student>(); public boolean seartStudent(String id) { for (int j = 0; j < stus.size(); j++) { if (stus.get(j).ID.equals(id)) { return true; } } return false; } @Override public int compareTo(Class o) { if(this.classHao==null) return 0; if(Integer.parseInt(this.classHao)-Integer.parseInt(o.classHao)>0){ return 1; }else if(Integer.parseInt(this.classHao)-Integer.parseInt(o.classHao)<0){ return -1; }else return 0; } }
新增的实验课增加了正则表达式的难度,如何判断输入的成绩信息是实验还是考试是一个难点,我查阅了很多资料最后才找到了如上代码的正则表达式,感兴趣可以借鉴借鉴,然后通过判断是否匹配相应的正则表达式来进入自己专属的if语句并完成相应功能,有正则表达式的代码会更容易看懂,改代码也会方便很多,建议大家多用用正则表达式,它真的很好用比判断输入一行的信息个数要好用很多且不容易出错。这次迭代不难也是3次作业最简单的一次,前提是第一次的框架要搭建好,不能乱,一乱后面就全乱了。
第八次题目集
7-2 课程成绩统计程序-3
第八次最重要的就是课程成绩统计程序,这里就只着重分析这一个题目,代码如下
import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String in = input.nextLine(); ParseInput handle=new ParseInput(); while (!in.equals("end")) { handle.parseInput(in);//解析用户输入的每一行数据 in = input.nextLine(); } handle.showStudents(); handle.showCourses(); handle.showClasses(); } } class InputMatching { static String sNANMatch = "[0-9]{8}"+" "+ "\\S{1,10}"; // static String stuNameMatching = "\\S{1,10}"; static String sMatch = "([1-9]?[0-9]|100)"; static String sMatch1 = "( ([1-9]?[0-9]|100)){3,10}"; static String cNMatching = "\\S{1,10}"; static String cTMatch = "(选修|必修|实验)"; static String cQMatch ="( [0-9]+([.]{1}[0-9]+){0,1})*"; static String cXSMatch ="(( 0\\.[0-9]+)|( [01](\\.0+)?)){2}"; static String cXSMatch1 ="(( 0\\.[0-9]+)|( [01](\\.0+)?))*"; static String ckCTMatch = "(考试|考察|实验)"; static String courseInput1 = cNMatching + " " + cTMatch + " " + ckCTMatch; static String courseInput2 = cNMatching+ " " + cTMatch + " " + ckCTMatch+cXSMatch; static String courseInput3 = cNMatching+ " " + cTMatch + " " + ckCTMatch+" "+"[4-9]"+ cXSMatch1; //scoreInput用于定义成绩信息模式(正则表达式) static String scoreInput1 = sNANMatch + " " + cNMatching+ " " + sMatch + " "+sMatch; static String scoreInput2 = sNANMatch + " " + cNMatching + " " + sMatch; static String scoreInput3 = sNANMatch + " " +cNMatching +sMatch1; public static int matchingInput(String s) { if (s.matches(courseInput1)||s.matches(courseInput2)||s.matches(courseInput3)) { return 1; } if (s.matches(scoreInput1)||s.matches(scoreInput2)||s.matches(scoreInput3)) { return 2; } return 0; } } class ParseInput{ ArrayList<Course> allCourses = new ArrayList<Course>(); ArrayList<Student> allStudents = new ArrayList<Student>(); ArrayList<Class> classes = new ArrayList<Class>(); public void parseInput(String in){ String[] parseInput = in.split(" "); if(InputMatching.matchingInput(in)==1) {//课程信息 if (in.matches(InputMatching.courseInput1)){//考察 String name = parseInput[0]; String type = parseInput[1]; String mode = parseInput[2]; for (Course course : allCourses) //判断之前是否已经录入该课程 if (course.name.equals(name)) { return; } if ((type.equals("必修") && (mode.equals("考察")|| mode.equals("实验")))|| (type.equals("选修") && mode.equals("实验")) || (type.equals("实验") && (mode.equals("考察") || mode.equals("考试")))) {//课程信息不对,不录入该课程 System.out.println(name + " : course type & access mode mismatch"); return; } Course cou = new Course(name, type, mode); allCourses.add(cou); return; } if (in.matches(InputMatching.courseInput2)) {//考试 String name = parseInput[0]; String type = parseInput[1]; String mode = parseInput[2]; ArrayList<Float> quanTemp = new ArrayList<Float>(); quanTemp.add(Float.parseFloat(parseInput[3])); quanTemp.add(Float.parseFloat(parseInput[4])); for (Course course : allCourses) //判断之前是否已经录入该课程 if (course.name.equals(name)) { return; } if ((quanTemp.get(0) + quanTemp.get(1) )<0.99 || (quanTemp.get(0) + quanTemp.get(1) >1.01)) { System.out.println(name + " : weight value error"); return; } if ((type.equals("必修") && (mode.equals("考察") || mode.equals("实验"))) || (type.equals("选修") && mode.equals("实验")) || (type.equals("实验") && (mode.equals("考察") || mode.equals("考试")))) {//课程信息不对,不录入该课程 System.out.println(name + " : course type & access mode mismatch"); return; } Course cou = new Course(name, type, mode, quanTemp); allCourses.add(cou); return; } if (in.matches(InputMatching.courseInput3)) {//实验 String name = parseInput[0]; String type = parseInput[1]; String mode = parseInput[2]; String ciShu = parseInput[3]; ArrayList<Float> quanTemp = new ArrayList<Float>(); for (Course course : allCourses) //判断之前是否已经录入该课程 if (course.name.equals(name)) { return; } if (Integer.parseInt(ciShu) != parseInput.length - 4) { System.out.println(name + " : number of scores does not match"); return; } for (int i = 0; i < Integer.parseInt(ciShu); i++) { quanTemp.add(Float.parseFloat(parseInput[4 + i])); } double all = 0; for (Float aFloat : quanTemp) { all += aFloat; } if (all <0.99 || all >1.01) { System.out.println(name + " : weight value error"); return; } if ((type.equals("必修") && (mode.equals("考察") || mode.equals("实验"))) || (type.equals("选修") && mode.equals("实验")) || (type.equals("实验") && (mode.equals("考察") || mode.equals("考试")))) {//课程信息不对,不录入该课程 System.out.println(name + " : course type & access mode mismatch"); return; } Course cou = new Course(name, type, mode, quanTemp); allCourses.add(cou); return; } } if (InputMatching.matchingInput(in) == 2) {//学生信息 if (in.matches(InputMatching.scoreInput2)) {//考察 String id = parseInput[0]; String name = parseInput[1]; String courseName = parseInput[2]; String classHao = id.substring(0, 6); Integer score2 ; for (Student stu : allStudents) {//判断这个学生之前是否已经录入过该门课程 if (stu.ID.equals(id)) { for (Course cou : allCourses) { if (cou.name.equals(courseName) && cou.grade2 != -1) { return; } } } } score2 = Integer.parseInt(parseInput[3]); int flag = -1; for (int i = 0; i < allStudents.size(); i++) { if (allStudents.get(i).ID.equals(id)) { flag = i; } } Student stu = new Student(); stu.ID = id; stu.name = name; allStudents.add(stu); if (classes.size() == 0) {//将学生加到班级中 Class newClass = new Class(); newClass.classHao = classHao; newClass.stus.add(stu); classes.add(newClass); } int exist = -1; int tem = -1; for (int i = 0; i < classes.size(); i++) { if (classes.get(i).classHao.equals(classHao)) { tem = i; if (classes.get(i).seartStudent(id)) {//若学生已经存在,则不加入班级 exist++; } } } if (exist == -1 && tem == -1) { Class newClass = new Class(); newClass.classHao = classHao; newClass.stus.add(stu); classes.add(newClass); } else if (exist == -1 && tem != -1) { classes.get(tem).stus.add(stu); } else if (exist != -1) { } int flag1 = -1;//判断课程是否存在 for (int j = 0; j < allCourses.size(); j++) { if (allCourses.get(j).name.equals(courseName)) { if (allCourses.get(j).mode.equals("考试")||allCourses.get(j).mode.equals("实验")) {//判断成绩是否匹配 System.out.println(id + " " + name + " : access mode mismatch"); return; } flag1 = j; } } if (flag1 == -1) { System.out.println(courseName + " does not exist"); return; } Course cou = new Course(); for (Course temp : allCourses) { if (temp.name.equals(courseName)) { cou.mode = temp.mode; cou.type = temp.type; } } cou.grade2 = score2; cou.name = courseName; if (flag == -1) { stu.courses.add(cou); }else allStudents.get(flag).courses.add(cou); } if (in.matches(InputMatching.scoreInput1)) {//考试 String id = parseInput[0]; String name = parseInput[1]; String courseName = parseInput[2]; String classHao = id.substring(0, 6); Integer score1 ; Integer score2 ; for (Student stu : allStudents) {//判断这个学生之前是否已经录入过该门课程 if (stu.ID.equals(id)) { for (Course cou : allCourses) { if (cou.name.equals(courseName) && cou.grade2 != -1) { return; } } } } score1 = Integer.parseInt(parseInput[3]); score2 = Integer.parseInt(parseInput[4]); int flag = -1; for (int i = 0; i < allStudents.size(); i++) { if (allStudents.get(i).ID.equals(id)) { flag = i; } } Student stu = new Student(); stu.ID = id; stu.name = name; allStudents.add(stu); if (classes.size() == 0) {//将学生加到班级中 Class newClass = new Class(); newClass.classHao = classHao; newClass.stus.add(stu); classes.add(newClass); } int exist = -1; int tem = -1; for (int i = 0; i < classes.size(); i++) { if (classes.get(i).classHao.equals(classHao)) { tem = i; if (classes.get(i).seartStudent(id)) {//若学生已经存在,则不加入班级 exist++; } } } if (exist == -1 && tem == -1) { Class newClass = new Class(); newClass.classHao = classHao; newClass.stus.add(stu); classes.add(newClass); } else if (exist == -1 && tem != -1) { classes.get(tem).stus.add(stu); } else if (exist != -1) { } int flag1 = -1;//判断课程是否存在 for (int j = 0; j < allCourses.size(); j++) { if (allCourses.get(j).name.equals(courseName)) { if (allCourses.get(j).type.equals("考察")||allCourses.get(j).type.equals("实验")) {//判断成绩是否匹配 System.out.println(id + " " + name + " : access mode mismatch"); return; } flag1 = j; } } if (flag1 == -1) { System.out.println(courseName + " does not exist"); return; } Course cou = new Course(); for (Course temp : allCourses) { if (temp.name.equals(courseName)) { cou.mode = temp.mode; cou.type = temp.type; cou.quan.addAll(temp.quan); } } cou.grade1 = score1; cou.grade2 = score2; cou.name = courseName; if (flag == -1) { stu.courses.add(cou); }else allStudents.get(flag).courses.add(cou); } } if (in.matches(InputMatching.scoreInput3)) {//实验 String id = parseInput[0]; String name = parseInput[1]; String courseName = parseInput[2]; String classHao = id.substring(0, 6); for (Student stu : allStudents) {//判断这个学生之前是否已经录入过该门课程 if (stu.ID.equals(id)) { for (Course cou : allCourses) { if (cou.name.equals(courseName) && cou.grade3.size() != 0) { return; } } } } int flag = -1; for (int i = 0; i < allStudents.size(); i++) { if (allStudents.get(i).ID.equals(id)) { flag = i; } } Student stu = new Student(); stu.ID = id; stu.name = name; allStudents.add(stu); if (classes.size() == 0) {//将学生加到班级中 Class newClass = new Class(); newClass.classHao = classHao; newClass.stus.add(stu); classes.add(newClass); } int exist = -1; int tem = -1; for (int i = 0; i < classes.size(); i++) { if (classes.get(i).classHao.equals(classHao)) { tem = i; if (classes.get(i).seartStudent(id)) {//若学生已经存在,则不加入班级 exist++; } } } if (exist == -1 && tem == -1) { Class newClass = new Class(); newClass.classHao = classHao; newClass.stus.add(stu); classes.add(newClass); } else if (exist == -1 && tem != -1) { classes.get(tem).stus.add(stu); } else if (exist != -1) { } int flag1 = -1;//判断课程是否存在 for (int j = 0; j < allCourses.size(); j++) { if (allCourses.get(j).name.equals(courseName)) { if (allCourses.get(j).mode.equals("考试") || allCourses.get(j).mode.equals("考察")) {//判断成绩是否匹配 System.out.println(id + " " + name + " : access mode mismatch"); return; } flag1 = j; } } if (flag1 == -1) { System.out.println(courseName + " does not exist"); return; } if (allCourses.get(flag1).quan.size() != parseInput.length - 3) {//成绩格式不对 System.out.println(id + " " + name + " : access mode mismatch"); return; } Course cou = new Course(); for (Course temp : allCourses) { if (temp.name.equals(courseName)) { cou.mode = temp.mode; cou.type = temp.type; cou.quan.addAll(temp.quan); } } ArrayList<Integer> gradeTemp = new ArrayList<Integer>(); for (int i = 0; i < allCourses.get(flag1).quan.size(); i++) { gradeTemp.add(Integer.parseInt(parseInput[3 + i])); } cou.grade3.addAll(gradeTemp); cou.name = courseName; if (flag == -1) { stu.courses.add(cou); }else allStudents.get(flag).courses.add(cou); } if(InputMatching.matchingInput(in)==0) {//输入格式错误 System.out.println("wrong format"); } } public void showStudents () { Collections.sort(allStudents); here: for (int i = 0; i < allStudents.size(); i++) { for(int j=0;j<i;j++){ if(allStudents.get(i).ID.equals(allStudents.get(j).ID)){ continue here; } } Student stu = allStudents.get(i); //从总选课表 ListChooseCourse 中获取该生的选课记录集合 ArrayList<Course> stuCourseSelects = getStudentSelects(stu.ID); if (stuCourseSelects.size()!=0){ System.out.println(stu.ID + " " + stu.name + " " + getAvgTotalScore(stuCourseSelects)); } else{ System.out.println(stu.ID + " " + stu.name + " did not take any exams"); } } } public void showCourses(){ Collections.sort(allCourses); int allGrade=0; int flag=0; for (Course allCours : allCourses) { for (Student allStudent : allStudents) { ArrayList<Course> stuCourseSelects = getStudentSelects(allStudent.ID); for (Course stuCourseSelect : stuCourseSelects) { if (allCours.name.equals(stuCourseSelect.name)) { allGrade += getAScore(stuCourseSelect); flag++; } } } if (flag == 0) { System.out.println(allCours.name + " has no grades yet"); } else { System.out.println(allCours.name + " " + (allGrade / flag)); } allGrade = 0; flag = 0; } } public void showClasses(){ Collections.sort(classes); for (Class aClass : classes) { int allGrade = 0; int weiKao = 0;//没有考试的人数 for (int j = 0; j < aClass.stus.size(); j++) { if (aClass.stus.get(j).courses.size() == 0) { weiKao++; } else { allGrade += getAvgTotalScore(getStudentSelects(aClass.stus.get(j).ID)); } } if (aClass.stus.size() == 0) { System.out.println(aClass.classHao + " has no grades yet"); } else { if ((aClass.stus.size() - weiKao) == 0) { System.out.println(aClass.classHao + " has no grades yet"); } else { System.out.println(aClass.classHao + " " + allGrade / (aClass.stus.size() - weiKao)); } } } } public int getAScore(Course temp){//得到一个学生一门课程分数 if(temp.mode.equals("考试")){ return (int)(temp.grade1*temp.quan.get(0) + temp.grade2*temp.quan.get(1)); }else if(temp.mode.equals("考察")){ return temp.grade2; }else{ double all=0; for(int i=0;i<temp.quan.size();i++){ all+=temp.quan.get(i)*temp.grade3.get(i); } return (int)all; } } public int getAvgTotalScore(ArrayList<Course> temp){//得到一个学生最终成绩 int allGrade=0; for (Course course : temp) { allGrade += getAScore(course); } return allGrade/temp.size(); } public ArrayList<Course> getStudentSelects(String id){//得到一个学生所上课程 ArrayList<Course> temp = new ArrayList<Course>(); for (Student allStudent : allStudents) { if (allStudent.ID.equals(id)) { for (Course tem : allStudent.courses) { temp.add(tem); } } } return temp; } } class Course implements Comparable<Course>{ String name; String type;//考试类型 String mode;//考试方法 int grade1;//平时成绩 int grade2=-1;//期末成绩 ArrayList<Float> quan = new ArrayList<Float>(); ArrayList<Integer> grade3 = new ArrayList<Integer>();//实验成绩 public Course(String name, String type, String mode) { this.name=name; this.type=type; this.mode=mode; } public Course() { } public Course(String name, String type, String mode, ArrayList<Float> q) { this.name=name; this.type=type; this.mode=mode; this.quan.addAll(q); } public int compareTo(Course o) { Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA); if(this.name==null) return 0; return compare.compare(this.name,o.name); } } class Student implements Comparable<Student>{ String ID; String name; ArrayList<Course> courses = new ArrayList<Course>(); @Override public int compareTo(Student o){ if(this.ID==null) return 0; if(Integer.parseInt(this.ID)-Integer.parseInt(o.ID)>0){ return 1; }else if(Integer.parseInt(this.ID)-Integer.parseInt(o.ID)<0){ return -1; }else return 0; } } class Class implements Comparable<Class>{ String classHao; ArrayList<Student> stus = new ArrayList<Student>(); public boolean seartStudent(String id) { for (Student student : stus) { if (student.ID.equals(id)) { return true; } } return false; } @Override public int compareTo(Class o) { if(this.classHao==null) return 0; if(Integer.parseInt(this.classHao)-Integer.parseInt(o.classHao)>0){ return 1; }else if(Integer.parseInt(this.classHao)-Integer.parseInt(o.classHao)<0){ return -1; }else return 0; } }
这一次的迭代更改了计算总成绩的计算方式,实质就是看代码的隅合度高不高,这次的代码减少了内存,所以很多同学在测试代码会发现代码长度过长,我也遇到了这样的问题,解决这个的办法就只有化繁为简,很多地方可以一两行就实现但是实际却用了5 6行,这个就看后面的精简程度了,问题不大,最最最让大家头疼的其实是隐藏的boss——精度问题,由于double类型精度不行,所以测试点很多没能过去,一旦解决了这个就会突然发现很多测试点迎刃而解,很多人来问我也都是这个问题。
以上所有代码都是满分代码,如果对你有帮助的话,可以随意借鉴,希望能帮助到你。
三、踩坑心得
1、精度精度精度,double类型在计算非常精确的小数时不可用,可以用BigDecimal类或者合理使用float类。
2、在不知道自己的正则表达式是否编写正确时可以用http//regex101.com这个网址,专门用来测试正则表达式,非常好用强烈推荐!!!
3、在最后一次题目集中那个关于栈的题目,存入的是地址,取出的是数值,所以出栈后再打印一遍还是原来入栈时的样子,出栈只出值,地址不变。
四、主要困难以及改进建议
1. 困难:不能清楚的抽象出类,思想还停留在上学期的面向过程编程。
建议:多加练习,抽象出类的思想需要大量的练习为基础,最终才能在脑海中形成一个类的世界。
2. 困难:对正则表达式太陌生,不知道在什么地方运用,从而使代码简洁高效。
建议:查阅相关资料同时通过http//regex101.com这个网站反复练习。
3. 建议:以后多用快读做到代码的高效性,摒弃Scanner类也是对代码对自己的一个提升。
4. 建议:多做注释,注释不仅仅是给别人看的,更是给自己看的。很有可能今天自己写的代码明天就忘记了,所以养成注释的良好习惯很重要。
五、总结
通过这三次题目集,我加深了ArrayList类的使用
以及对继承和组合之间的区别和联系
还有如何使代码做到隅合度低,可以更方便的修改代码,不至于改一次就混乱一次
和一看到小数就要想到精度问题等等...
改进与建议
Java课程终于落下帷幕,面向对象这种思维方式给我很大启发,逻辑在代码中至关重要,有好的逻辑才有清晰的代码,培养逻辑能力是成为一个1合格程序员的关键
自学能力也是评判一个人能力大小的重要指标。
最希望的就是每次pta作业结束后都能够讲解,不然同系列的题目最后只会拖的越来越多,不会还是不会,大大影响学习进度,还有就是实验结束后给出模范代码以供参考学习,这样才明白如何改进代码不至于停滞不前。
标签:总结性,return,String,int,equals,pta,blog,mode,name From: https://www.cnblogs.com/xq686868/p/17512235.html