最新要闻

广告

手机

蜀道装备6月27日快速回调 世界速递

蜀道装备6月27日快速回调 世界速递

今头条!群雄逐鹿,动力电池竞争“白热化”

今头条!群雄逐鹿,动力电池竞争“白热化”

家电

pta第三次总结

来源:博客园

前言

题目集7:本次题目集由于是系统的建立因此其中的功能涉及多个维度包括时间、数组、字符。其中不仅要不断的去完成各个类中功能设计,同时还要清楚各个类之间的联系与调用,其中日期类问题考察了包括Calender类和data类的使用,数组包括字符数组也包括整形数组,要求运用split的方法和while循环,在一定的程度上也同样考察了对matches的运用和正则表达式的表示规律等Java知识;其与第六次题目集中的菜单4同属于菜单3的迭代结果,但是其与菜单4方向不同的地方是加入了口味度的区分,在对应的关系上又做了一定的复杂逻辑扩展;难度较大,题量小;

题目集8:本次题目集由于是系统的建立因此其中的功能涉及多个维度包括数组、字符、排序。其中不仅要不断的去完成各个类中功能设计,同时还要清楚各个类之间的联系与调用;在课程类信息中对课程平均分和班级总平均分以及学生的个人平均分中要有一定的区分,在这其中选课类占用了很大的关键,它起了衔接学生与课程和成绩的关系;数组包括字符数组也包括整形数组,要求运用split的方法和while循环,在一定的程度上也同样考察了对matches的运用和正则表达式的表示规律等Java知识,同时运用了相关Arraylist、Arrays、Locale类的知识点去处理了相关数据,本次为课程类的第一次出题;难度较大,题量小;


(相关资料图)

题目集9:统计Java程序中关键词的出现次数,该题为布置的一道附加题,设计的最多的知识点有Map类中TreeMap的运用(运用其中部分的方法对内部数据进行value的大小排序)和Arrays类的运用,在相关检测以及排除中用到了字符串的去处功能,涉及到正则表达式的匹配以及查找删除,有Matter和Patte类的运用;本次题目集难度一般,题量小;

题目集10:该次题目集重点侧重于对HashMap的运用,考察了对HashMap中的性质以及其中包含的方法(类似于put()、get()),同时考察了对HashMap中数据的排序,用的的知识点包括Arraylist和Collection类的方法;

在改题目集中有一道课程管理系统的迭代题,该题在相关课程类型中加入了实验类,这里更加考察的是学生对正则表达式的运用以及对不同成绩处理模式的应用,在逻辑上增加了不同情况下的判别方式,但涉及知识点没有太大的变化;其次考察了对多态的理解;本次题目集难度一般,题量适中;

题目集11:该次题目集侧重知识点有Arraylist储存类的相关运用,课程成绩统计程序-3在第二次的基础上修改了计算总成绩的方式,同时完全改变了判别的方式,涉及的知识点一如既往的广,包含大大小小的逻辑判断和各类方法应用;同时包含了对字符串相关的substring(m,n)的方法运用以及foreach的输出方式,同时包含利用接口的方式去实现Stack类的功能;以及对覆盖的更深层的运用;本次题目集难度较大,题量适中;

设计与分析

题目集7 7-1 菜单5

该题目在菜单3的基础上加入了特色菜类型和口味度的识别条件:

特色菜的口味类型:川菜、晋菜、浙菜

川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣;

晋菜增加酸度值,酸度0-4级;对应酸度水平为:不酸、微酸、稍酸、酸、很酸;

浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜;

在各个菜品添加时要在类中加入对应菜品的类别和对应的口味度;在输出时也有在对应的规定下进行输出和判定

创建各个类:Main类处理输入信息,将信息全部导入各类中,处理完后将所有数据进行统计和整理,然后按照格式输出;Dish类将存储菜品各类信息;Menu将储存Dish类中的各类菜;Order类储存订单信息;Record类用来储存订单类;·Table用来记录信息桌号信息和用户信息以及对应时间对应的折扣(由于桌号是和用户和时间相对应的);

在计算平均值时要四舍五入,这里我们可以用Math.round()的方法去完成,但要注意其中数据必须为float型;

在处理日期信息将日期转化为周几时我们可以用到Calender类的方法:

但这里可能是存在概念差别要进行判别:

我的代码如下:

