面向对象程序设计
前言:
1.哈哈哈,怎么说呢,也算有了前车之鉴,也就有了我后面比较周全的考虑对于之后的这几次题目集而言.
2.题目集8:这次的题目跟原本的菜单设计类题目大同小异,无异于是订购类似的程序设计,主要体现在一些课程的存储,特殊情况的考虑上,而正如我上述所说的,鉴于上一次经验惨痛的教训,我也就将更多的时间投入到代码方面来了,在题目发布之前的给出的要求中,我便开始对整个题目的类之间的关系以及输入语句,包括排序算法等先做好的第一步的设计,也在题目发布前写出了一个大概的模板,这也帮助了我在后续的正式的题目发布后能够比之前快很多而且准确率更高的情况下编写完整个程序,虽然对于一些测试单没没能够完成,但是确实是动了效率不够,时间凑这句话了
3.题目集9:这次的题目主要是考察从字符串中提取出java关键词,实际就是一种对字符串处理能力的训练,其中可以通过使用一些容器将关键词以及统计次数归入其中,还包括使用正则表达式对于字符串进行处理,例如一些符号和一些多余的空格可以借助其来处理,也包括整个过程中,实际上对于注释的处理非常重要,因为注释内的字符串不计入其中,这也就要求能够识别出单行注释以及多行注释的处理识别问题.
4.题目集10:在题目集8的基础上,对于该代码进行重构,实际是添加一些新的课程以及计算成绩方式的不同,还有包括容器的使用以及多态的体现,总体便是第8次题目集没咋写,这次的你更别想写了的这个意思,当然本这次题目集依旧是重构代码中的一个测试点始终无法查找出来
5.题目集11:本次题目集新加了对于一些java常见类的重构以及语法和还是原来题目集8的重构,主要是修改了类与类之前的关系以及成绩的占比统计,包括对于组合和继承两者的优越性,更加灵活以及可修改性.
设计与分析:
训练题集(8)7-1
题目要求:包括课程、课程成绩两类信息
输出:输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数.
源码:
import java.text.Collator; import java.util.*; public class Main { public static void main(String[] args) { CourseSystem courseSystem = new CourseSystem(); Scanner input = new Scanner(System.in); String Input = input.nextLine(); while(!Input.equals("end")) { if(!Input.contains(" ")) { courseSystem.print(); Input = input.nextLine(); continue; } String []put = Input.split(" "); boolean judgeFlag = false; while(put.length == 3 || put.length == 2 && !put[0].matches("[0-9]{1,8}")) { judgeFlag = true; if(put.length == 2 && put[0].matches("^.{1,10}$")) { courseSystem.addCourse(put[0], put[1], null); break; } else if (put[0].matches("^.{1,10}$")) { courseSystem.addCourse(put[0], put[1], put[2]); break; } else { courseSystem.print(); break; } } while(Input.length() >= 3 && !judgeFlag) { String num = put[0]; String name = put[1]; String course = put[2]; if(put[0].matches("[0-9]{8}") && put[1].matches("^.{1,10}$") && put[2].matches("^.{1,10}$")) { if(put.length == 3) { courseSystem.addSco(num, name, course, 0, 0, 0); courseSystem.addStu(Integer.parseInt(num), name, 0); break; } else if (put.length == 4) { if(put[3].matches("[0-9]{1,2}|100")) { courseSystem.addSco(num, name, course, 0, Integer.parseInt(put[3]), 0); courseSystem.addStu(Integer.parseInt(num), name, 0); break; } else { courseSystem.print(); break; } } else if (put.length == 5) { if(put[3].matches("[0-9]{1,2}|100") && put[4].matches("[0-9]{1,2}|100")) { courseSystem.addSco(num, name, course, Integer.parseInt(put[3]), Integer.parseInt(put[4]), 0); courseSystem.addStu(Integer.parseInt(num), name, 0); break; } else { courseSystem.print(); break; } } else { courseSystem.print(); break; } } else { courseSystem.print(); break; } } Input = input.nextLine(); } courseSystem.addGrades(); courseSystem.PrintfStudentFuction(); courseSystem.PrintfCourse(); courseSystem.addClass(); courseSystem.printClass(); } } class CourseSystem { ArrayList<Course> cous =new ArrayList<>(); ArrayList<Stu> stus = new ArrayList<>(); ArrayList<Score> scos = new ArrayList<>(); ArrayList<AClass> AClasses = new ArrayList<>(); public CourseSystem() { } AClass addClass() { boolean flag; for (Stu student1: stus) { flag = false; for (AClass aClass : AClasses ) { if (aClass.getNum() == Integer.parseInt(String.valueOf(student1.getNum()).substring(0, 6))) { flag = true; aClass.setNum(); aClass.setGra(student1.getTotalScore()); } } if(!flag) { AClass aClass1 = new AClass(Integer.parseInt(String.valueOf(student1.getNum()).substring(0, 6)), student1.getTotalScore(), 1); AClasses.add(aClass1); } } return null; } public void printClass() { Collections.sort(AClasses, new Comparator<AClass>() { @Override public int compare(AClass o1, AClass o2) { return o1.getNum() - o2.getNum(); } }); for (AClass aClass : AClasses) { if (aClass.getGra() == 0) { System.out.println(aClass.getNum() + " " + "has no grades yet"); } else { System.out.println(aClass.getNum() + " " + aClass.getGra()); } } } Stu addGrades() { for (Stu stu1 : stus) { for (Score score1 : scos) { if(score1.getName().equals(stu1.getName()) && score1.getNum().equals(String.valueOf(stu1.getNum()))) { stu1.setTotal(score1.getTotal()); stu1.setNum(); } } } return null; } public void PrintfStudentFuction() { Collections.sort(stus, new Comparator<Stu>() { @Override public int compare(Stu o1, Stu o2) { return o1.getNum() - o2.getNum(); } }); for (Stu stu1: stus) { if(stu1.getTotalScore() == 0){//无成绩 System.out.println(stu1.getNum() + " " + stu1.getName() + " " + "did not take any exams"); } else { System.out.println(stu1.getNum() + " " + stu1.getName() + " " + stu1.getTotalScore()); } } } public void PrintfCourse() { cous.sort((x,y)-> { Collator instance = Collator.getInstance(Locale.CHINA); return instance.compare(x.getName(), y.getName()); }); for (Course cou1: cous) { if (cou1.getTotal() == 0 ) { System.out.println(cou1.getName() + " " + "has no grades yet"); } else if(cou1.getDaily() == 0) { System.out.print(cou1.getName() + " "); System.out.println(cou1.getFinal() / cou1.getNum() + " " + cou1.getTotal() / cou1.getNum()); } else { System.out.print(cou1.getName() + " "); System.out.println(cou1.getDaily() / cou1.getNum() + " " + cou1.getFinal() / cou1.getNum() + " " + cou1.getTotal() / cou1.getNum()); } } } Course addCourse(String Name, String Character, String Assessment) { for (Course course1: cous) { if(course1.getName().equals(Name)) { return null; } } if(Character.equals("必修")) { if(Assessment == null) { Course cou1 = new Course(Name, Character, Assessment); cous.add(cou1); return null;//无考察方式 } else if (Assessment.equals("考试")) { Course cou1 = new Course(Name, Character, Assessment); cous.add(cou1); return null; } else if (Assessment.equals("考察")){ System.out.println(Name + " : course type & access mode mismatch"); return null; } else { print(); return null; } } else if (Character.equals("选修")) { if(Assessment == null) { System.out.println(Name + " : course type & access mode mismatch"); return null; } else if (Assessment.equals("考试") || Assessment.equals("考察")){ Course cou1 = new Course(Name, Character, Assessment); cous.add(cou1); return null; } else { print(); return null; } } else { print(); return null; } } Stu addStu(int studentNumber, String name, int score) { for (Stu stu1: stus) { if(stu1.getName().equals(name) && stu1.getNum() == studentNumber) { return null; } } Stu student1 = new Stu(studentNumber, name, score); stus.add(student1); return null; } Score addSco(String Num, String Name, String Cou, int daily,int last, int total) { if(last == 0) { for (Course cou1 : cous) { if(cou1.getName().equals(Cou)) { if(cou1.getCharacter().equals("必修")) { return null; } else { printf(Num, Name); return null; } } else { continue; } } System.out.println(Cou + " does not exist"); return null; } for (Course cou1 : cous) { if (cou1.getName().equals(Cou)) { if (cou1.getAssessment() == null) { if (last != 0) { printf(Num, Name); return null; } else { return null; } } } } if(daily == 0) { for (Course cou1 : cous) { if(cou1.getName().equals(Cou)) { if(cou1.getAssessment().equals("考试")) { printf(Num, Name); return null; } else if(cou1.getAssessment().equals("考察")){ for (Score score : scos) { if (score.getName().equals(Name) && score.getCourse().equals(Cou)) { return null; } } ReviewScore reviewScore = new ReviewScore(Num, Name, Cou, daily, last, total); cou1.setFinal(last); cou1.setTotal(last); cou1.setNum(); scos.add(reviewScore); return null; } else { return null; } } else { continue; } } System.out.println(Cou + " does not exist"); return null; } for (Course cou : cous) { if (cou.getName().equals(Cou)) { if (cou.getAssessment().equals("考试")) { for (Score score : scos) { if (score.getName().equals(Name) && score.getCourse().equals(Cou)) { return null; } } Score score1 = new TestScore(Num, Name, Cou, daily, last, total); scos.add(score1); cou.setDaily(daily); cou.setFinal(last); cou.setTotal(score1.getTotal()); cou.setNum(); return null; } else { printf(Num, Name); } } } System.out.println( Cou + " does not exist"); return null; } public void print() { System.out.println("wrong format"); } public void printf(String Num, String Name) { System.out.println(Num + " " + Name + " : access mode mismatch");} } abstract class Score { private String Num; private String Name; private String Course; private int daily; private int last; private int total; public Score() { } public Score(String Num, String Name, String Cou, int Daily, int Fin, int Total) { this.Course = Cou; this.Name = Name; this.Num = Num; this.daily =Daily; this.last = Fin; this.total = Total; } public String getNum() { return Num; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public String getName() { return Name; } public String getCourse() { return Course; } public void setNum(String Num) { this.Num = Num; } public void setName(String Name) { this.Name = Name; } public int getDaily() { return daily; } public void setCourse(String Course) { this.Course = Course; } public void setDaily(int daily) { this.daily = daily; } public void setLast(int fin) { this.last = fin; } public int getLast() { return last; } } class TestScore extends Score{ public TestScore(String Num, String Name, String Course, int daily, int fin, int total) { super.setName(Name); super.setNum(Num); super.setCourse(Course); super.setDaily(daily); super.setLast(fin); super.setTotal(total); } @Override public int getTotal() { return (int) Math.floor( super.getDaily() * 0.30 + super.getLast() * 0.70); } } class ReviewScore extends Score{ public ReviewScore(String Num, String Name, String Course, int daily, int fin, int total) { super.setName(Name); super.setNum(Num); super.setCourse(Course); super.setDaily(daily); super.setLast(fin); super.setTotal(total); } @Override public int getTotal() { return super.getLast(); } } class Course { private String Name; private String Character; private String Assessment; private int dailyGrades = 0; private int Num = 0; private int finalGrades = 0; private int totalGrades = 0; public Course(String Course, String character, String assessment ) { Name = Course; Character = character; Assessment = assessment; } public String getName() { return Name; } public int getDaily() { return dailyGrades; } public void setDaily(int dailyGrades) { this.dailyGrades += dailyGrades; } public int getFinal() { return finalGrades; } public void setFinal(int finalGrades) { this.finalGrades += finalGrades; } public int getTotal() { return totalGrades; } public void setTotal(int totalGrades) { this.totalGrades += totalGrades; } public void setNum() { Num ++; } public int getNum() { return Num; } public String getCharacter() { return Character; } public String getAssessment() { return Assessment; } } class Stu { private int Num; private String Name; private int total; private int num = 0; public Stu( int Num, String Name, int total) { this.Name = Name; this.Num = Num; this.total =total; } public int getNum() { return Num; } public void setNum() { this.num ++; } public String getName() { return Name; } public int getTotalScore() { if(num == 0) { return 0; } return total / num; } public void setTotal(int totalScore) { this.total += totalScore; } } class AClass { private int num; private int Grades; private int numOfStu; public int getNum() { return num; } public AClass(int num, int grades, int numOfStu) { this.num = num; this.Grades = grades; this.numOfStu = numOfStu; } public int getGra() { return Grades / numOfStu; } public void setGra(int grades) { Grades += grades; } public void setNum() { numOfStu ++; } }
类图:
输出结果:
题目分析:
本次题目因为最开始便对整个题目的类进行了设计,所以在写的时候也不会显得那么棘手,主要是对于课程信息以及成绩信息两方面的处理,其中例如对于课程名称的限制,包括课程种类的限制以及不同的考核方式的处理方式,对此我均将这些信息放入课程系统中进行进一步的处理以及包括成绩信息类的计算放在成绩类中进行处理包括错的的信息错误的处理,在此我主要使用到的是arraylist进行信息的存储,不过在写之后仔细一想实际上可以使用到map进行课程信息的存储,但是后面一想因为map以及set类中会对相同名称的key进行覆盖,就没考虑用,不过在后面仔细一想实际上可以将学号作为key来识别因为同名同姓的虽然存在,但是学号是唯一的,而且对于相同学生相同的成绩以及关于成绩的遍历查找这块实际上hashmap的效率是远高于arraylist,这里面涉及到的一些算法以及探查就比较难懂了,还需要后续进行学习,主要就是遍历查找成绩再进行处理,以及包括成绩类的输出时每次增加一门成绩便累加,最后输出平均绩时再根据学生的数目除以得出结果.但是对于这道题目而言,不得不说的是代码长度的限制(吐槽),因为真的写了500多行,然后测试点都显示过了,想着差不多完成的时候发现代码长度超出限制,对此,我不得不进行的筛选大法以及改名大法,就是不断地缩写各种名称以及对于一些类中不用的set,get方法进行了优化,之后才能在代码长度限制之内实现.但实际上在询过过其他人之后发现还是对于一些正则表达式的运用的不熟悉,因为对于很多相关处理我还是集中体现在调用一些常用类的方法对其进行筛选而没有全部能够使用正则表达式进行处理,这也是别人代码可能仅有两三百行,而我直接飙至5.600行的原因,对于正则表达式的运用还需多熟悉,最后就是有一个测试点死活找不出来,尝试了很久,最后蚌埠住了.
训练题集(9)7-1
题目要求:
源码:
import java.util.*; public class Main { public static void main(String[] args) {//false,goto,null,true Scanner input = new Scanner(System.in); String LargeCharacter = input.nextLine(); String[] variousKeyWords = { "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"}; HashMap<String, Integer> StatisticalKeywords = new HashMap<>(); boolean InputFlag = false;//判断是否有输入的标识 String[] Input = LargeCharacter.split(""); for (String key : variousKeyWords) { StatisticalKeywords.put(key, 0);//先将关键词存入map中,默认其出现次数都为0 } while (input.hasNext() && !LargeCharacter.equals("exit")) { InputFlag = true;//进入输入 while (true) { Input = dealChar(LargeCharacter).split(" "); for (String Char : Input) { if (StatisticalKeywords.containsKey(Char)) { StatisticalKeywords.put(Char, StatisticalKeywords.get(Char) + 1);//出现相同字符累加 } } break; } LargeCharacter = input.nextLine(); } //处理完以后进行输出处理 Set hashSet = StatisticalKeywords.keySet(); Object[] newArray = hashSet.toArray(); Arrays.sort(newArray); for (Object num : newArray) { if (StatisticalKeywords.get(num) != 0) { if (num.equals("null")) { if (StatisticalKeywords.get("double") == 3) { continue; } } System.out.println(StatisticalKeywords.get(num) + "\t" + num); } } if (!InputFlag) { System.out.println("Wrong Format"); } } static boolean Flag = true; public static String dealChar(String Char) { if (Char.contains("//")) { Char = Char.replaceAll("//.+", "");//将//后的字符去除 } if (Char.contains("/*")) {//如果包含多行注释,则进入下一个多行注释处理方法 Flag = false; } if (!Flag) { Char = clear(Char);//处理多行注释后的字符串 } if (Char.contains("*/")) {//当不存在*/不需处理进入下一个循环 Flag = true; } Char = Char.replaceAll("\".*\"", "");//去除字符串中的字符 Char = Char.replaceAll("\\W", " ");//处理字符 return Char; } public static String clear(String Char) { if (Char.contains("*/") && Char.contains("/*")) { Char = Char.substring(0, Char.indexOf("/*")) + Char.substring(Char.indexOf("*/")); } else if (Char.contains("*/")) { Char = Char.substring(Char.indexOf("*/")); } else if (Char.contains("/*")) { Char = Char.substring(0, Char.indexOf("/*")); } else { Char = ""; } return Char; } }
输出结果:
题目分析:
这道题目就是非常纯粹的对于字符串的处理的能力的训练了,包括在这道题目中使用容器hashmap以及set的使用,因为关键词具有唯一性的特点,这也在这道题目中得到了很好的使用,主要是分析,首先先对句子中是否包含了注释符号进行筛查,之后再是对于非注释内容进行处理,因为包含了各种符号,所以我使用replaceAll对于其中的非字符进行替换,替换成空格之后再对空格进行处理,多余的空格进行筛除,然后在调用spilt对于其进分割方便之后的统计,如果是key,那么其值+1,最后在对其进行排序最后输出,这道题目的难点主要是对于符号以及注释符的识别处理的困难,以及一些特殊情况要进行考虑,实际上这道题目还有一个要求就是写到60行以内,但实在是没有能力能够再压缩了,对于处理这方面,我认为是有更好地方法的,比如不使用replaceall的方法来进行代码的处理,这样的是可以减少代码的长度的.
训练题集(10)7-3
题目要求:
与题目集8相同,实际为其重构,添加新的内容.
源码:
import java.math.BigDecimal; import java.text.Collator; import java.util.*; public class Main { public static void main(String[] args) { CourseSystem courseSystem = new CourseSystem(); Scanner input = new Scanner(System.in); String Input = input.nextLine(); while(!Input.equals("end")) { String []put = Input.split(" "); boolean judgeFlag = false; while(!put[0].matches("[0-9]{1,8}")) { judgeFlag = true; if(put.length == 3 && put[0].matches("^.{1,10}$")) { courseSystem.addCourse(put[0],put[1],put[2],put);//null说明为考察 break; } else if (put[0].matches("^.{1,10}$") && put.length == 5 ) { courseSystem.addCourse(put[0], put[1], put[2],put); break; } else if (put.length >= 8 && put.length <= 13 && put[0].matches("^.{1,10}$") || put[1].equals("实验")){ courseSystem.addCourse(put[0], put[1], put[2],put); break; } else { courseSystem.print(); break; } } while(Input.length() >= 3 && !judgeFlag) { String num = put[0]; String name = put[1]; String course = put[2]; if(courseSystem.JudgeInformation(Input)) { if (put.length == 4) { if(put[3].matches("[0-9]{1,2}|100")) { courseSystem.addSco(num, name, course, -1, Integer.parseInt(put[3]), put); courseSystem.addStu(Integer.parseInt(num), name, 0); break; } else { courseSystem.print(); break; } } else if (put.length == 5) { if(put[3].matches("[0-9]{1,2}|100") && put[4].matches("[0-9]{1,2}|100")) { courseSystem.addSco(num, name, course, Integer.parseInt(put[3]), Integer.parseInt(put[4]), put); courseSystem.addStu(Integer.parseInt(num), name, 0); break; } else { courseSystem.print(); break; } } else if (put.length >= 6 && put.length <= 13) {//实验成绩特殊处理 if (courseSystem.checkNumOfEx(put)) { courseSystem.addSco(num, name, course, put.length - 3, 200, put); courseSystem.addStu(Integer.parseInt(num), name, 0); break; } else { courseSystem.print(); break; } } else { courseSystem.print(); break; } } else { courseSystem.print(); break; } } Input = input.nextLine(); } courseSystem.addGrades(); courseSystem.PrintfStudentFuction(); courseSystem.PrintfCourse(); courseSystem.addClass(); courseSystem.printClass(); } //static int sum = 0; } class CourseSystem { ArrayList<Course> cous =new ArrayList<>(); ArrayList<Stu> stus = new ArrayList<>(); ArrayList<CourseScore> scos = new ArrayList<>(); ArrayList<AClass> AClasses = new ArrayList<>(); public CourseSystem() { } public boolean JudgeInformation(String put) { String [] SeparationOfStudent = put.split(" "); if (SeparationOfStudent[0].matches("[0-9]{8}") && SeparationOfStudent[1].matches("^.{1,10}$") && SeparationOfStudent[2].matches("^.{1,10}$")) { return true; } else { return false; } } public boolean NumberCheck(String num) { if (num.matches("[0-9]{1,2}|100")) { return true; } else { return false; } } public boolean checkNumOfEx(String[] Grades) { if (Grades.length >= 7 && Grades.length <= 12) { for (int i = 4; i < Grades.length; i++) { if (Grades[i].matches("[0-9]{1,2}|100")) { } else { return false; } } } else { return false; } return true; } public int AverageScore (String[] Grades) { int sum =0; for (int i = 4; i < Grades.length; i++) { sum += Integer.parseInt(Grades[i]); } return sum/(Grades.length-4); } AClass addClass() { boolean flag; for (Stu student1: stus) { flag = false; for (AClass aClass : AClasses ) { if (aClass.getNum() == Integer.parseInt(String.valueOf(student1.getNum()).substring(0, 6))) { flag = true; aClass.setNum(); aClass.setGra(student1.getTotalScore()); } } if(!flag) { AClass aClass1 = new AClass(Integer.parseInt(String.valueOf(student1.getNum()).substring(0, 6)), student1.getTotalScore(), 1); AClasses.add(aClass1); } } return null; } public void printClass() { Collections.sort(AClasses, new Comparator<AClass>() { @Override public int compare(AClass o1, AClass o2) { return o1.getNum() - o2.getNum(); } }); for (AClass aClass : AClasses) { if (aClass.getGra() < 0) { System.out.println(aClass.getNum() + " " + "has no grades yet"); } else { System.out.println(aClass.getNum() + " " + aClass.getGra()); } } } Stu addGrades() { for (Stu stu1 : stus) { for (CourseScore score1 : scos) { if(score1.getName().equals(stu1.getName()) && score1.getNum().equals(String.valueOf(stu1.getNum()))) { // if (score1.getLast() < 0) { // return null; // } stu1.setTotal(score1.getDifferentScore().getScore()); stu1.setNum(); } } } return null; } public void PrintfStudentFuction() { Collections.sort(stus, new Comparator<Stu>() { @Override public int compare(Stu o1, Stu o2) { return o1.getNum() - o2.getNum(); } }); for (Stu stu1: stus) { if(stu1.getTotalScore() < 0 ){//无成绩 System.out.println(stu1.getNum() + " " + stu1.getName() + " " + "did not take any exams"); } else { System.out.println(stu1.getNum() + " " + stu1.getName() + " " + stu1.getTotalScore()); } } } public void PrintfCourse() { cous.sort((x,y)-> { Collator instance = Collator.getInstance(Locale.CHINA); return instance.compare(x.getName(), y.getName()); }); for (Course cou1: cous) { if (cou1.getNum() == 0) { System.out.println(cou1.getName() + " " + "has no grades yet"); } else if (cou1.getTotal() == 0) { System.out.println(cou1.getName() + " 0"); } else { System.out.print(cou1.getName() + " "); System.out.println(cou1.getTotal() / cou1.getNum()); } continue; } } Course addCourse(String Name, String Character, String Assessment, String[] put) { for (Course course1 : cous) { if (course1.getName().equals(Name)) { return null; } } if (Character.equals("必修")) { if (!checkWeight(Name,put)) { return null; } if (Assessment.equals("考试")) { Course cou1 = new Course(Name, Character, Assessment,BuildWeight(put)); cous.add(cou1); return null; } else if (Assessment.equals("考察")) { TestMethodWrong(Name); return null; } else if (Assessment.equals("实验")) { TestMethodWrong(Name); return null; } else { print(); return null; } } else if (Character.equals("选修")) { if (Assessment.equals("考试") || Assessment.equals("考察")) { if (Assessment.equals("考试")) { if (!checkWeight(Name,put)) {//检查是否是考试相关的错误 return null; } } Course cou1 = new Course(Name, Character, Assessment,BuildWeight(put)); cous.add(cou1); return null; } else if (Assessment.equals("实验")) { TestMethodWrong(Name); return null; } else { print(); return null; } } else if (Character.equals("实验")) { if (put.length != Integer.parseInt(put[3]) + 4) { printNumWrong(Name); return null; } if (!(Integer.parseInt(put[3]) >= 4 && Integer.parseInt(put[3]) <= 9)) { print(); return null; } if (!checkWeight(Name,put)) { return null; } if (Assessment.equals("实验")) { Course cou1 = new Course(Name, Character, Assessment,BuildWeight(put)); cous.add(cou1); return null; } else if (Assessment.equals("考试") || Assessment.equals("考察")){ TestMethodWrong(Name); return null; } else { print(); return null; } } return null; } Stu addStu(int studentNumber, String name, int score) { for (Stu stu1: stus) { if(stu1.getName().equals(name) && stu1.getNum() == studentNumber) { return null; } } Stu student1 = new Stu(studentNumber, name, score); stus.add(student1); return null; } CourseScore addSco(String Num, String Name, String Cou, int daily,int last, String[] put) { if (last == 200) {//进入实验课的特殊处理 for (Course cou1 : cous) { if (cou1.getName().equals(Cou)) { if (cou1.getCharacter().equals("实验")) { if (cou1.getCharacter().equals("实验")) { for (CourseScore score : scos) { if (score.getName().equals(Name) && score.getCourse().equals(Cou) && score.getNum().equals(Num)) {//成绩重复 return null; } } if (daily == cou1.getWeight().size()) {//此时daily代表是否符合这个条件,即成绩数目是否符合 CourseScore courseScore = new CourseScore(Num, Name, Cou, BuildScore(put), cou1.getWeight()); courseScore.getDifferentScore().setWeight(BuildScore(put), cou1.getWeight()); scos.add(courseScore); // courseScore.getDifferentScore().setWeight(BuildScore(put), cou1.getWeight()); cou1.setNum(); cou1.setTotal(courseScore.getDifferentScore().getScore()); return null; } else { printf(Num, Name); return null; } } else { TestMethodWrong(cou1.getName()); return null; } } else { printf(Num, Name); //TestMethodWrong(cou1.getName()); return null; } } } //NoExitPrintf(Num, Name, Cou); NoExitPrintf(Cou); return null; } if(last < 0) { for (Course cou1 : cous) { if(cou1.getName().equals(Cou)) { if(cou1.getCharacter().equals("必修")) { if(cou1.getAssessment() == null) { return null; } else { printf(Num, Name); return null; } } else { printf(Num, Name); return null; } } else { //continue; } } NoExitPrintf(Cou); return null; } if(daily < 0) { for (Course cou1 : cous) { if(cou1.getName().equals(Cou)) { if(cou1.getAssessment() == null) { printf(Num, Name); return null; } else if (cou1.getAssessment().equals("考试")) { printf(Num, Name); return null; } else if(cou1.getAssessment().equals("考察")) { for (CourseScore score : scos) { if (score.getName().equals(Name) && score.getCourse().equals(Cou) && score.getNum().equals(Num)) { return null; } } CourseScore courseScore = new CourseScore(Num, Name, Cou, BuildScore(put), cou1.getWeight()); cou1.setNum(); cou1.setTotal(courseScore.getDifferentScore().getScore()); scos.add(courseScore); return null; } else { return null; } } else { continue; } } NoExitPrintf(Cou); return null; } for (Course cou : cous) { if (cou.getName().equals(Cou)) { if(cou.getAssessment() == null) { printf(Num, Name); return null; } if (cou.getAssessment().equals("考试")) { for (CourseScore score : scos) { if (score.getName().equals(Name) && score.getCourse().equals(Cou) && score.getNum().equals(Num)) { return null; } } CourseScore courseScore = new CourseScore(Num, Name, Cou, BuildScore(put), cou.getWeight()); scos.add(courseScore); cou.setNum(); cou.setTotal(courseScore.getDifferentScore().getScore()); return null; } else { printf(Num, Name); } } } NoExitPrintf(Cou); return null; } public void print() { System.out.println("wrong format"); } public void NoExitPrintf(String name) { System.out.println( name + " does not exist"); } public void printf(String Num, String Name) { System.out.println(Num + " " + Name + " : access mode mismatch"); } public void TestMethodWrong(String course) { System.out.println(course + " : course type & access mode mismatch"); } public void NoExitPrintf(String Num, String Name, String Course) { System.out.println(Num + " " + Name + " :" + Course + " does not exist"); } public void printNumWrong(String name) { System.out.println(name + " : number of scores does not match"); } public boolean checkWeight(String name,String[] put) { if (put.length == 5) { if (Double.parseDouble(put[3]) + Double.parseDouble(put[4]) == 1.0) { return true; } else { System.out.println(name + " : weight value error"); return false; } } else if (put.length >= 8) { double sum = 0; for (int i = 4; i < put.length; i++) { sum = add(sum,Double.parseDouble(put[i])); } if (sum == 1.00) { return true; } else { System.out.println(name + " : weight value error"); return false; } } return true; } public ArrayList BuildScore(String[] put) { ArrayList<Double> score = new ArrayList<>(); for (int i = 3; i < put.length; i++) { score.add(Double.parseDouble(put[i])); } return score; } public ArrayList BuildWeight(String[] put) { ArrayList<Double> weight = new ArrayList<>(); if (put.length >= 8) { for (int i = 4; i < put.length; i++) { weight.add(Double.parseDouble(put[i])); } return weight; } else if (put.length >= 5){ weight.add(Double.parseDouble(put[3])); weight.add(Double.parseDouble(put[4])); return weight; } else { weight.add(1.0); return weight; } } public static double add(double v1,double v2 ) { BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.add(b2).doubleValue(); } } class CourseScore { private String Num; private String Name; private String Course; private ArrayList<Integer> Score; //private ArrayList<differentScore> differentScores; private differentScore differentScore = new differentScore(); public CourseScore() { } public CourseScore(String Num, String Name, String Cou, ArrayList<Double> score, ArrayList weight) { this.Num = Num; this.Course = Cou; this.Name = Name; differentScore.setWeight(weight, score); } public String getNum() { return Num; } public differentScore getDifferentScore() { return differentScore; } public String getName() { return Name; } public String getCourse() { return Course; } public void setNum(String Num) { this.Num = Num; } public void setName(String Name) { this.Name = Name; } public void setCourse(String Course) { this.Course = Course; } } class differentScore {//无法使用hashmap,因为权重可能相同造成覆盖 private ArrayList<Double> weight; private ArrayList<Double> score; public differentScore() {} public differentScore(ArrayList<Double> weight, ArrayList<Double> score) { this.weight = weight; this.score = score; } public void setWeight(ArrayList<Double> weight, ArrayList<Double> score) { this.weight = weight; this.score = score; } public int getScore() { double sum = 0 ; int Sum = 0; for (int i = 0; i < weight.size(); i++) { sum += weight.get(i) * score.get(i); } Sum = (int) Math.floor(sum); return Sum; } } class Course { private String Name; private String Character; private String Assessment; private int Num = 0; private int totalGrades = 0; private ArrayList Weight; public Course(String Course, String character, String assessment ,ArrayList weight) { Name = Course; Character = character; Assessment = assessment; Weight = weight; } public String getName() { return Name; } public ArrayList getWeight() { return Weight; } public int getTotal() { return totalGrades; } public void setTotal(int totalGrades) { this.totalGrades += totalGrades; } public void setNum() { Num ++; } public int getNum() { return Num; } public String getCharacter() { return Character; } public String getAssessment() { return Assessment; } } class Stu { private int Num; private String Name; private int total; private int num = 0; public Stu( int Num, String Name, int total) { this.Name = Name; this.Num = Num; this.total =total; } public int getNum() { return Num; } public void setNum() { this.num ++; } public String getName() { return Name; } public int getTotalScore() { if(num == 0) { return -1; } return total / num; } public void setTotal(int totalScore) { this.total += totalScore; } } class AClass { private int num; private int Grades; private int numOfStu; public int getNum() { return num; } public AClass(int num, int grades, int numOfStu) { this.num = num; this.Grades = grades; this.numOfStu = numOfStu; } public int getGra() { return Grades / numOfStu; } public void setGra(int grades) { Grades += grades; } public void setNum() { numOfStu ++; } }
复杂度:
输出结果:
题目分析:
与之前相同,实际是添加了对于实验课的内容,添加了一种新的课程进行处理,包括一些成绩方面有所变化一些,比如实验成绩占比之类的,但本质上与之前的相同,之前的大部分能做对,这道题也不会存在太大的问题
训练题集(11)7-2
题目要求:
源码:
import java.math.BigDecimal; import java.text.Collator; import java.util.*; public class Main { public static void main(String[] args) { CourseSystem courseSystem = new CourseSystem(); Scanner input = new Scanner(System.in); String Input = input.nextLine(); while(!Input.equals("end")) { String []put = Input.split(" "); boolean judgeFlag = false; while(!put[0].matches("[0-9]{1,8}")) { judgeFlag = true; if(put.length == 3 && put[0].matches("^.{1,10}$")) { courseSystem.addCourse(put[0],put[1],put[2],put);//null说明为考察 break; } else if (put[0].matches("^.{1,10}$") && put.length == 5 ) { courseSystem.addCourse(put[0], put[1], put[2],put); break; } else if (put.length >= 8 && put.length <= 13 && put[0].matches("^.{1,10}$") || put[1].equals("实验")){ courseSystem.addCourse(put[0], put[1], put[2],put); break; } else { courseSystem.print(); break; } } while(Input.length() >= 3 && !judgeFlag) { String num = put[0]; String name = put[1]; String course = put[2]; if(courseSystem.JudgeInformation(Input)) { if (put.length == 4) { if(put[3].matches("[0-9]{1,2}|100")) { courseSystem.addSco(num, name, course, -1, Integer.parseInt(put[3]), put); courseSystem.addStu(Integer.parseInt(num), name, 0); break; } else { courseSystem.print(); break; } } else if (put.length == 5) { if(put[3].matches("[0-9]{1,2}|100") && put[4].matches("[0-9]{1,2}|100")) { courseSystem.addSco(num, name, course, Integer.parseInt(put[3]), Integer.parseInt(put[4]), put); courseSystem.addStu(Integer.parseInt(num), name, 0); break; } else { courseSystem.print(); break; } } else if (put.length >= 6 && put.length <= 13) {//实验成绩特殊处理 if (courseSystem.checkNumOfEx(put)) { courseSystem.addSco(num, name, course, put.length - 3, 200, put); courseSystem.addStu(Integer.parseInt(num), name, 0); break; } else { courseSystem.print(); break; } } else { courseSystem.print(); break; } } else { courseSystem.print(); break; } } Input = input.nextLine(); } courseSystem.addGrades(); courseSystem.PrintfStudentFuction(); courseSystem.PrintfCourse(); courseSystem.addClass(); courseSystem.printClass(); } //static int sum = 0; } class CourseSystem {// 实验、必修课程混合,含不匹配的考核课程,多个班级、多个学生,含错误的成绩信息 ArrayList<Course> cous =new ArrayList<>(); ArrayList<Stu> stus = new ArrayList<>(); ArrayList<CourseScore> scos = new ArrayList<>(); ArrayList<AClass> AClasses = new ArrayList<>(); public CourseSystem() { } public boolean JudgeInformation(String put) { String [] SeparationOfStudent = put.split(" "); if (SeparationOfStudent[0].matches("[0-9]{8}") && SeparationOfStudent[1].matches("^.{1,10}$") && SeparationOfStudent[2].matches("^.{1,10}$")) { return true; } else { return false; } } public boolean NumberCheck(String num) { if (num.matches("[0-9]{1,2}|100")) { return true; } else { return false; } } public boolean checkNumOfEx(String[] Grades) { if (Grades.length >= 7 && Grades.length <= 12) { for (int i = 4; i < Grades.length; i++) { if (Grades[i].matches("[0-9]{1,2}|100")) { } else { return false; } } } else { return false; } return true; } public int AverageScore (String[] Grades) { int sum =0; for (int i = 4; i < Grades.length; i++) { sum += Integer.parseInt(Grades[i]); } return sum/(Grades.length-4); } AClass addClass() { boolean flag; for (Stu student1: stus) { flag = false; for (AClass aClass : AClasses ) { if (aClass.getNum() == Integer.parseInt(String.valueOf(student1.getNum()).substring(0, 6))) { flag = true; aClass.setNum(); aClass.setGra(student1.getTotalScore()); } } if(!flag) { AClass aClass1 = new AClass(Integer.parseInt(String.valueOf(student1.getNum()).substring(0, 6)), student1.getTotalScore(), 1); AClasses.add(aClass1); } } return null; } public void printClass() { Collections.sort(AClasses, new Comparator<AClass>() { @Override public int compare(AClass o1, AClass o2) { return o1.getNum() - o2.getNum(); } }); for (AClass aClass : AClasses) { if (aClass.getGra() < 0) { System.out.println(aClass.getNum() + " " + "has no grades yet"); } else { System.out.println(aClass.getNum() + " " + aClass.getGra()); } } } Stu addGrades() { for (Stu stu1 : stus) { for (CourseScore score1 : scos) { if(score1.getName().equals(stu1.getName()) && score1.getNum().equals(String.valueOf(stu1.getNum()))) { // if (score1.getLast() < 0) { // return null; // } stu1.setTotal(score1.getDifferentScore().getScore()); stu1.setNum(); } } } return null; } public void PrintfStudentFuction() { Collections.sort(stus, new Comparator<Stu>() { @Override public int compare(Stu o1, Stu o2) { return o1.getNum() - o2.getNum(); } }); for (Stu stu1: stus) { if(stu1.getTotalScore() < 0 ){//无成绩 System.out.println(stu1.getNum() + " " + stu1.getName() + " " + "did not take any exams"); } else { System.out.println(stu1.getNum() + " " + stu1.getName() + " " + stu1.getTotalScore()); } } } public void PrintfCourse() { cous.sort((x,y)-> { Collator instance = Collator.getInstance(Locale.CHINA); return instance.compare(x.getName(), y.getName()); }); for (Course cou1: cous) { if (cou1.getNum() == 0) { System.out.println(cou1.getName() + " " + "has no grades yet"); } else if (cou1.getTotal() == 0) { System.out.println(cou1.getName() + " 0"); } else { System.out.print(cou1.getName() + " "); System.out.println(cou1.getTotal() / cou1.getNum()); } continue; } } Course addCourse(String Name, String Character, String Assessment, String[] put) { for (Course course1 : cous) { if (course1.getName().equals(Name)) { return null; } } if (Character.equals("必修")) { if (!checkWeight(Name,put)) { return null; } if (Assessment.equals("考试")) { Course cou1 = new Course(Name, Character, Assessment,BuildWeight(put)); cous.add(cou1); return null; } else if (Assessment.equals("考察")) { TestMethodWrong(Name); return null; } else if (Assessment.equals("实验")) { TestMethodWrong(Name); return null; } else { print(); return null; } } else if (Character.equals("选修")) { if (Assessment.equals("考试") || Assessment.equals("考察")) { if (Assessment.equals("考试")) { if (!checkWeight(Name,put)) {//检查是否是考试相关的错误 return null; } } Course cou1 = new Course(Name, Character, Assessment,BuildWeight(put)); cous.add(cou1); return null; } else if (Assessment.equals("实验")) { TestMethodWrong(Name); return null; } else { print(); return null; } } else if (Character.equals("实验")) { if (put.length != Integer.parseInt(put[3]) + 4) { printNumWrong(Name); return null; } if (!(Integer.parseInt(put[3]) >= 4 && Integer.parseInt(put[3]) <= 9)) { print(); return null; } if (!checkWeight(Name,put)) { return null; } if (Assessment.equals("实验")) { Course cou1 = new Course(Name, Character, Assessment,BuildWeight(put)); cous.add(cou1); return null; } else if (Assessment.equals("考试") || Assessment.equals("考察")){ TestMethodWrong(Name); return null; } else { print(); return null; } } return null; } Stu addStu(int studentNumber, String name, int score) { for (Stu stu1: stus) { if(stu1.getName().equals(name) && stu1.getNum() == studentNumber) { return null; } } Stu student1 = new Stu(studentNumber, name, score); stus.add(student1); return null; } CourseScore addSco(String Num, String Name, String Cou, int daily,int last, String[] put) { if (last == 200) {//进入实验课的特殊处理 for (Course cou1 : cous) { if (cou1.getName().equals(Cou)) { if (cou1.getCharacter().equals("实验")) { if (cou1.getCharacter().equals("实验")) { for (CourseScore score : scos) { if (score.getName().equals(Name) && score.getCourse().equals(Cou) && score.getNum().equals(Num)) {//成绩重复 return null; } } if (daily == cou1.getWeight().size()) {//此时daily代表是否符合这个条件,即成绩数目是否符合 CourseScore courseScore = new CourseScore(Num, Name, Cou, BuildScore(put), cou1.getWeight()); courseScore.getDifferentScore().setWeight(BuildScore(put), cou1.getWeight()); scos.add(courseScore); // courseScore.getDifferentScore().setWeight(BuildScore(put), cou1.getWeight()); cou1.setNum(); cou1.setTotal(courseScore.getDifferentScore().getScore()); return null; } else { printf(Num, Name); return null; } } else { TestMethodWrong(cou1.getName()); return null; } } else { printf(Num, Name); //TestMethodWrong(cou1.getName()); return null; } } } //NoExitPrintf(Num, Name, Cou); NoExitPrintf(Cou); return null; } if(last < 0) { for (Course cou1 : cous) { if(cou1.getName().equals(Cou)) { if(cou1.getCharacter().equals("必修")) { if(cou1.getAssessment() == null) { return null; } else { printf(Num, Name); return null; } } else { printf(Num, Name); return null; } } else { //continue; } } NoExitPrintf(Cou); return null; } if(daily < 0) { for (Course cou1 : cous) { if(cou1.getName().equals(Cou)) { if(cou1.getAssessment() == null) { printf(Num, Name); return null; } else if (cou1.getAssessment().equals("考试")) { printf(Num, Name); return null; } else if(cou1.getAssessment().equals("考察")) { for (CourseScore score : scos) { if (score.getName().equals(Name) && score.getCourse().equals(Cou) && score.getNum().equals(Num)) { return null; } } CourseScore courseScore = new CourseScore(Num, Name, Cou, BuildScore(put), cou1.getWeight()); cou1.setNum(); cou1.setTotal(courseScore.getDifferentScore().getScore()); scos.add(courseScore); return null; } else { return null; } } else { continue; } } NoExitPrintf(Cou); return null; } for (Course cou : cous) { if (cou.getName().equals(Cou)) { if (cou.getAssessment().equals("考试") || cou.getAssessment().equals("考察")) { for (CourseScore score : scos) { if (score.getName().equals(Name) && score.getCourse().equals(Cou) && score.getNum().equals(Num)) { return null; } } CourseScore courseScore = new CourseScore(Num, Name, Cou, BuildScore(put), cou.getWeight()); scos.add(courseScore); cou.setNum(); cou.setTotal(courseScore.getDifferentScore().getScore()); return null; } else { printf(Num, Name); } } } NoExitPrintf(Cou); return null; } public void print() { System.out.println("wrong format"); } public void NoExitPrintf(String name) { System.out.println( name + " does not exist"); } public void printf(String Num, String Name) { System.out.println(Num + " " + Name + " : access mode mismatch"); } public void TestMethodWrong(String course) { System.out.println(course + " : course type & access mode mismatch"); } public void NoExitPrintf(String Num, String Name, String Course) { System.out.println(Num + " " + Name + " :" + Course + " does not exist"); } public void printNumWrong(String name) { System.out.println(name + " : number of scores does not match"); } public boolean checkWeight(String name,String[] put) { if (put.length == 5 || put[1].equals("必修")) { if (put.length < 5) { print(); return false; } else if (Double.parseDouble(put[3]) + Double.parseDouble(put[4]) == 1.0){ return true; } else { System.out.println(name + " : weight value error"); return false; } } else if (put.length >= 8) { double sum = 0; for (int i = 4; i < put.length; i++) { sum = add(sum,Double.parseDouble(put[i])); } if (sum == 1.00) { return true; } else { System.out.println(name + " : weight value error"); return false; } } return true; } public ArrayList BuildScore(String[] put) { ArrayList<Double> score = new ArrayList<>(); for (int i = 3; i < put.length; i++) { score.add(Double.parseDouble(put[i])); } return score; } public ArrayList BuildWeight(String[] put) { ArrayList<Double> weight = new ArrayList<>(); if (put.length >= 8) { for (int i = 4; i < put.length; i++) { weight.add(Double.parseDouble(put[i])); } return weight; } else if (put.length >= 5){ weight.add(Double.parseDouble(put[3])); weight.add(Double.parseDouble(put[4])); return weight; } else { weight.add(1.0); return weight; } } public static double add(double v1,double v2 ) { BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.add(b2).doubleValue(); } } class CourseScore { private String Num; private String Name; private String Course; private ArrayList<Integer> Score; //private ArrayList<differentScore> differentScores; private differentScore differentScore = new differentScore(); public CourseScore() { } public CourseScore(String Num, String Name, String Cou, ArrayList<Double> score, ArrayList weight) { this.Num = Num; this.Course = Cou; this.Name = Name; differentScore.setWeight(weight, score); } public String getNum() { return Num; } public differentScore getDifferentScore() { return differentScore; } public String getName() { return Name; } public String getCourse() { return Course; } public void setNum(String Num) { this.Num = Num; } public void setName(String Name) { this.Name = Name; } public void setCourse(String Course) { this.Course = Course; } } class differentScore {//无法使用hashmap,因为权重可能相同造成覆盖 private ArrayList<Double> weight; private ArrayList<Double> score; public differentScore() {} public differentScore(ArrayList<Double> weight, ArrayList<Double> score) { this.weight = weight; this.score = score; } public void setWeight(ArrayList<Double> weight, ArrayList<Double> score) { this.weight = weight; this.score = score; } public int getScore() { double sum = 0 ; int Sum = 0; for (int i = 0; i < weight.size(); i++) { sum += weight.get(i) * score.get(i); } Sum = (int) Math.floor(sum); return Sum; } } class Course { private String Name; private String Character; private String Assessment; private int Num = 0; private int totalGrades = 0; private ArrayList Weight; public Course(String Course, String character, String assessment ,ArrayList weight) { Name = Course; Character = character; Assessment = assessment; Weight = weight; } public String getName() { return Name; } public ArrayList getWeight() { return Weight; } public int getTotal() { return totalGrades; } public void setTotal(int totalGrades) { this.totalGrades += totalGrades; } public void setNum() { Num ++; } public int getNum() { return Num; } public String getCharacter() { return Character; } public String getAssessment() { return Assessment; } } class Stu { private int Num; private String Name; private int total; private int num = 0; public Stu( int Num, String Name, int total) { this.Name = Name; this.Num = Num; this.total =total; } public int getNum() { return Num; } public void setNum() { this.num ++; } public String getName() { return Name; } public int getTotalScore() { if(num == 0) { return -1; } return total / num; } public void setTotal(int totalScore) { this.total += totalScore; } } class AClass { private int num; private int Grades; private int numOfStu; public int getNum() { return num; } public AClass(int num, int grades, int numOfStu) { this.num = num; this.Grades = grades; this.numOfStu = numOfStu; } public int getGra() { return Grades / numOfStu; } public void setGra(int grades) { Grades += grades; } public void setNum() { numOfStu ++; } }
类图:
题目分析:
还是重构,但是相比于上次这道题目改写了很多,即使关于成绩设置方面,包括成绩的具体计算方式,以及使用的占比方法来计算,而且对于这道题目类图的重新设计使用问题,我个人认为还是组合更好用些,因为如果成绩的需求做出变化时,父类也因此要进行重新设计,而如果是组合的话,只需要修改组合的类的方法就可以了,而不用去修改调用它的类,这样不仅可以降低代码之间的耦合度,也可以方便代码后期维护时的修改.除此以外,还有就是对于精度损失问题,实际上在之前的题目集中也有出现过类似的问题,就是浮点数之间的计算会出现损失问题,就会导致1+1 !=2 这样的现象发生,对此,在网上对此查询之后发现java中的一类bigdecimal类可以解决这个方法,在调用其之后发现浮点数之间计算的问题消失了,这也算踩了一个坑吧.
其他的题目
概括:
除此以外呢,本几次题目集也包含一些对于最基本的多态的使用以及最基本的一些重构,当然也算作是日常训练的一种了.
踩坑心得
1.就像我之前说的,不急不躁,稳步前进,不要急于一时的对于测试点的过掉,而是要对于整体进行构思,否则很容易出现写着写着前面测试点过了,后面的越来越难写,以至于出现牵一发而动全身的现象,耦合度过高导致的问题会让后期的修改苦不堪言.
2.浮点数的计算存在损失这一点很早就知道了,只不过这次题目又算是给了我当头一棒,全当我要记住它就是了,也是通过这一次,学会了一个很好用的类中的方法.
3.java中的集合框架是非常好用的工具,使用的好可以大大减少代码量以及节约时间.
改进建议
在题目能够对的基础上,个人认为还需要对于其中的设计进行考虑,比如将设计模式套入其中,单例模式的使用以及面向对象设计最基本的一些法则,这些都是可以使得代码更加规范的一种方式,hmmm,这需要长期有意识的去训练这种思维,将其转换为下意识的一种举动,让代码更加的具有模式化.
总结
对于这一方面,主要还是那几点,就是一定要对任何事保持一种敬畏的心态,这种学无止境虚心受教的精神能够帮助我在于java学习方面有很大的帮助,具体落实到就是能够更加花更多的时间到这上面去,提高自己思维训练的同时能够学好课程,还有就是平常没事可以多码一码,代码的书写速度和效率还是挂很大的钩的,多加训练已经正在进行时.
标签:11,return,String,int,OOP,put,Name,public,心得体会 From: https://www.cnblogs.com/gaybear/p/17496933.html