pta7,8加期末

发布时间 2023-12-09 22:11:20作者: ljxbxx

1.前言

在当今信息爆炸的时代,Java作为一门强大且广泛应用的编程语言,已经成为了众多开发者的首选。它的跨平台性、健壮性以及丰富的生态系统,使得它在企业级应用、移动应用、大数据处理等领域都有着广泛的应用。

本博客我们将会深入探讨Java语言的基础概念和核心特性,如面向对象编程、多线程、异常处理等,以帮助读者建立坚实的编程基础。希望您在这里能够获取到有价值的知识,并与我们一起共同成长。让我们开始这段Java的旅程吧!

关于这三次pta作业做出总结,总的来说三次作业每一次都比上一次更难,难度依次上升。这最后三次的作业集也给我了很大的挑战。

2. 设计与分析

7-1 容器-ArrayList-排序

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

class Student {
    String id;
    String name;
    int mathScore;
    int physicsScore;

    public Student(String id, String name, int mathScore, int physicsScore) {
        this.id = id;
        this.name = name;
        this.mathScore = mathScore;
        this.physicsScore = physicsScore;
    }

    public int getScoreSum() {
        return mathScore + physicsScore;
    }

    @Override
    public String toString() {
        return id + " " + name + " " + getScoreSum();
    }
}

public class Main {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            String input = scanner.nextLine();
            if (input.equals("end")) {
                break;
            }

            String[] data = input.split(" ");
            String id = data[0];
            String name = data[1];
            int mathScore = Integer.parseInt(data[2]);
            int physicsScore = Integer.parseInt(data[3]);

            Student student = new Student(id, name, mathScore, physicsScore);
            students.add(student);
        }

        Collections.sort(students, Comparator.comparingInt(Student::getScoreSum).reversed());

        for (Student student : students) {
            System.out.println(student);
        }
    }
}

  要点:一个简单的学生成绩排序程序。

7-2 课程成绩统计程序-3

  1 import java.text.Collator;
  2 import java.util.*;
  3 import java.util.regex.Matcher;
  4 import java.util.regex.Pattern;
  5 public class Main {
  6     public static void main(String[] args)
  7     {
  8         Scanner in = new Scanner(System.in);
  9         School school = new School();
 10         String a;
 11         while(!(a=in.nextLine()).equals("end"))
 12         {
 13             school.parseInput(a);
 14         }
 15         school.showStudent();
 16         school.showCourse();
 17         school.showClass();
 18     }
 19 }
 20 class InputMatching {
 21     static String stuNumMatching = "[0-9]{8}";//8个0-9的数字
 22     static String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
 23     static String scoreMatching = "([1-9]?[0-9]|100)";
 24     static String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
 25     static String courseTypeMatching = "(选修|必修|实验)";
 26     static String checkCourseTypeMatching = "(考试|考察|实验)";
 27     //courseInput用于定义课程信息模式(正则表达式)
 28     static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching;
 29     static String courseInput1 = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching+" "+"[0-9]+(\\.[0-9]+)*(\\s[0-9]+(\\.[0-9]+)*\\s?)*";
 30     //scoreInput用于定义成绩信息模式(正则表达式)
 31     static String scoreInput1 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + " "+scoreMatching;
 32     static String scoreInput2 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching ;
 33     public static int matchingInput(String s) {
 34         if (matchingCourse(s)) {
 35             return 1;
 36         }
 37         if (matchingScore(s)) {
 38             return 2;
 39         }//这里还是 先判断上面的两种情况 先判断后再根据输入 去跳转到加入成绩的那一部分
 40         String [] s1 = s.split(" ");
 41         if(s1.length>5)
 42         {
 43             return 2;//在这里直接返回对应的值
 44         }
 45         //这里偷一下懒 不选择正则表达式 直接根据字符串的长度去判断
 46         return 0;
 47     }
 48     private static boolean matchingCourse(String s) {
 49         return s.matches(courseInput)||s.matches(courseInput1);//判断课程信息
 50     }
 51     private static boolean matchingScore(String s) {
 52         return s.matches(scoreInput1)||s.matches(scoreInput2);//判断学生成绩信息
 53     }
 54 }
 55 class Course implements Comparable<Course>
 56 {
 57     String allXingxi;
 58     String name;//记录课程名字
 59     String mode;//记录课程性质
 60     String type;//考查的方式
 61     int chooseCount;//记录多少人选择 这门课程
 62     float totalMark;//这个是这门课的分数
 63     float normalMark;//这个是总的平时成绩 有的科目有 有的科目没有
 64     float finallyMark;//这个是最后的考试成绩 有的科目有
 65     Course(String name, String mode, String type,String allow)
 66     {
 67         this.name = name;
 68         this.mode = mode;
 69         this.type = type;
 70         this.allXingxi = allow;
 71         this.totalMark = 0;
 72         this.normalMark = 0;
 73         this.finallyMark = 0;
 74         this.chooseCount = 0;
 75     }
 76     void setMark(float normalMark,float finallyMark)
 77     {
 78         this.normalMark += normalMark;
 79         this.finallyMark += finallyMark;
 80         this.chooseCount++;
 81     }
 82     void setMark(float finallyMark)
 83     {
 84         this.finallyMark+=finallyMark;
 85         this.chooseCount++;
 86     }
 87     @Override
 88     public int compareTo(Course o) {
 89         Collator collator= Collator.getInstance(Locale.CHINA);
 90         return collator.compare(this.name,o.name);
 91     }
 92     void courseShow()
 93     {
 94         setGrade();
 95         if(this.chooseCount==0)
 96         {
 97             System.out.println(this.name+" has no grades yet");
 98         }
 99         else
100         {
101             if(this.type.equals("实验"))
102             {
103                 System.out.println(this.name+" "+(int)this.finallyMark);
104                 return ;
105             }
106             if(this.normalMark == 0)
107             {
108                 System.out.println(this.name+" "+(int)this.finallyMark+" "+(int)this.finallyMark);
109             }
110             else
111             {
112                 System.out.println(this.name+" "+(int)this.normalMark+" "+(int)this.finallyMark+" "+(int)this.totalMark);
113             }
114         }
115     }
116     void setGrade()
117     {
118         if(this.chooseCount==0){}
119         else
120         {
121             this.normalMark = this.normalMark/this.chooseCount;
122             this.finallyMark = this.finallyMark / this.chooseCount;
123             this.totalMark = this.normalMark+this.finallyMark;
124         }
125     }
126 }
127 class Coursemenu
128 {
129     ArrayList<Course> classmenu= new ArrayList<>();
130     void addCourse(Course newCourse)
131     {
132         classmenu.add(newCourse);
133     }
134     Course searchCourse(String name)
135     {
136         for (Course c : classmenu)
137         {
138             if (c.name.equals(name))
139                 return c;
140         }
141         return null;
142     }
143 }
144 class Class implements Comparable<Class>
145 {
146     boolean hasChooseErrorStudents;
147     String num;
148     double totalMark;
149     int studentNum;
150     Class(String num)
151     {
152         this.num = num;
153         this.studentNum=0;
154         this.totalMark=0;
155         this.hasChooseErrorStudents = false;
156     }
157     ArrayList <Student> studentmenu = new ArrayList<>();
158     Student searchStudent(String studentNum)
159     {
160         for (Student student : this.studentmenu) {
161             if (student.num.equals(studentNum))
162                 return student;
163         }
164         return null;
165     }
166     void addStudent(Student currentStudent)
167     {
168         this.studentmenu.add(currentStudent);
169         this.studentNum++;
170     }
171     void setMark(float mark1,float mark2)
172     {
173         this.totalMark += (mark1 +mark2);
174     }
175     void setMark(float mark1)
176     {
177         this.totalMark += mark1;
178     }
179     @Override
180     public int compareTo(Class o) {
181         return this.num.compareTo(o.num);
182     }
183     void classShow()
184     {
185         setGrade();
186         if(this.hasChooseErrorStudents)
187         {
188             return ;
189         }
190         if(this.studentNum==0||this.totalMark==0)
191         {
192             System.out.println(this.num + " has no grades yet");
193         }
194         else
195         {
196             System.out.println(this.num+" "+(int)this.totalMark);
197         }
198     }
199     void setGrade()
200     {
201         if(this.studentNum == 0){}
202         else
203         {
204             for(int i=0 ;i<this.studentmenu.size() ;i++)
205             {
206                 if(this.studentmenu.get(i).hasChooseError)
207                 {
208                     this.hasChooseErrorStudents = true;
209                     return ;
210                 }
211                 if(this.studentmenu.get(i).countCourse>1)
212                 {
213                     this.studentNum += this.studentmenu.get(i).countCourse-1;
214                 }
215             }
216             this.totalMark = this.totalMark/this.studentNum;
217         }
218     }
219 }
220 class Student implements Comparable<Student>
221 {
222     boolean hasChooseError;
223     String num;
224     String name;
225     float mark;//记录分数的代码
226     int countCourse;//记录课程数量
227     ArrayList<Course> courses = new ArrayList<>();
228     Student(String num,String name)
229     {
230         this.num = num;
231         this.name = name;
232         this.mark = 0;
233         this.countCourse = 0;
234         this.hasChooseError = false;
235     }
236     void addCourse(Course course)
237     {
238         this.courses.add(course);
239         this.countCourse++;
240     }
241     void setMark(float  mark1,float mark2)
242     {
243         this.mark += (mark1 + mark2);
244     }
245     void setMark(float mark1)
246     {
247         this.mark += mark1;//考察成绩就是最后成绩
248     }
249 
250     @Override
251     public int compareTo(Student o) {
252         return this.num.compareTo(o.num);
253     }
254     void stuShow()
255     {
256         setGrade();
257         if(this.hasChooseError == true)
258         {
259             return ;
260         }
261         if(this.mark==-1)
262         {
263             System.out.println(this.num+" "+this.name+" did not take any exams");
264         }
265         else
266         {
267             System.out.println(this.num+" "+this.name+" "+(int)(this.mark));
268         }
269     }
270     void setGrade()
271     {
272         if(this.countCourse==0)
273         {
274             this.mark=-1;
275         }
276         else
277         {
278             this.mark=this.mark/this.countCourse;
279         }
280     }
281 }
282 class School
283 {
284     Class currentClass ;//临时班级
285     Student currentStudent ;//临时学生
286     Course currentCourse;//临时课
287     ArrayList<Class> classes = new ArrayList<>();
288     ArrayList<Student> students = new ArrayList<>();
289     Coursemenu coursemenu=new Coursemenu();//这个是用来记录已经存在的课程信息
290     void parseInput(String a)
291     {
292         switch (InputMatching.matchingInput(a)) {
293             case 0: System.out.println("wrong format");break;
294             case 1: addCourse(a);break;
295             case 2: addScore(a);break;
296         }
297     }
298     boolean isOne(String a)
299     {
300         String [] c = a.split(" ");
301         String regex = "\\d+(\\.\\d+)?";
302         // 编译正则表达式
303         Pattern pattern = Pattern.compile(regex);
304         // 匹配字符串并计算总和
305         Matcher matcher = pattern.matcher(a);
306         float sum = 0;
307         while (matcher.find()) {
308             float num = Float.parseFloat(matcher.group());
309             sum += num;
310         }
311         sum -= (float)Integer.parseInt(c[3]);
312         if(sum == 1.0f)
313             return true;
314         else
315             return false;
316     }
317     boolean isTwo(String a)
318     {
319         String regex = "\\d+(\\.\\d+)?";
320         // 编译正则表达式
321         Pattern pattern = Pattern.compile(regex);
322         // 匹配字符串并计算总和
323         Matcher matcher = pattern.matcher(a);
324         float sum = 0;
325         while (matcher.find()) {
326             float num = Float.parseFloat(matcher.group());
327             sum += num;
328         }
329         if(sum == 1.0f)
330             return true;
331         else
332             return false;
333     }
334     void addCourse(String a)
335     {
336         //此时不必去判断输入的课程是不是正确的在调用该方法之前就已经判断好了的
337         String [] b = a.split(" ");//此时传入的是 java 必修 考试
338         currentCourse = coursemenu.searchCourse(b[0]);//寻找已有的课程表里面是不是有这门课
339         if(currentCourse == null)//如果没有这门课的话 那么就添加
340         {
341             //" "+"[0-9]+(\\.[0-9]+)*(\s[0-9]+(\\.[0-9]+)*\s?)*"
342             if(a.matches("\\S* 实验* 实验 [0-9]+(\\.[0-9]+)*(\\s[0-9]+(\\.[0-9]+)*\\s?)*"))
343             {
344                 String [] oop = a.split(" ");
345                 if(Integer.parseInt(oop[3]) == oop.length-4)
346                 {
347                     if(Integer.parseInt(oop[3])>=4&&Integer.parseInt(oop[3])<=9) {
348                         if (isOne(a)) {
349                             currentCourse = new Course(b[0], b[1], b[2], a);
350                             coursemenu.addCourse(currentCourse);
351                         } else {
352                             System.out.println(b[0] + " : weight value error");
353                         }
354                     }
355                     else{
356                         System.out.println("wrong format");
357                     }
358                 }
359                 else{
360                     System.out.println(b[0] + " : number of scores does not match");
361                 }
362                 return ;//结束当前的函数
363             }
364             else if(a.matches("\\S* 实验* 考试 [0-9]+(\\.[0-9]+)*(\\s[0-9]+(\\.[0-9]+)*\\s?)*")||a.matches("\\S* 实验* 考察 [0-9]+(\\.[0-9]+)*(\\s[0-9]+(\\.[0-9]+)*\\s?)*"))
365             {
366                 System.out.println(b[0] + " : course type & access mode mismatch");
367                 return ;
368             }
369             if (a.matches("\\S* 必修* 考察 [0-9]+(\\.[0-9]+)*(\\s[0-9]+(\\.[0-9]+)*\\s?)*"))//不符合的情况
370             {
371                 System.out.println(b[0] + " : course type & access mode mismatch");
372             }
373             else
374             {
375                 if (a.matches("\\S* 必修* 考试 [0-9]+(\\.[0-9]+)*(\\s[0-9]+(\\.[0-9]+)*\\s?)*"))//"^java\\s+必修\\s+考试\\s+(\\d+(\\.\\d+)?\\s+){2}$";
376                 {
377                     if(isTwo(a)) {
378                         currentCourse = new Course(b[0], b[1], b[2], a);
379                     }
380                     else{
381                         System.out.println("wrong format");
382                         return;
383                     }
384                 } else if (a.matches("\\S* 选修* 考试 [0-9]+(\\.[0-9]+)*(\\s[0-9]+(\\.[0-9]+)*\\s?)*"))
385                 {
386                     if(isTwo(a)) {
387                         currentCourse = new Course(b[0], b[1], b[2], a);
388                     }
389                     else{
390                         System.out.println("wrong format");
391                         return;
392                     }
393                 } else if (a.matches("\\S* 选修* 考察"))
394                 {
395                     currentCourse = new Course(b[0],b[1],b[2],a);
396                 }
397                 coursemenu.addCourse(currentCourse);
398             }
399         }
400     }
401     void addScore(String a)
402     {
403         String[] s=a.split(" ");
404         String classNum = s[0].substring(0, 6);
405         String studentNum = s[0];
406         String studentName = s[1];
407         String courseName = s[2];
408         if(searchClass(classNum)==null)//如果不存在这个班级的话 那么我们就要新建一个班级
409         {
410 
411             currentClass = new Class(classNum);
412             addClass(currentClass);
413 
414             currentStudent = new Student(studentNum, studentName);
415             currentClass.addStudent(currentStudent);//放入指定班级里面去
416             students.add(currentStudent);//把当前学生加入到学生信息表中 方便之后的输出
417             solveGrade(a);//处理成绩的问题
418         }
419         else//如果这个班存在的话 不用再判断有无学生
420         {
421             currentClass = searchClass(classNum);//找到班级 如果没有这个班的话 就再上面创建这个班级
422             if(currentClass.searchStudent(studentNum) == null)
423             {
424                 currentStudent = new Student(studentNum,studentName);
425                 currentClass.addStudent(currentStudent);
426                 if(students.contains(currentStudent)){}
427                 else{
428                     students.add(currentStudent);
429                 }
430             }
431             else
432             {
433                 currentStudent = currentClass.searchStudent(studentNum);
434                 if(students.contains(currentStudent)){}
435                 else{
436                     students.add(currentStudent);
437                 }
438             }
439             solveGrade(a);
440         }
441     }
442     Class searchClass(String classNum)
443     {
444         for (Class aClass : classes) {
445             if (aClass.num.equals(classNum))
446                 return aClass;
447         }
448         return  null;
449     }
450     void addClass(Class currentClass)
451     {
452         classes.add(currentClass);
453     }
454     void solveGrade(String a)
455     {
456         String [] b = a.split(" ");
457 
458         if(b.length >5) {
459             if (coursemenu.searchCourse(b[2]) != null)//学生选的这门课是存在的
460             {
461                 currentCourse = coursemenu.searchCourse(b[2]);
462                 if(currentCourse.type.equals("实验")){
463                     String [] temp =currentCourse.allXingxi.split(" ");
464                     if(b.length-3 == Integer.parseInt(temp[3])){
465                         if(currentStudent.courses.contains(currentCourse)){}
466                         else{
467                             float sum = 0;
468                             float ccup;
469                             for(int i=0 ; i<b.length-3;i++)
470                             {
471                                 ccup = Float.parseFloat(temp[4+i]);
472                                 sum += ccup * Integer.parseInt(b[i+3]);
473                             }
474                             currentStudent.addCourse(currentCourse);
475                             currentStudent.setMark((int) sum);
476                             currentClass.setMark(sum);
477                             currentCourse.setMark(sum);
478                         }
479                     }
480                     else{
481                         System.out.println(b[0] + " " + b[1] + " : access mode mismatch");//模式不匹配
482                     }
483                 }
484                 else{
485                     System.out.println(b[0] + " " + b[1] + " : access mode mismatch");//模式不匹配
486                 }
487             }
488             else
489             {
490                 System.out.println(b[2] + " does not exist");
491             }
492         }
493         else if(b.length==5)
494         {
495             int normalScore = Integer.parseInt(b[3]);
496             int examScore = Integer.parseInt(b[4]);
497             if(coursemenu.searchCourse(b[2])!=null)
498             {
499                 currentCourse = coursemenu.searchCourse(b[2]);
500                 if(currentCourse.type.equals("考试"))
501                 {
502                     String [] temp =currentCourse.allXingxi.split(" ");
503                     if(currentStudent.courses.contains(currentCourse)){}
504                     else {
505                         float ccup1;
506                         float ccup2;
507                         ccup1 = Float.parseFloat(temp[3]);
508                         ccup2 = Float.parseFloat(temp[4]);
509                         currentStudent.addCourse(currentCourse);
510                         currentStudent.setMark(normalScore * ccup1, examScore * ccup2);
511                         currentClass.setMark(normalScore * ccup1, examScore * ccup2);
512                         currentCourse.setMark(normalScore*ccup1, examScore * ccup2);
513                     }
514                 }
515                 else
516                 {
517                     System.out.println(b[0] + " " + b[1] + " : access mode mismatch");//模式不匹配
518                 }
519             }
520             else//在课表上面不存在该课
521             {
522                 System.out.println(b[2] + " does not exist");//课不存在
523             }
524         }
525         else //存在而且该考察方式是考察
526         {
527             int normalScore = Integer.parseInt(b[3]);//平时分
528             if(coursemenu.searchCourse(b[2])!=null)//学生选的这门课是存在的
529             {
530                 currentCourse = coursemenu.searchCourse(b[2]);
531                 if(currentCourse.type.equals("考察"))//长度为5 所以是和考试有关的
532                 {
533                     if(currentStudent.courses.contains(currentCourse)){}
534                     else {
535                         currentStudent.addCourse(currentCourse);
536                         currentStudent.setMark(normalScore);
537                         currentClass.setMark(normalScore);
538                         currentCourse.setMark(normalScore);
539                     }
540                 }
541                 else
542                 {
543                     System.out.println(b[0] + " " + b[1] + " : access mode mismatch");//模式不匹配
544                 }
545             }
546             else//在课表上面不存在该课
547             {
548                 System.out.println(b[2] + " does not exist");//课不存在
549             }
550         }
551     }
552     void showStudent()
553     {
554         if(students.size() == 0){return;}
555         else{
556         Collections.sort(students);
557         for(Student s : students)
558         {
559             s.stuShow();
560         }}
561     }
562     void showClass()
563     {
564         if(classes.size() == 0)
565             return;
566         else {
567             Collections.sort(classes);
568             for (Class s : classes) {
569                 s.classShow();
570             }
571         }
572     }
573     void showCourse()
574     {
575         if(coursemenu.classmenu.size() == 0)
576             return;
577         else {
578             Collections.sort(coursemenu.classmenu);
579             for (Course s : coursemenu.classmenu) {
580                 s.courseShow();
581             }
582         }
583     }
584 }

要点:

该题涉及的Java知识点包括输入输出、字符串处理、数据结构和算法、异常处理等。相比上两个版本,本题修改了成绩类的继承关系为组合关系,并增加了分项成绩类来进行成绩的计算。

题量适中,涉及多个输入输出项和计算要求,同时还有对继承和组合关系的理解和比较。

难度中等,需要综合运用Java的输入输出、字符串处理、数据结构和算法、异常处理等知识点来解决问题。此外,还需要理解并比较继承和组合关系的区别,思考哪种关系更适应变更,并相应地修改代码。

通过修改类结构、采用组合关系的方式,可以使代码更加灵活,模块化程度更高。通过使用分项成绩类,可以根据权重计算各个分项的成绩,并在计算总成绩时进行累加。这样可以更容易适应需求的变更,例如增加新的分项成绩或修改权重,而无需修改大量的代码。

因此,通过使用组合关系来构建成绩类,代码更具灵活性和可维护性,能够更好地适应变更。对于这些知识点的熟练掌握和灵活运用,将有助于解决该题目,并能更好地理解和应用继承和组合关系。

类图:

 

可以进一步考虑以下方面:

 组合关系的优势:与继承关系相比,组合关系更具灵活性和可扩展性。通过将分项成绩类与课程成绩类进行组合,可以轻松地处理单门课程的多个成绩分项,并根据权重来计算总成绩。这种设计模式符合“组合优于继承”的原则,能够更好地适应需求变更和扩展。. 可配置的分项成绩权重:由于新增了分项成绩类,在录入课程信息时,需要输入每个分项成绩的权重。这样的设计允许灵活地对分项成绩进行权重分配,可以根据实际情况进置。例如,对于某门课程,可以根据教学目标和考核要求来确定各个分项成绩的比重,从而更准确地计算总成绩。总成绩计算的实现:在计算总成绩时,通过分项成绩类的权重和成绩来计算各个分项的得分,然后将得分相加得到总成绩。这种分项成绩和总成绩的计算方式更加灵活,可以适应不同课程的考核方式和评分标准。. 异常处理的完善:需要对异常情况进行处理。例如,检查输入的分项成绩数量和权重的数量是否匹配,检查分项成绩权重的总和是否为1,以及对其他格式错误和越界的输入进行处理。通过合理的异常处理机制,可以更好地保证程序的稳定性和可靠性。

总的来说,课程成绩统计程序-3在前两个版本的基础上,通过改变成绩类的继承关系为组合关系,并引入了分项成绩类来处理分项成绩和计算总成绩。通过这种设计方式,代码变得更灵活、可扩展性更强,能够更好地适应需求的变更。对于这些知识点的理解和灵活运用,将有助于解决该问题,并在实际应用中具备更好的适应性和可维护性。

7-3 jmu-Java-02基本语法-03-身份证排序

import java.util.Arrays;
import java.util.Scanner;

/**
 * 身份证号排序
 * 以前这道题做了很久,不会然后copy别人的
 * 其实今天看来这道题还是挺简单的
 * @author Sakura
 *
 */
public class Main {

    public static void main(String[] args) {
        Scanner sc=new Scanner (System.in);
        int n;
        n=sc.nextInt();
        String s[]=new String[n];             //装身份证号的
        String birth[]=new String[n];             //这个是装生日的
       
        String str;
        for(int i=0;i<n;i++) {
            s[i]=sc.next();     //并没有用nextLine()感觉那样写没有这么写简单
        }
        while(true) {
            str=sc.next();
            if(str.equals("e")) {
                System.out.println("exit");
                break;
            }else if(str.equals("sort1")) {
                for(int i=0;i<n;i++) {
                    birth[i]=s[i].substring(6,10 )+'-'+ s[i].substring(10, 12)+'-'+s[i].substring(12, 14);
                   
                }
                Arrays.sort(birth);            //生日先升序
                for(int i=0;i<n;i++) {
                    System.out.println(birth[i]);
                }
               
            }else if(str.equals("sort2")) {
                //生日先升序排一下
                for(int i=0;i<n;i++) {
                    birth[i]=s[i].substring(6,10 )+ s[i].substring(10, 12)+s[i].substring(12, 14);
                   
                }
                Arrays.sort(birth);
                for(int i=0;i<n;i++) {
                    for(int j=0;j<n;j++){
                        if(s[j].contains(birth[i])) {
                            System.out.println(s[j]);
                            break;
                        }
                    }
                }
            }
        }
    }

}

  要点:输入"sort1",程序将对生日按升序进行排序并打印;如果输入"sort2",程序将按照生日升序排序并打印身份证号码。

3.一些心得体会

主要困难:
1. 输入合法性验证:代码中对输入的合法性进行了一些验证和错误处理。然而,这部分代码仍然比较冗长,可读性较差,很多重复的判断逻辑。这可能会导致代码的维护和扩展困难。
2. 代码重复:代码中存在一些重复的判断逻辑和重复的代码块,使得代码的冗余度较高,可读性较差,也增加了代码维护的难度。

改进:
1. 提取公共方法:将输入合法性验证和错误处理的逻辑提取为独立的方法,以提高代码的可读性和可维护性。可以考虑设计一个输入处理类,将输入的合法性判断、错误处理和信息存储等功能封装在该类中,使得代码更加模块化和易于扩展。
2. 减少重复代码:通过提取公共方法、使用循环和条件语句等方式,减少代码中的重复逻辑和重复代码的存在。可以通过抽象出公共的判断和处理逻辑,将其封装成独立的方法或工具类,方便复用和维护。
3. 引入异常处理:对于输入合法性验证和错误处理,可以引入异常处理机制,使用自定义异常类来表示不合法的输入和错误情况,从而增加代码的可读性和可维护性。 

综上所述,改进的主要方向是简化代码逻辑、减少重复代码和提高代码的可读性和可维护性。通过引入更好的设计模式和编码规范,可以使代码更加简洁和易于理解。

 

4.总结

 兜兜转转终于到了激动人心的尾声阶段,这是对Java基础学习的第三次博客作业的总结,同时也是我这一学期最后一次对Java学习的总结,但这不是结束,而是新阶段的开始!

  1.经过了一个学期的Java学习,自己从最初的小萌新,逐渐成长了起来,不是“大神”只是菜鸟,Java学习还有很长一条路要走,而我只能说是拿到了一把开局的基本道具,还需要不断升级,努力,还需要再花一些工夫。。

  2.本次的阶段性学习学习了多态的基本概念及使用方法,掌握了转型和下转型的基本概念和使用方法,掌握了java中final关键字的含义以及使用方法,抽象类的基本概念和使用方法,继承,接口等基本概念和使用方法,其中ArrayList,Hashset和Hashmap使用较多比较熟练,本阶段的主要以对象为处理对象,类与类之间的联系比较紧密熟练。

以上是我这次作业的全部内容了,感谢你的观看。