import java.util.Scanner;import java.util.Calendar;public class Main {     public static void main(String[] args) {            Scanner in = new Scanner(System.in);            int cntTable=0;//桌号            int[] xu=new int[20];            int q=0;            Menu mu = new Menu();            User[] u=new User[20];            int nm=0;            int flag=0;            Table[] tablemes = new Table[56];            int j = 0;//菜单数            int l = 0;//订单数            int k = 0;//代点菜数            int can=1;            Dish tt;            // sum = 0;            int count;            String[] temp;            int a1,a2,a3,a4,a5;            while (true) {                String st = in.nextLine();                temp = st.split(" ");                if(st.equals("end"))                    break;                count = temp.length;                if (count == 2) {//一个空格                    //String[] temp1 = st.split(" ");                    if (temp[1].equals("delete")) {//第二个为delete                        a1 = Integer.parseInt(temp[0]);                        if(tablemes[cntTable].odt.findRecordByNum(a1)==null) {                           System.out.println("delete error");                        }                        else {                            Record r=new Record();                            r=tablemes[cntTable].odt.findRecordByNum(a1);                                if(mu.searthDish(r.d.name).type.equals("川菜")) {                                    tablemes[cntTable].sum0-=r.num*r.grade;                                    tablemes[cntTable]. num0-=r.num;                                }                                else if(mu.searthDish(r.d.name).type.equals("晋菜")) {                                   tablemes[cntTable].sum3-=r.num*r.grade;                                  tablemes[cntTable].num1+=r.num;                                }                                else if(mu.searthDish(r.d.name).type.equals("浙菜")) {                                   tablemes[cntTable].sum4-=r.num*r.grade;                                  tablemes[cntTable].num2-=r.num;                                }                                                                if(r.d.flag==1) {                                    if(tablemes[cntTable].week>=6) {                                    int a = r.getPrice();                                    tablemes[cntTable].sum2-=a;                                    tablemes[cntTable].sum1-=r.getPrice();                                    }                                    else {                                        int a=Math.round(r.getPrice()*0.7F);                                                                                tablemes[cntTable].sum1-=r.getPrice();                                        tablemes[cntTable].sum2 -=a;                                    }                                    }                                    else {                                        int a = r.getPrice();                                                                               tablemes[cntTable].sum1-=r.getPrice();                                        tablemes[cntTable].sum2 -=a;                                    }                            }                            tablemes[cntTable].odt.delARecordByOrderNum(a1);                        }                     else {//菜单添加                        if(flag==0) {                        if(temp[1].matches("[0-9]{1,2}$")) {                         a2 = Integer.parseInt(temp[1]);                                                if(a2>300||a2<0) {                            System.out.println(temp[0]+" price out of range ");                        }                        else {                        int ex=0;                            for(int i=0;i300||a2<0) {                        System.out.println(temp[0]+" price out of range ");                    }                    else {                    int ex=0;                            for(int i=0;i3) {                                 System.out.println(a3+" portion out of range "+a4);                            }                            else if(a5>15||a5<0) {                            System.out.println(a3+" num out of range "+a5);                        }                            else {                                                       tablemes[cntTable].odt.records[l].d = tt;                                                            int a = tablemes[cntTable].odt.records[l].getPrice();                                System.out.println(tablemes[cntTable].odt.records[l].orderNum + " " + tt.name + " " +a );                                tablemes[cntTable].sum +=a;                            }                        }                        else {                            System.out.println(temp[1]+" does not exist");                        }                        l++;                                                }                    }                    }                    //continue;                }                else if(count ==5) {                    if(can==1) {                        a3 =Integer.parseInt(temp[0]);                        int a6 = Integer.parseInt(temp[2]);                        a4 = Integer.parseInt(temp[3]);                        a5=Integer.parseInt(temp[4]);                                           if(temp[4].length()==2&&temp[4].charAt(0)=="0") {                            System.out.println("wrong format");                        }                        if(mu.searthDish(temp[1])==null) {                            System.out.println(temp[1]+" does not exist");                        }                        else {                                            if(mu.searthDish(temp[1]).type.equals("川菜")&&a6<0||a6>5) {                                 System.out.println("spicy num out of range :"+a6);                        }                        else if(mu.searthDish(temp[1]).type.equals("晋菜")&&a6>4||a6<0) {                                System.out.println("acidity num out of range :"+a6);                        }                        else if(mu.searthDish(temp[1]).type.equals("浙菜")&&a6>3||a6<0) {                                System.out.println("sweetness num out of range :"+a6);                        }                                                 else{                        tablemes[cntTable].odt.addARecord(a3, temp[1],a6,a4 , a5);                        tt = mu.searthDish(temp[1]);                        if (tt != null) {                            if(a4>3) {                                 System.out.println(a3+" portion out of range "+a4);                            }                            else if(a5>15||a5<0) {                            System.out.println(a3+" num out of range "+a5);                        }                            else {                            if(mu.searthDish(temp[1]).type.equals("川菜")) {                                    tablemes[cntTable].sum0+=a6*a5;                                    tablemes[cntTable]. num0+=a5;                                }                                else if(mu.searthDish(temp[1]).type.equals("晋菜")) {                                   tablemes[cntTable].sum3+=a6*a5;                                  tablemes[cntTable].num1+=a5;                                }                                else if(mu.searthDish(temp[1]).type.equals("浙菜")) {                                   tablemes[cntTable].sum4+=a6*a5;                                  tablemes[cntTable].num2+=a5;                                }                                tablemes[cntTable].odt.records[l].d = tt;                                if(tt.flag==1) {                                    if(tablemes[cntTable].week>=6) {                                    int a = tablemes[cntTable].odt.records[l].getPrice();                                    System.out.println(tablemes[cntTable].odt.records[l].orderNum + " " + tt.name + " " +a );                                    tablemes[cntTable].sum2+=a;                                    tablemes[cntTable].sum1+=tablemes[cntTable].odt.records[l].getPrice();                                    }                                    else {                                        int a=Math.round(tablemes[cntTable].odt.records[l].getPrice()*0.7F);                                        System.out.println(tablemes[cntTable].odt.records[l].orderNum + " " + tt.name + " " + tablemes[cntTable].odt.records[l].getPrice() );                                        tablemes[cntTable].sum1+=tablemes[cntTable].odt.records[l].getPrice();                                        tablemes[cntTable].sum2 +=a;                                    }                                    }                                    else {                                        int a = tablemes[cntTable].odt.records[l].getPrice();                                        System.out.println(tablemes[cntTable].odt.records[l].orderNum + " " + tt.name + " " +a );                                        tablemes[cntTable].sum1+=tablemes[cntTable].odt.records[l].getPrice();                                        tablemes[cntTable].sum2 +=a;                                    }                            }                        }                        else {                            System.out.println(temp[1]+" does not exist");                        }                        l++;                                           }                                        }                }                }               else if (count == 7) {//三个空格                    //String[] temp2 = st.split(" ");                    if (temp[0].equals("table")) {//桌号                        can=1;                        if(temp[1].matches("[0-9]{1,2}$")) {                        cntTable=Integer.parseInt(temp[1]);                        xu[q]=cntTable;                        q++;                        tablemes[cntTable] = new Table();                        if(cntTable<1||cntTable>55) {                            System.out.println(cntTable+" table num out of range");                            can=0;                            tablemes[cntTable].f=0;                        }                        else {                        if(temp[3].length()<10&&temp[4].length()==11&&temp[4].matches("^1(80|81|89|33|35|36)([0-9]{8})$")) {                        l = 0;                                                //tablemes[cntTable].tableDtime = st;                        tablemes[cntTable].AheadProcess(st);                        if(tablemes[cntTable].f==1) {                        if(tablemes[cntTable].discnt!=-1) {                        flag=1;                        tablemes[cntTable].exit=1;                        tablemes[cntTable].username=temp[3];                        tablemes[cntTable].usernum=temp[4];                        System.out.println("table " + cntTable + ": ");                        if(nm==0) {                            u[nm]=new User();                            u[nm].name=temp[3];                            u[nm].num=temp[4];                            nm++;                        }                        else {                        int fl=0;                        for(int w=0;w15||a6<0) {                        System.out.println(a3+" num out of range "+a6);                    }                    else if(temp[5].length()==2&&temp[5].charAt(0)=="0") {                        System.out.println("wrong format");                    }                    else {                                        if(mu.searthDish(temp[2]).type.equals("川菜")&&a6<0||a6>5) {                             System.out.println("spicy num out of range :"+a6);                    }                    else if(mu.searthDish(temp[2]).type.equals("晋菜")&&a6>4||a6<0) {                            System.out.println("acidity num out of range :"+a6);                    }                    else if(mu.searthDish(temp[2]).type.equals("浙菜")&&a6>3||a6<0) {                            System.out.println("sweetness num out of range :"+a6);                    }                                else {                    tablemes[cntTable].odt.addARecord(a2, temp[2],a3,a4 , a6);                    tt = mu.searthDish(temp[2]);                    if (tt != null) {                        if(a4>3) {                             System.out.println(a3+" portion out of range "+a4);                        }                        else {                        if(mu.searthDish(temp[2]).type.equals("川菜")) {                                tablemes[a1].sum0+=a3*a6;                                tablemes[a1]. num0+=a6;                            }                            else if(mu.searthDish(temp[2]).type.equals("晋菜")) {                               tablemes[a1].sum3+=a3*a6;                              tablemes[a1].num1+=a6;                            }                            else if(mu.searthDish(temp[2]).type.equals("浙菜")) {                               tablemes[a1].sum4+=a3*a6;                              tablemes[a1].num2+=a6;                            }                            tablemes[cntTable].odt.records[l].d = tt;                            if(tt.flag==1) {                                if(tablemes[cntTable].week>=6) {                                int a = tablemes[cntTable].odt.records[l].getPrice();                                System.out.println(a2+ " table "+ cntTable+ " pay for table " + a1 + " " +a );                                tablemes[cntTable].sum2+=a;                                tablemes[cntTable].sum1+=tablemes[cntTable].odt.records[l].getPrice();                                }                                else {                                    int a=Math.round(tablemes[cntTable].odt.records[l].getPrice()*0.7F);                                    System.out.println(a2+ " table "+ cntTable+ " pay for table " + a1 + " " + tablemes[cntTable].odt.records[l].getPrice() );                                    tablemes[cntTable].sum1+=tablemes[cntTable].odt.records[l].getPrice();                                    tablemes[cntTable].sum2 +=a;                                }                                }                                else {                                    int a = tablemes[cntTable].odt.records[l].getPrice();                                    System.out.println(a2+ " table "+ cntTable+ " pay for table " + a1 + " " +a );                                    tablemes[cntTable].sum1+=tablemes[cntTable].odt.records[l].getPrice();                                    tablemes[cntTable].sum2 +=a;                                }                        }                    }                    else {                        System.out.println(temp[2]+" does not exist");                    }                    l++;                                        }                                    }                }                }                }                //st = sc.nextLine()                else {                    System.out.println("wrong format");                }            }           for(int i=0;iu[s+1].name.charAt(0)) {                       User u2=new User();                       u2=u[s];                       u[s]=u[s+1];                       u[s+1]=u2;                   }               }           }           for(int w=0;w=2022&&year<=2023) {            if(month>0||month<13) {                if(year==2022) {                    mon[2]=29;                }                if(day<=mon[month]&&day>0) {                    return true;                }                else                    return false;            }            else                return false;        }        else            return false;    }    void AheadProcess(String tableDtime){        this.tableDtime = tableDtime;        processTime();        if(validtime()==true){        if(hh<24&&hh>=0&&mm>=0&&mm<=60&&ss>=0&&ss<=60) {        discount();        }        else {            f=0;        }        }//CheckAtime();        else{            System.out.println("not a valid time period");        }    }    void processTime(){//处理时间        String[] temp = tableDtime.split(" ");        tableNum = Integer.parseInt(temp[1]);        String[] temp1 = temp[5].split("/");        String[] temp2 = temp[6].split("/");        year = Integer.parseInt(temp1[0]);        month = Integer.parseInt(temp1[1]);        day = Integer.parseInt(temp1[2]);                Calendar c = Calendar.getInstance();        c.set(year, (month-1), day);        week = c.get(Calendar.DAY_OF_WEEK);        if(week==1)            week = 7;        else            week--;        hh = Integer.parseInt(temp2[0]);        mm = Integer.parseInt(temp2[1]);        ss = Integer.parseInt(temp2[2]);    }    //void CheckAtime(){    // f= !(discnt < 0);    // }    void discount(){        if(week>5&&week<=7)        {            if(hh>=10&&hh<=20)                discnt= 1.0F;            else if(hh==9&&mm>=30)                discnt= 1.0F;            else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)                discnt= 1.0F;        }        else        {            if(hh>=17&&hh<20)                discnt=0.8F;            else if(hh==20&&mm<30)                discnt=0.8F;            else if(hh==20&&mm==30&&ss==0)                discnt=0.8F;            else if(hh==10&&mm>=30||hh>=11&&hh<=13)                discnt=0.6F;            else if(hh==14&&mm<30)                discnt=0.6F;            else if(hh==14&&mm==30&&ss==0)                discnt=0.6F;                   }    }}class Order {    Record[] records=new Record[20];//保存订单上每一道的记录    int count=0;    int getTotalPrice(){        int all=0;        for(int i=0;iView Code

多次尝试之下在时间截至之前还存在以下错误:

错误形式的存在太复杂,可能性太多了,无法准确去找出;对删除记录的非零返回可能是数据返回错误,具体错误没有找到;

代码类图如下:

相应SourceMonitor的生成报表:

发现其中主类比较复杂,估计是在结构上有些逻辑比较混乱;

题目集8 课程成绩统计程序

本题使用Java中的Scanner类来读取输入信息,然后使用字符串的分割和判断(split()、matches())方法等操作进行数据处理。

其次对输入数据进行区分,这里用的是判断分割后的字符数组的长度来区分,每个长度对应不同的数据处理,可以是加入课程信息,也可以是输入成绩信息;分别对应将课程信息储存至选课系统中和将学生储存至学生类中以及加入对应班级里,这里考虑到是否要在对应班级类中添加新的个体,要区分班级是否已经存在;对学生信息,这里要考虑学生的各项成绩,故在学生类中也要添加成绩的数组以计算相应的平均分;

然后计算每个课程的总成绩时,需要考虑课程的性质和考核方式。

对于必修课程,需要进行考试,总成绩需要按照平时成绩和期末成绩的权重进行加权求和。

对于选修课程,可以选择考试或考察的方式进行考核。如果是考试,总成绩同样需要按照平时成绩和期末成绩的权重进行加权求和;如果是考察,总成绩直接等于期末成绩。

根据班级号,将同一班级的学生成绩信息进行分组,然后计算每个班级的平均成绩。对课程的排序方法,对应的是按照课程的名称来区分,故在此要用的Comparable类进行对课程搜字符的大小判断和排序,以达到题意中的要求;

题目中对班级的输出也有具体要求,按照班级大小排序,这里可以使用冒泡排序法,也可以通过TreeMap来对应,但我认为冒泡更加亲民;

对个人平均分的输出,按照平均分大小进行输出,同样可以用冒泡来改变各个学生相关的位置,达到对应的输出效果;

对Comparable类的运用(对课程名称排序)复写compareTo的方法:

我的代码如下:

import java.util.Scanner;import java.util.ArrayList;import java.util.Comparator;import java.text.Collator;import java.util.Arrays;import java.util.Locale;import java.util.List;import java.util.Collections;public class Main {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[] temp;        int count;        int count1 = 0;        SelectCourse selectCourse = new SelectCourse();        Classroom classroom[] = new Classroom[10];        while(true) {            String st = input.nextLine();            temp = st.split(" ");            if (st.equals("end"))                break;            count = temp.length;            if(count==2||count==3) {                if (temp[0].length() <= 10&&temp[0].length()>0) {                    if(count ==3){                         if (temp[1].matches(".*[必选]修.*")&&temp[2].matches(".*考[试察].*")) {//判断是否符合形式                                if (temp[1].matches(".*必修.*") && temp[2].matches(".*考察.*")) {                                    System.out.println(temp[0] + " : course type & access mode mismatch");                                }                                else{                                    if(selectCourse.getCourse(temp[0])==null) {                                        selectCourse.addCourse(temp[0], temp[1], temp[2]);                                    }                                }                        }                        else{                            System.out.println("wrong format");                        }                    }                    else if(count==2){                        if(temp[1].matches(".*必修.*")) {                            if (selectCourse.getCourse(temp[0])==null) {                                selectCourse.addCourse(temp[0], temp[1], "考试");                            }                        }                        else if(temp[1].matches(".*选修.*")){                            System.out.println(temp[0]+" : course type & access mode mismatch");                        }                        else{                            System.out.println("wrong format");                        }                    }                }                else{                    System.out.println("wrong format");                }            } else if(count == 4||count == 5){       if (temp[3].matches("[0-9]{1,3}$")) {            int a = Integer.parseInt(temp[3]);            int f1=0;            int a1 =0;            if(count ==5){            if(temp[4].matches("[0-9]{1,3}$")){            a1 = Integer.parseInt(temp[4]);            if(a1>100||a1<0){                f1=1;             }            }                else{                    f1=1;                }             }             if(f1==0){             if (a <= 100 && a >= 0) {             if (temp[0].matches("^([0-9]{8})$")) {                 int flag = 0, g = 0;                 String str = "";                 for (int i = 0; i < 6; i++) {                      str += temp[0].charAt(i);                  }               if (count1 != 0) {                   for (int i = 0; i < count1; i++) {                       if (classroom[i].getNum().equals(str)) {                            g = i;                            flag = 1;                          }             }         }         if (flag == 0) {         classroom[count1] = new Classroom();         classroom[count1].setNum(str);         count1++;           }         if (temp[1].length() <= 10) {         if (selectCourse.getStudent(temp[1]) == null) {              selectCourse.addStudent(temp[1], temp[0]);         if(flag == 1){             classroom[g].adderStudent(selectCourse.getStudent(temp[1]));          }           else{            classroom[count1-1].adderStudent(selectCourse.getStudent(temp[1]));            }           } else if (selectCourse.getStudent(temp[1]) != null && !selectCourse.getStudent(temp[1]).getNum().equals(temp[0])) {        selectCourse.addStudent(temp[1], temp[0]);          if(flag == 1){           classroom[g].adderStudent(selectCourse.getStudent(temp[1]));           }           else{             classroom[count1-1].adderStudent(selectCourse.getStudent(temp[1]));          }       }       if (selectCourse.getCourse(temp[2]) != null && temp[2].length() <= 10) {             if (count == 4 && selectCourse.getCourse(temp[2]).getAssessment().equals("考察")) {             if (selectCourse.getStudent(temp[1]).isExist(temp[2])) {              selectCourse.addScore(-1, a);               selectCourse.getStudent(temp[1]).adderCourse(selectCourse.getCourse(temp[2]), selectCourse.getScore(selectCourse.e - 1));             }          } else if (count == 5 && selectCourse.getCourse(temp[2]).getAssessment().equals("考试")) {                 if (selectCourse.getStudent(temp[1]).isExist(temp[2])) {                     selectCourse.addScore(a, a1);                     selectCourse.getStudent(temp[1]).adderCourse(selectCourse.getCourse(temp[2]), selectCourse.getScore(selectCourse.e - 1));                  }          } else {                System.out.println(temp[0] + " " + temp[1] + " : access mode mismatch");              }           } else {                  System.out.println(temp[2] + " does not exist");            }           }           else{           System.out.println("wrong format");           }        }else {             System.out.println("wrong format");             }          } else {                System.out.println("wrong format");           }          }     else{              System.out.println("wrong format");         }                    }                else{                    System.out.println("wrong format");                }            }            else{                System.out.println("wrong format");            }        }        selectCourse.listStudents();        for(int i = 0;i< selectCourse.s;i++){            if(selectCourse.getStudent(i).c!=0) {                int sum = 0;                for (int j = 0; j < selectCourse.getStudent(i).c; j++) {                    sum+=selectCourse.getStudent(i).getScore(j).getTotalScore();                }                int ave = (int)(sum/selectCourse.getStudent(i).c);                System.out.println(selectCourse.getStudent(i).getNum() + " " + selectCourse.getStudent(i).getName() + " " + ave);            }            else{                System.out.println(selectCourse.getStudent(i).getNum()+" "+selectCourse.getStudent(i).getName()+" did not take any exams");            }        }        for(int i = 0;i < selectCourse.k;i++) {            if (selectCourse.getCourse(i) != null) {                int sum = 0, ave = 0;                int sum1 = 0, ave1 = 0;                int sum2 = 0, ave2 = 0;                int n = 0;                for (int j = 0; j < selectCourse.s; j++) {                    for (int k = 0; k < selectCourse.getStudent(j).c; k++) {                        if (selectCourse.getStudent(j).getCourse(k).getName().equals(selectCourse.getCourse(i).getName())) {                            sum += selectCourse.getStudent(j).getScore(k).getTotalScore();                            sum1 += selectCourse.getStudent(j).getScore(k).getGradualScore();                            sum2 += selectCourse.getStudent(j).getScore(k).getEndTestScore();                            n++;                        }                    }                }                if (n != 0) {                    ave =(int)((float)(sum)/(float)(n));                    ave1 =(int)((float)(sum1)/(float)(n));                    ave2 =(int)((float)(sum2)/(float)(n));                    selectCourse.getCourse(i).setAve(ave);                    selectCourse.getCourse(i).setAve1(ave1);                    selectCourse.getCourse(i).setAve2(ave2);                }            }        }        Course course[]=selectCourse.listCoruse();        for (int i = 0;i< selectCourse.k;i++) {            if (course[i].getAve() != -1) {                if (course[i].getAve1() != -1) {                    System.out.println(course[i].getName() + " " + course[i].getAve1() + " " + course[i].getAve2() + " " + course[i].getAve());                } else {                    System.out.println(course[i].getName() + " " + course[i].getAve2() + " " + course[i].getAve());                }            }            else {                System.out.println(course[i].getName()+" has no grades yet");            }        }         for(int i = 0;ib){                    Classroom c = null;                    c = classroom[i];                    classroom[i]=classroom[j];                    classroom[j]=c;                }            }        }        for (int i =0;i courses = new ArrayList<>();    ArrayListscores=new ArrayList<>();    int c = 0;    int e = 0;    public Student(){    }    public String getName() {        return name;    }    public String getNum() {        return num;    }    public void setName(String name) {        this.name = name;    }    public void setNum(String num) {        this.num = num;    }    public void adderCourse(Course course,int num) {        courses.add(course);        courses.get(c).score.setEndTestScore(num);        c++;    }    public void adderCourse(Course course,Score score){        courses.add(course);        scores.add(score);        c++;    }    public Course getCourse(int i){        return courses.get(i);    }    public Score getScore(int i){        return scores.get(i);    }    public boolean isExist(String name){        int flag = 0;        for(int i =0 ;i students = new ArrayList<>();    private int ave = -1;    public int getAve() {        return ave;    }    public void setAve(int ave) {        this.ave = ave;    }    public void adderStudent(Student s){        students.add(s);    }    public Student getStudent(int i){        return students.get(i);    }    public void setNum(String num) {            this.num = num;    }    public String getNum() {        return num;    }}class SelectCourse {    private Student student;    private Course[] courses = new Course[10];    private Student[] students = new Student[10];    private Score[] scores = new Score[10];    int k=0;    int s=0;    int e = 0;    public Course getCourse(int i){        return courses[i];    }    public Student getStudent(int i){        return students[i];    }    public Course getCourse(String name) {        Course c = null;        for(int i = 0;ia1){                    Student s =null;                    s=students[i];                    students[i]=students[j];                    students[j]=s;                }            }        }    }    public void addCourse(String name,String nature,String assessment) {        courses[k]= new Course();        courses[k].setName(name);        courses[k].setNature(nature);        courses[k].setAssessment(assessment);        k++;    }    public Score getScore(int i) {        return scores[i];    }    public void addScore(int num,int num1) {        this.scores[e] =new Score();        scores[e].setGradualScore(num);        scores[e].setEndTestScore(num1);        e++;    }    public Student getStudent() {        return student;    }    public void setStudent(Student student) {        this.student = student;    }   public Course[] listCoruse(){        Course courses1[] =new Course[k];        for(int i =0;ilist=Arrays.asList(courses1);        Collections.sort(list);        return courses1;    }}class Score {   private int endTestScore = -1;//期末成绩    private int gradualScore = -1;//平时成绩    private int totalScore;//总成绩    public void setEndTestScore(int endTestScore) {        this.endTestScore = endTestScore;    }    public void setGradualScore(int gradualScore) {        this.gradualScore = gradualScore;    }    public int getEndTestScore() {        return endTestScore;    }    public int getGradualScore() {        return gradualScore;    }    public int getTotalScore(){        int total;        if(getGradualScore()==-1){            total =getEndTestScore();        }        else {            total = (int)((float)(getGradualScore())*0.3+(float)(getEndTestScore())*0.7);        }        return total;    }}class Course implements Comparable{    private String name;//课程名称    private String nature;//课程种类    private String assessment = "考试";//考核方式    Score score =new Score();    private int totalScore;//总成绩    private int ave =-1;    private int ave1;    private int ave2;    public int getAve1() {        return ave1;    }    public int getAve2() {        return ave2;    }    public void setAve1(int ave1) {        this.ave1 = ave1;    }    public void setAve2(int ave2) {        this.ave2 = ave2;    }    public int getAve(){        return ave;    }    public void setAve(int ave){        this.ave =ave;    }    public Course(){    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAssessment() {        return assessment;    }    public String getNature() {        return nature;    }    public void setAssessment(String assessment) {        this.assessment = assessment;    }    public void setNature(String nature) {        this.nature = nature;    }    public Course(String name,String nature,String assessment){        this.name = name;        this.nature = nature;        this.assessment = assessment;    }    public int compareTo(Course course) {        Comparatorcom = Collator.getInstance(Locale.CHINA);        return ((Collator)com).compare(this.name,course.getName());    }}View Code

该题在对应的错误出现的输出上顺序是有优先级的,这样的状况在题目中有部分提示,但是也有一些是隐藏的可能无法准确的找出,找出答案错误的情况;在无数次尝试后我解决了25,26,相关的额错误类信息问题;

最后一个测试点不知道什么问题,在多次尝试下还是无法找出,因此放弃了;

代码类图如下:

主类圈复杂度如下:

由于主类的逻辑比较复杂,故对应复杂度偏高;

题目集9统计Java程序中关键词的出现次数

可以使用Scanner类从控制台逐行读取输入的Java源码,直到遇到"exit"时结束读取。用字符数组表示所有相关的java关键字,然后输入相关代码,在对内部关键字进行查找之前要对代码进行处理,这里采用的是用正则表达式和if,else的判别方法来区分是否为可用数据的,注意这里是要有判别顺序的,首先排除/* */中的代码,然后将“ ”中的代码去除,然后将注释//后的代码去掉,这里有一个点是要将=替换为“1”;这是一种会出错的情况;对多余的数据去除后。我们将可以的代码添加到一个新的字符串中,然后对相关判别,首先对是否含有数据进行判别,有后利用

Pattern pattern = Pattern.compile("\\b" + str + "\\b");//创建关键词的正则表达式 Matcher matcher = pattern.matcher(b);//字符串与关键词匹配

Pattern类:用来创建正则表达式的模式,从而对文本进行匹配。

Matcher类:根据Pattern所生成的模式对文本进行匹配,并提供对匹配结果的访问。

\b:单词边界匹配符。

str:关键词单词,为了防止误匹配需在前后加上单词边界匹配符。

b:Java源码字符串。

matcher:用于字符串与关键词的匹配,并返回相关的结果。

用matcher.find()相关方法来匹配符合的关键字,将其就加入添加至对应的关键字个数中没发现一个符合条件的就加一,最后将相关数据加入TreeMap中;最后判别是否存在关键字,存在即输出;不存在则 wrong foramt;

我的代码如下:

代码如下

题目集10容器-HashMap-排序

本题涉及HashMap的运用,首先使用Scanner类从控制台读取输入的成绩信息。以"end"作为结束标志,逐行输入学生的信息,每行包括学号、姓名和成绩三个部分。其次使用HashMap数据结构来存储学生信息。以学号作为键,将学生姓名和成绩作为值,存储到HashMap中。然后按照学号从大到小的顺序遍历HashMap中的数据,按照要求格式输出学生信息。可以使用Entry接口和Collections类的sort()方法来实现。其次注意点是:

处理输入不合法、空或者没有学生信息的情况。同时需要注意开头和结尾输入为空行或空格的情况。

我的代码如下:

代码如下
import java.util.Scanner;import java.util.HashMap;import java.util.ArrayList;import java.util.Collections;public class Main {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        HashMap hashMap = new HashMap<>();        while (true) {            String num = in.nextLine();            if (num.equals("end")) {                break;            }            String stu[] = num.split(" ");            int choice = stu.length;            if (choice == 3) {                int score = Integer.parseInt(stu[2]);                Student student = new Student(stu[1], score);                hashMap.put(Integer.parseInt(stu[0]),student);            }        }        ArrayList> list = new ArrayList<>(hashMap.entrySet());        Collections.sort(list, (o1, o2) -> (o2.getKey() - o1.getKey()));        for (HashMap.Entry entry : list) {            System.out.println(entry.getKey() + " " + entry.getValue().getName() + " " + entry.getValue().getScore());        }    }}class Student {    private String name;    private int score;    public Student(String name, int score) {        this.name = name;        this.score = score;    }    public String getName() {        return name;    }    public int getScore() {        return score;    }}

题目集107-3 课程成绩统计程序-2

本题为课程成绩统计程序-1的迭代,该题在原有的思路中增加了实验课的信息,在这里就存在相关的思路的变化,由于实验课程中信息的格式特殊性,以及成绩信息中的格式特殊性,我们要增加长度的情况,由于实验类课程成绩信息格式为:学号 姓名 课程名 实验次数 实验分数1 。。。故要根据实验次数变化调整,当满足count ==4+a时格式成立由于实验总成绩取平均分,故可以在计算好对应平均分后再加入到学生成绩和课程成绩中去;以该方向切入可以发现思路与1中必修选修处理模式相差不大;在计算平均分时要注意精度的问题,以防出现非零返回:这里要考虑各项错误输出顺序,同样要整理好以防止出现对应的输出错误;

例如该测试点的答案错误,无法找出其中存在的问题;

我的代码如下:

import java.util.Scanner;import java.util.ArrayList;import java.util.Comparator;import java.text.Collator;import java.util.Arrays;import java.util.Locale;import java.util.List;import java.util.Collections;public class Main {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[] temp;        int count;        int count1 = 0;        SelectCourse selectCourse = new SelectCourse();        Classroom classroom[] = new Classroom[10];        while(true) {            String st = input.nextLine();            temp = st.split(" ");            if (st.equals("end"))                break;            count = temp.length;            if(count==2||count==3) {                if (temp[0].length() <= 10&&temp[0].length()>0) {                    if(count ==3){                        if (temp[1].matches(".*[必选]修.*")) {//判断是否符合形式                            if(temp[2].matches("实验")){                                System.out.println(temp[0] + " : course type & access mode mismatch");                            }                            else if(temp[2].matches(".*考[试察].*")) {                                if (temp[1].matches(".*必修.*") && temp[2].matches(".*考察.*")) {                                    System.out.println(temp[0] + " : course type & access mode mismatch");                                } else {                                    if (selectCourse.getCourse(temp[0]) == null) {                                        selectCourse.addCourse(temp[0], temp[1], temp[2]);                                    }                                }                            }                        }                        else if(temp[1].matches(".*实验.*")){                            if(temp[2].matches(".*考[试察].*")){                                System.out.println(temp[0] + " : course type & access mode mismatch");                            }                            else if(temp[2].matches(".*实验.*")) {                                if (selectCourse.getCourse(temp[0]) == null) {                                    selectCourse.addCourse(temp[0], temp[1], temp[2]);                                }                            }                        }                        else{                            System.out.println("wrong format");                        }                    }                    else if(count==2){                        if(temp[1].matches(".*必修.*")) {                            if (selectCourse.getCourse(temp[0])==null) {                                selectCourse.addCourse(temp[0], temp[1], "考试");                            }                        }                        else if(temp[1].matches(".*选修.*")){                            System.out.println(temp[0]+" : course type & access mode mismatch");                        }                        else{                            System.out.println("wrong format");                        }                    }                }                else{                    System.out.println("wrong format");                }            }            else if(count == 4||count == 5){                if (temp[3].matches("[0-9]{1,3}$")) {                    int a = Integer.parseInt(temp[3]);                    int f1=0;                    int a1 =0;                    if(count ==5){                        if(temp[4].matches("[0-9]{1,3}$")){                            a1 = Integer.parseInt(temp[4]);                            if(a1>100||a1<0){                                f1=1;                            }                        }                        else{                            f1=1;                        }                    }                    if(f1==0){                        if (a <= 100 && a >= 0) {                            if (temp[0].matches("^([0-9]{8})$")) {                                int flag = 0, g = 0;                                String str = "";                                for (int i = 0; i < 6; i++) {                                    str += temp[0].charAt(i);                                }                                if (count1 != 0) {                                    for (int i = 0; i < count1; i++) {                                        if (classroom[i].getNum().equals(str)) {                                            g = i;                                            flag = 1;                                        }                                    }                                }                                if (flag == 0) {                                    classroom[count1] = new Classroom();                                    classroom[count1].setNum(str);                                    count1++;                                }                                if (temp[1].length() <= 10) {                                    if (selectCourse.getStudent(temp[1]) == null) {                                        selectCourse.addStudent(temp[1], temp[0]);                                        if(flag == 1){                                            classroom[g].adderStudent(selectCourse.getStudent(temp[1]));                                        }                                        else{                                            classroom[count1-1].adderStudent(selectCourse.getStudent(temp[1]));                                        }                                    } else if (selectCourse.getStudent(temp[1]) != null && !selectCourse.getStudent(temp[1]).getNum().equals(temp[0])) {                                        selectCourse.addStudent(temp[1], temp[0]);                                        if(flag == 1){                                            classroom[g].adderStudent(selectCourse.getStudent(temp[1]));                                        }                                        else{                                            classroom[count1-1].adderStudent(selectCourse.getStudent(temp[1]));                                        }                                    }                                    if (selectCourse.getCourse(temp[2]) != null && temp[2].length() <= 10) {                                        if (count == 4 && selectCourse.getCourse(temp[2]).getAssessment().equals("考察")) {                                            if (selectCourse.getStudent(temp[1]).isExist(temp[2])) {                                                selectCourse.addScore(-1, a);                                                selectCourse.getStudent(temp[1]).adderCourse(selectCourse.getCourse(temp[2]), selectCourse.getScore(selectCourse.e - 1));                                            }                                        } else if (count == 5 && selectCourse.getCourse(temp[2]).getAssessment().equals("考试")) {                                            if (selectCourse.getStudent(temp[1]).isExist(temp[2])) {                                                selectCourse.addScore(a, a1);                                                selectCourse.getStudent(temp[1]).adderCourse(selectCourse.getCourse(temp[2]), selectCourse.getScore(selectCourse.e - 1));                                            }                                        } else {                                            System.out.println(temp[0] + " " + temp[1] + " : access mode mismatch");                                        }                                    } else {                                        System.out.println(temp[2] + " does not exist");                                    }                                }                                else{                                    System.out.println("wrong format");                                }                            }else {                                System.out.println("wrong format");                            }                        } else {                            System.out.println("wrong format");                        }                    }                    else{                        System.out.println("wrong format");                    }                }                else{                    System.out.println("wrong format");                }            }            else if(count>=4){                if (temp[3].matches("[4-9]{1}$")) {                    int a = Integer.parseInt(temp[3]);                    int f1=0;                    int a1 =0,sum =0;                    if(count ==4+a){                        for(int i = 4;i<4+a;i++){                            int score =Integer.parseInt(temp[i]);                            if(score>100||score<0){                                f1=1;                            }                            sum+=score;                        }                        if(f1==0){                            a1 = (int)((float)(sum)/(float)(a));                        }                    }                    if(f1==0){                            if (temp[0].matches("^([0-9]{8})$")) {                                int flag = 0, g = 0;                                String str = "";                                for (int i = 0; i < 6; i++) {                                    str += temp[0].charAt(i);                                }                                if (count1 != 0) {                                    for (int i = 0; i < count1; i++) {                                        if (classroom[i].getNum().equals(str)) {                                            g = i;                                            flag = 1;                                        }                                    }                                }                                if (flag == 0) {                                    classroom[count1] = new Classroom();                                    classroom[count1].setNum(str);                                    count1++;                                }                                if (temp[1].length() <= 10) {                                    if (selectCourse.getStudent(temp[1]) == null) {                                        selectCourse.addStudent(temp[1], temp[0]);                                        if(flag == 1){                                            classroom[g].adderStudent(selectCourse.getStudent(temp[1]));                                        }                                        else{                                            classroom[count1-1].adderStudent(selectCourse.getStudent(temp[1]));                                        }                                    } else if (selectCourse.getStudent(temp[1]) != null && !selectCourse.getStudent(temp[1]).getNum().equals(temp[0])) {                                        selectCourse.addStudent(temp[1], temp[0]);                                        if(flag == 1){                                            classroom[g].adderStudent(selectCourse.getStudent(temp[1]));                                        }                                        else{                                            classroom[count1-1].adderStudent(selectCourse.getStudent(temp[1]));                                        }                                    }                                    if (selectCourse.getCourse(temp[2]) != null && temp[2].length() <= 10) {                                        if (count == 4+a && selectCourse.getCourse(temp[2]).getAssessment().equals("实验")) {                                            if (selectCourse.getStudent(temp[1]).isExist(temp[2])) {                                                selectCourse.addScore(-1, a1);                                                selectCourse.getStudent(temp[1]).adderCourse(selectCourse.getCourse(temp[2]), selectCourse.getScore(selectCourse.e - 1));                                            }                                        } else {                                            System.out.println(temp[0] + " " + temp[1] + " : access mode mismatch");                                        }                                    } else {                                        System.out.println(temp[2] + " does not exist");                                    }                                }                                else{                                    System.out.println("wrong format");                                }                            }else {                                System.out.println("wrong format");                            }                    }                    else{                        System.out.println("wrong format");                    }                }                else{                    System.out.println("wrong format");                }            }            else{                System.out.println("wrong format");            }        }        selectCourse.listStudents();        for(int i = 0;i< selectCourse.s;i++){            if(selectCourse.getStudent(i).c!=0) {                int sum = 0;                for (int j = 0; j < selectCourse.getStudent(i).c; j++) {                    sum+=selectCourse.getStudent(i).getScore(j).getTotalScore();                }                int ave = (int)(sum/selectCourse.getStudent(i).c);                System.out.println(selectCourse.getStudent(i).getNum() + " " + selectCourse.getStudent(i).getName() + " " + ave);            }            else{                System.out.println(selectCourse.getStudent(i).getNum()+" "+selectCourse.getStudent(i).getName()+" did not take any exams");            }        }        for(int i = 0;i < selectCourse.k;i++) {            if (selectCourse.getCourse(i) != null) {                int sum = 0, ave = 0;                int sum1 = 0, ave1 = 0;                int sum2 = 0, ave2 = 0;                int n = 0;                for (int j = 0; j < selectCourse.s; j++) {                    for (int k = 0; k < selectCourse.getStudent(j).c; k++) {                        if (selectCourse.getStudent(j).getCourse(k).getName().equals(selectCourse.getCourse(i).getName())) {                            sum += selectCourse.getStudent(j).getScore(k).getTotalScore();                            sum1 += selectCourse.getStudent(j).getScore(k).getGradualScore();                            sum2 += selectCourse.getStudent(j).getScore(k).getEndTestScore();                            n++;                        }                    }                }                if (n != 0) {                    ave =(int)((float)(sum)/(float)(n));                    ave1 =(int)((float)(sum1)/(float)(n));                    ave2 =(int)((float)(sum2)/(float)(n));                    selectCourse.getCourse(i).setAve(ave);                    selectCourse.getCourse(i).setAve1(ave1);                    selectCourse.getCourse(i).setAve2(ave2);                }            }        }        Course course[]=selectCourse.listCoruse();        for (int i = 0;i< selectCourse.k;i++) {            if (course[i].getAve() != -1) {                if(course[i].getAssessment().equals("实验")){                    System.out.println(course[i].getName()+ " " + course[i].getAve());                }                else {                    if (course[i].getAve1() != -1) {                        System.out.println(course[i].getName() + " " + course[i].getAve1() + " " + course[i].getAve2() + " " + course[i].getAve());                    } else {                        System.out.println(course[i].getName() + " " + course[i].getAve2() + " " + course[i].getAve());                    }                }            }            else {                System.out.println(course[i].getName()+" has no grades yet");            }        }        for(int i = 0;ib){                    Classroom c = null;                    c = classroom[i];                    classroom[i]=classroom[j];                    classroom[j]=c;                }            }        }        for (int i =0;i courses = new ArrayList<>();    ArrayListscores=new ArrayList<>();    int c = 0;    int e = 0;    public Student(){    }    public String getName() {        return name;    }    public String getNum() {        return num;    }    public void setName(String name) {        this.name = name;    }    public void setNum(String num) {        this.num = num;    }    public void adderCourse(Course course,int num) {        courses.add(course);        courses.get(c).score.setEndTestScore(num);        c++;    }    public void adderCourse(Course course,Score score){        courses.add(course);        scores.add(score);        c++;    }    public Course getCourse(int i){        return courses.get(i);    }    public Score getScore(int i){        return scores.get(i);    }    public boolean isExist(String name){        int flag = 0;        for(int i =0 ;i students = new ArrayList<>();    private int ave = -1;    public int getAve() {        return ave;    }    public void setAve(int ave) {        this.ave = ave;    }    public void adderStudent(Student s){        students.add(s);    }    public Student getStudent(int i){        return students.get(i);    }    public void setNum(String num) {        this.num = num;    }    public String getNum() {        return num;    }}class SelectCourse {    private Student student;    private Course[] courses = new Course[20];    private Student[] students = new Student[20];    private Score[] scores = new Score[20];    int k=0;    int s=0;    int e = 0;    public Course getCourse(int i){        return courses[i];    }    public Student getStudent(int i){        return students[i];    }    public Course getCourse(String name) {        Course c = null;        for(int i = 0;ia1){                    Student s =null;                    s=students[i];                    students[i]=students[j];                    students[j]=s;                }            }        }    }    public void addCourse(String name,String nature,String assessment) {        courses[k]= new Course();        courses[k].setName(name);        courses[k].setNature(nature);        courses[k].setAssessment(assessment);        k++;    }    public Score getScore(int i) {        return scores[i];    }    public void addScore(int num,int num1) {        this.scores[e] =new Score();        scores[e].setGradualScore(num);        scores[e].setEndTestScore(num1);        e++;    }    public Student getStudent() {        return student;    }    public void setStudent(Student student) {        this.student = student;    }    public Course[] listCoruse(){        Course courses1[] =new Course[k];        for(int i =0;ilist=Arrays.asList(courses1);        Collections.sort(list);        return courses1;    }}class Score {    private int endTestScore = -1;//期末成绩    private int gradualScore = -1;//平时成绩    private int totalScore;//总成绩    public void setEndTestScore(int endTestScore) {        this.endTestScore = endTestScore;    }    public void setGradualScore(int gradualScore) {        this.gradualScore = gradualScore;    }    public int getEndTestScore() {        return endTestScore;    }    public int getGradualScore() {        return gradualScore;    }    public int getTotalScore(){        int total;        if(getGradualScore()==-1){            total =getEndTestScore();        }        else {            total = (int)((float)(getGradualScore())*0.3+(float)(getEndTestScore())*0.7);        }        return total;    }}class Course implements Comparable{    private String name;//课程名称    private String nature;//课程种类    private String assessment = "考试";//考核方式    Score score =new Score();    private int totalScore;//总成绩    private int ave =-1;    private int ave1;    private int ave2;    public int getAve1() {        return ave1;    }    public int getAve2() {        return ave2;    }    public void setAve1(int ave1) {        this.ave1 = ave1;    }    public void setAve2(int ave2) {        this.ave2 = ave2;    }    public int getAve(){        return ave;    }    public void setAve(int ave){        this.ave =ave;    }    public Course(){    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAssessment() {        return assessment;    }    public String getNature() {        return nature;    }    public void setAssessment(String assessment) {        this.assessment = assessment;    }    public void setNature(String nature) {        this.nature = nature;    }    public Course(String name,String nature,String assessment){        this.name = name;        this.nature = nature;        this.assessment = assessment;    }    public int compareTo(Course course) {        Comparatorcom = Collator.getInstance(Locale.CHINA);        return ((Collator)com).compare(this.name,course.getName());    }}View Code

代码类图如下:

圈复杂度如下:

结构比较复杂;

题目集11课程成绩统计程序-3

本次题目为2的迭代,要求设计一个综合性的Java程序来记录学生的课程成绩信息,并且统计课程的总成绩和不同分项成绩的加权,最终计算出学生、班级、课程的平均成绩。为了实现该功能,我们需要逐步分析所有输入和输出的要求,并且设计程序来处理输入数据、处理计算、排序以及输出结果等。本题目要求使用组合而非继承的方式设计成绩类,这意味着课程成绩由课程成绩类和分项成绩类两部分组成,而课程成绩类具备组合分项成绩类的能力。此外,需要注意输入格式以及数据清洗过程,比如成绩格式的正确性、权值之和是否为1等问题。可以将学生信息、分项成绩信息、课程成绩信息、课程信息、班级信息等数据进行建模。此时可以采用组合关系,用课程成绩类组合分项成绩类,而不是利用继承方式。我的代码如下:
import java.util.Scanner;import java.util.ArrayList;import java.util.Comparator;import java.text.Collator;import java.util.Arrays;import java.util.Locale;import java.util.List;import java.util.Collections;public class Main {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[] temp;        int count;        int count1 = 0;        SelectCourse selectCourse = new SelectCourse();        Classroom classroom[] = new Classroom[10];        while(true) {            String st = input.nextLine();            temp = st.split(" ");            if (st.equals("end"))                break;    count = temp.length;    if(count == 4||count == 5) {    if (temp[0].matches("[0-9]{8}")) {     if (temp[3].matches("[0-9]{1,3}$")) {      int a = Integer.parseInt(temp[3]);     int f1 = 0;        int a1 = 0;        if (count == 5) {         if (temp[4].matches("[0-9]{1,3}$")) {          a1 = Integer.parseInt(temp[4]);        if (a1 > 100 || a1 < 0) {           f1 = 1;        }        } else {           f1 = 1;       }     }      if (f1 == 0) {        if (a <= 100 && a >= 0) {         if (temp[0].matches("^([0-9]{8})$")) {          int flag = 0, g = 0;          String str = "";        for (int i = 0; i < 6; i++) {               str += temp[0].charAt(i);              }         if (count1 != 0) {             for (int i = 0; i < count1; i++) {               if (classroom[i].getNum().equals(str)) {                   g = i;                   flag = 1;               }               }         }          if (flag == 0) {               classroom[count1] = new Classroom();               classroom[count1].setNum(str);               count1++;               }        if (temp[1].length() <= 10) {            if (selectCourse.getStudent(temp[1]) == null) {              selectCourse.addStudent(temp[1], temp[0]);              if (flag == 1) {             classroom[g].adderStudent(selectCourse.getStudent(temp[1]));              } else {                 classroom[count1 - 1].adderStudent(selectCourse.getStudent(temp[1]));              }             } else if (selectCourse.getStudent(temp[1]) != null && !selectCourse.getStudent(temp[1]).getNum().equals(temp[0])) {              selectCourse.addStudent(temp[1], temp[0]);            if (flag == 1) {             classroom[g].adderStudent(selectCourse.getStudent(temp[1]));               } else {                        classroom[count1 - 1].adderStudent(selectCourse.getStudent(temp[1]));                     }               }             if (selectCourse.getCourse(temp[2]) != null && temp[2].length() <= 10) {              if (count == 4 && selectCourse.getCourse(temp[2]).getAssessment().equals("考察")) {                  if (selectCourse.getStudent(temp[1]).isExist(temp[2])) {                     selectCourse.addScore(-1, a);                    selectCourse.getStudent(temp[1]).adderCourse(selectCourse.getCourse(temp[2]), selectCourse.getScore(selectCourse.e - 1));                      }                  } else if (count == 5 && selectCourse.getCourse(temp[2]).getAssessment().equals("考试")) {                  if (selectCourse.getStudent(temp[1]).isExist(temp[2])) {                 selectCourse.addScore(a, a1);                 selectCourse.getStudent(temp[1]).adderCourse(selectCourse.getCourse(temp[2]), selectCourse.getScore(selectCourse.e - 1));                  }                  } else {                          System.out.println(temp[0] + " " + temp[1] + " : access mode mismatch");                  }                   } else {                     System.out.println(temp[2] + " does not exist");                  }                                    } else {                                        System.out.println("wrong format");                                    }                                } else {                                    System.out.println("wrong format");                                }                            } else {                                System.out.println("wrong format");                            }                        } else {                            System.out.println("wrong format");                        }                    } else {                        System.out.println("wrong format");                    }                } else {                    if (temp[0].length() <= 10 && temp[0].length() > 0) {                        if (count == 5) {                            if (temp[2].matches("[0-9]{1}")) {                                if (temp[1].matches(".*必修.*")) {                                    if (selectCourse.getCourse(temp[0]) == null) {                                        int n = Integer.parseInt(temp[2]);                                        if(Double.parseDouble(temp[3])+ Double.parseDouble(temp[4])==1){                                        double[] q = {Double.parseDouble(temp[3]), Double.parseDouble(temp[4])};                                        selectCourse.addCourse(temp[0], temp[1], "考试", n, q);                                        }                                        else{                                             System.out.println(temp[0]+" : weight value error");                                        }                                    }                                } else if (temp[1].matches(".*选修.*")) {                                    System.out.println(temp[0] + " : course type & access mode mismatch");                                } else {                                    System.out.println("wrong format");                                }                            } else {                                if (temp[0].length() <= 10 && temp[0].length() > 0) {                                    if (temp[1].matches(".*[必选]修.*") && temp[2].matches(".*考[试察].*")) {//判断是否符合形式                                        if (temp[1].matches(".*必修.*") && temp[2].matches(".*考察.*")) {                                            System.out.println(temp[0] + " : course type & access mode mismatch");                                        } else {                                            if (selectCourse.getCourse(temp[0]) == null) {                                                int n = Integer.parseInt(temp[3]);                                                if(Double.parseDouble(temp[4])==1){                                                double[] q = {Double.parseDouble(temp[4])};                                                selectCourse.addCourse(temp[0], temp[1], temp[2], n, q);                                                }                                                else{                                                    System.out.println(temp[0]+" : weight value error");                                                }                                            }                                        }                                    } else {                                        System.out.println("wrong format");                                    }                                }                            }                        } else {                            System.out.println("wrong format");                        }                    }                }            }            else if(count>=3) {                if (!temp[0].matches("[0-9]{8}")) {                    if(temp[0].length()<=10){                       if(temp[1].equals("实验")&&temp[2].equals("实验")){                           if(temp[3].matches("[4-9]{1}")){                            if (selectCourse.getCourse(temp[0]) == null) {                               int o = Integer.parseInt(temp[3]);                               if(count==4+o){                                   double sum = 0;                                   for(int i = 0;i 100 || score < 0) {                                    f1 = 1;                                }                                sum += score * selectCourse.getCourse(temp[2]).getQ()[i - 3];                            }                            if (f1 == 0) {                                a1 = (int)(sum);                            }                        }                        if (f1 == 0) {                            if (temp[0].matches("^([0-9]{8})$")) {                                int flag = 0, g = 0;                                String str = "";                                for (int i = 0; i < 6; i++) {                                    str += temp[0].charAt(i);                                }                                if (count1 != 0) {                                    for (int i = 0; i < count1; i++) {                                        if (classroom[i].getNum().equals(str)) {                                            g = i;                                            flag = 1;                                        }                                    }                                }                                if (flag == 0) {                                    classroom[count1] = new Classroom();                                    classroom[count1].setNum(str);                                    count1++;                                }                                if (temp[1].length() <= 10) {                                    if (selectCourse.getStudent(temp[1]) == null) {                                        selectCourse.addStudent(temp[1], temp[0]);                                        if (flag == 1) {                                            classroom[g].adderStudent(selectCourse.getStudent(temp[1]));                                        } else {                                            classroom[count1 - 1].adderStudent(selectCourse.getStudent(temp[1]));                                        }                                    } else if (selectCourse.getStudent(temp[1]) != null && !selectCourse.getStudent(temp[1]).getNum().equals(temp[0])) {                                        selectCourse.addStudent(temp[1], temp[0]);                                        if (flag == 1) {                                            classroom[g].adderStudent(selectCourse.getStudent(temp[1]));                                        } else {                                            classroom[count1 - 1].adderStudent(selectCourse.getStudent(temp[1]));                                        }                                    }                                    if (selectCourse.getCourse(temp[2]) != null && temp[2].length() <= 10) {                                        if (count == 3 + a && selectCourse.getCourse(temp[2]).getAssessment().equals("实验")) {                                            if (selectCourse.getStudent(temp[1]).isExist(temp[2])) {                                                selectCourse.addScore(-1, a1);                                                selectCourse.getStudent(temp[1]).adderCourse(selectCourse.getCourse(temp[2]), selectCourse.getScore(selectCourse.e - 1));                                            }                                        } else {                                            System.out.println(temp[0] + " " + temp[1] + " : access mode mismatch");                                        }                                    } else {                                        System.out.println(temp[2] + " does not exist");                                    }                                } else {                                    System.out.println("wrong format");                                }                            } else {                                System.out.println("wrong format");                            }                        } else {                            System.out.println("wrong format");                        }                    }                }            }            else{                System.out.println("wrong format");            }        }        selectCourse.listStudents();        for(int i = 0;i< selectCourse.s;i++){            if(selectCourse.getStudent(i).c!=0) {                int sum = 0;                for (int j = 0; j < selectCourse.getStudent(i).c; j++) {                    sum+=selectCourse.getStudent(i).getScore(j).getTotalScore();                }                int ave = (int)(sum/selectCourse.getStudent(i).c);                System.out.println(selectCourse.getStudent(i).getNum() + " " + selectCourse.getStudent(i).getName() + " " + ave);            }            else{                System.out.println(selectCourse.getStudent(i).getNum()+" "+selectCourse.getStudent(i).getName()+" did not take any exams");            }        }        for(int i = 0;i < selectCourse.k;i++) {            if (selectCourse.getCourse(i) != null) {                int sum = 0, ave = 0;                int sum1 = 0, ave1 = 0;                int sum2 = 0, ave2 = 0;                int n = 0;                for (int j = 0; j < selectCourse.s; j++) {                    for (int k = 0; k < selectCourse.getStudent(j).c; k++) {                        if (selectCourse.getStudent(j).getCourse(k).getName().equals(selectCourse.getCourse(i).getName())) {                            sum += selectCourse.getStudent(j).getScore(k).getTotalScore();                            sum1 += selectCourse.getStudent(j).getScore(k).getGradualScore();                            sum2 += selectCourse.getStudent(j).getScore(k).getEndTestScore();                            n++;                        }                    }                }                if (n != 0) {                    ave =(int)((float)(sum)/(float)(n));                    ave1 =(int)((float)(sum1)/(float)(n));                    ave2 =(int)((float)(sum2)/(float)(n));                    selectCourse.getCourse(i).setAve(ave);                    selectCourse.getCourse(i).setAve1(ave1);                    selectCourse.getCourse(i).setAve2(ave2);                }            }        }        Course course[]=selectCourse.listCoruse();        for (int i = 0;i< selectCourse.k;i++) {            if (course[i].getAve() != -1) {                if(course[i].getAssessment().equals("实验")){                    System.out.println(course[i].getName()+ " " + course[i].getAve());                }                else {                    if (course[i].getAve1() != -1) {                        System.out.println(course[i].getName() + " " + course[i].getAve1() + " " + course[i].getAve2() + " " + course[i].getAve());                    } else {                        System.out.println(course[i].getName() + " " + course[i].getAve2() + " " + course[i].getAve());                    }                }            }            else {                System.out.println(course[i].getName()+" has no grades yet");            }        }        for(int i = 0;ib){                    Classroom c = null;                    c = classroom[i];                    classroom[i]=classroom[j];                    classroom[j]=c;                }            }        }        for (int i =0;i courses = new ArrayList<>();    ArrayListscores=new ArrayList<>();    int c = 0;    int e = 0;    public Student(){    }    public String getName() {        return name;    }    public String getNum() {        return num;    }    public void setName(String name) {        this.name = name;    }    public void setNum(String num) {        this.num = num;    }    public void adderCourse(Course course,int num) {        courses.add(course);        courses.get(c).score.setEndTestScore(num);        c++;    }    public void adderCourse(Course course,Score score){        courses.add(course);        scores.add(score);        c++;    }    public Course getCourse(int i){        return courses.get(i);    }    public Score getScore(int i){        return scores.get(i);    }    public boolean isExist(String name){        int flag = 0;        for(int i =0 ;i students = new ArrayList<>();    private int ave = -1;    public int getAve() {        return ave;    }    public void setAve(int ave) {        this.ave = ave;    }    public void adderStudent(Student s){        students.add(s);    }    public Student getStudent(int i){        return students.get(i);    }    public void setNum(String num) {        this.num = num;    }    public String getNum() {        return num;    }}class SelectCourse {    private Student student;    private Course[] courses = new Course[20];    private Student[] students = new Student[20];    private Score[] scores = new Score[20];    int k=0;    int s=0;    int e = 0;    public Course getCourse(int i){        return courses[i];    }    public Student getStudent(int i){        return students[i];    }    public Course getCourse(String name) {        Course c = null;        for(int i = 0;ia1){       Student s =null;       s=students[i];       students[i]=students[j];       students[j]=s;      }   } }}    public void addCourse(String name,String nature,String assessment) {        courses[k]= new Course();        courses[k].setName(name);        courses[k].setNature(nature);        courses[k].setAssessment(assessment);        k++;    }    public void addCourse(String name,String nature,String assessment,int n,double[] q) {        courses[k]= new Course();        courses[k].setName(name);        courses[k].setNature(nature);        courses[k].setAssessment(assessment);        courses[k].setN(n);        courses[k].setQ(q);        k++;    }    public Score getScore(int i) {        return scores[i];    }    public void addScore(int num,int num1) {        this.scores[e] =new Score();        scores[e].setGradualScore(num);        scores[e].setEndTestScore(num1);        e++;    }    public Student getStudent() {        return student;    }    public void setStudent(Student student) {        this.student = student;    }    public Course[] listCoruse(){        Course courses1[] =new Course[k];        for(int i =0;ilist=Arrays.asList(courses1);        Collections.sort(list);        return courses1;    }}class Score {    private int endTestScore = -1;//期末成绩    private int gradualScore = -1;//平时成绩    private int totalScore;//总成绩    public void setEndTestScore(int endTestScore) {        this.endTestScore = endTestScore;    }    public void setGradualScore(int gradualScore) {        this.gradualScore = gradualScore;    }    public int getEndTestScore() {        return endTestScore;    }    public int getGradualScore() {        return gradualScore;    }    public int getTotalScore(){        int total;        if(getGradualScore()==-1){            total =getEndTestScore();        }        else {            total = (int)((float)(getGradualScore())*0.3+(float)(getEndTestScore())*0.7);        }        return total;    }}class Course implements Comparable{    private String name;//课程名称    private String nature;//课程种类    private String assessment = "考试";//考核方式    private int n;    private double[] q;    public void setN(int n) {        this.n = n;    }    public void setQ(double[] q) {        this.q = q;    }    public int getN() {        return n;    }    public double[] getQ() {        return q;    }    Score score =new Score();    private int totalScore;//总成绩    private int ave =-1;    private int ave1;    private int ave2;    public int getAve1() {        return ave1;    }    public int getAve2() {        return ave2;    }    public void setAve1(int ave1) {        this.ave1 = ave1;    }    public void setAve2(int ave2) {        this.ave2 = ave2;    }    public int getAve(){        return ave;    }    public void setAve(int ave){        this.ave =ave;    }    public Course(){    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAssessment() {        return assessment;    }    public String getNature() {        return nature;    }    public void setAssessment(String assessment) {        this.assessment = assessment;    }    public void setNature(String nature) {        this.nature = nature;    }    public Course(String name,String nature,String assessment){        this.name = name;        this.nature = nature;        this.assessment = assessment;    }    public Course(String name,String nature,String assessment,int n,double[] q){        this.name = name;        this.nature = nature;        this.assessment = assessment;        this.n = n;        this.q = q;    }    public int compareTo(Course course) {        Comparatorcom = Collator.getInstance(Locale.CHINA);        return ((Collator)com).compare(this.name,course.getName());    }}View Code

代码类图如下:

圈复杂度如下:

结构较为复杂;

踩坑心得1.对菜单题目中的各个问题,在对相关数据处理如对错误信息处理的判断中存在顺序的错误问题要着重考虑,以防止出现类似的输出顺序错误;2.在处理数据时,要考虑相关精度问题,利用float改变精度以防止可能存在的结果为小数的状况,而且对应相除的形式要排除是否会存在分母为0的情况,否则会出现非零返回;3.在使用类中的属性或者方法时,务必记得创建对象;同时在创建类的数组,需要使用其元素时也需要对当前元素new一下,否则会报空指针或者非零返回,java.lang.NullPointerException: Cannot read field "******" because "******" is null。4.对四舍五入我们可以使用Math.round的方式;5.在字符串判断是否相同的情况,一定用equals()判断,对数据的匹配处理使用matches();6.对字符串中查找关键字问题中,按顺序查找很关键,例如首先要排除/**/中数据,其次肯在“ ”中存在//的符号,所以应该将“ ”先排除后排除//,同时要认真·审题,对没有数据和输入为空时要做出相应输出。7.对HashMap的排序可以使用Arraylist的方式,利用Collection类的方法处理;

注意该方式是从大到小的排序方式;

8.对课程信息问题里要注意当成绩信息错误和课程名是否存在时只有对学生的姓名和学号都符合要求则要添加学生进入类中,注意其中的各个错误的各个数据的处理关系,同时添加班级要避免重复;

9.对冒泡的正确形式要熟悉,不要老是将j=i后i

10.处理字符串中的数据可以通过substring()方法去实现,可以减少代码量;

改进建议1.在处理类数据排序时,一昧的追求用冒泡去排序,逻辑较为复杂,可以通过Comparable中对compareTo方法复写以实现对类数组中的顺序排序;2.在输出时用for循环输出,太过老套,可以通过foreach的方式去遍历输出,更加新颖而且速度快;3.对逻辑的处理,对正则表达式的应用还是较少,普遍通过if方式去判断,逻辑太过于混乱,代码量也太大了;总结从10-16周里我学到了HashMap和TreeMap(有序)中相关的知识和概念,明确了两者区分;同时对javafx中的构图和各个知识点有了了解和运用,对轨迹、布局、反应、插图、动画都有了一定的了解和运用,能够自主去完成雨刷可视化和农夫过河可视化;同时认识了Stack和Queen的栈问题,一个正向一个反向,对其中的方法运用有了很大的理解;同时学会了对数据的排序方法复写CompareTo实现,sort();对正则表达式也有了进一步的人生,也更加深刻了对类的继承、多态和方法覆盖的意义,对HashSet的排序也有了对应的理解,对其中的方法和其不可重复的规则有了初步认识;当然还有很多不足,在做题时对题目的研究不够透彻,老是会因此花费很长时间,同时对一些高级简单的方法掌握不够透彻,对Arraylist和Hashset中的方法运用不够到位,在javafx中对轨迹的掌握不是很熟练;Java课程的教学理念“OBE”(以学生为中心),它带来了更加关注学生以及学生学习效果的教学方法,从而提高学生对课程的理解和掌握程度。我认为这是一个非常有用的教学理念。建议老师在教学中注重学生的个性化需求和差异化教育,根据学生不同的学习风格和学习能力,采用不同的教学策略和方法,以激发学生的学习热情,在课堂上老师对知识点进行扩充后再让我们进行练习;边讲边练的模式能让我们更加熟悉刚掌握的知识;线上线下的同时授课的方式,能让我们涉及到的知识也越丰富,但是可能会存在由于个人问题漏掉课程而无法跟紧步伐的情况,希望老师可以加强对线上课的监督和要求;教学过程中使用PTA题目集驱动,既能够及时发现学生的疑惑和难点,也可以促进学生的思考和创造能力,增加学生的自主学习时间。然而,需要注意作业的难度和数量,不能过于拖累学生。建议老师在指定作业的时候要注意作业难度和数量,以免伤害学生的学习积极性。;实验课程的设立能让我们对各个知识的运用更加的去探讨和研究,深化对各知识点的理解;

关键词: