(一)前言:
本篇将介绍本人在对于学校布置的三个PTA习题的总结,首先对于三次题目的知识点,有涉及到java入门的输入,输出,以及用if-else语句对条件进行判断,同时也涉及到了对于浮点数格式化输出的要求(例如保留几位小数,以单精度浮点型输出还是双精度浮点型输出做出一定的要求),其中也包含String字符串数组的使用及初始化的同时进行赋值,以及String字符串的方法函数substring的使用,其中还包含有Math.abs()函数的使用以及对于两个浮点数相等的判断方法,对于字符串的函数的使用还包含有indexof()函数以及字符串的比较函数equals()函数等,以上是第一次题目集的主要知识点,对于第一次题目集来说,其中的题目量一般,题目难度也比较低,对于刚入们java的友友们比较友好,可以掌握java的一些基本知识。
对于第二次题目集来说,其题目量也不算大,但题目难度相较于第一次来说提升了不少,因为在第二次题目集中,开始涉及到了各种类的创建于使用,因为java是一门面向对象的编程语言,所以学习java的过程中,对于类的学习是必不可少的,在第二次题目集中涉及到了类中属性及方法的编写,对于String数组也开始大量的使用,对于其中的第三、四道题目,还涉及到了哈希表的使用,后面的题目还包含了无参构造方法以及有参构造方法的使用及编写,在第六题中用到了format()函数来对输出结果进行格式化,对于第七题,其中包含了多个类之间的相互关联等关系,其中的关系较为复杂,题目难度也有所提升,最后一题则考察了格式化输入(正则表达式),以及对于java库中有的日期类的使用。
对于第三次题目集,题目量是比较少的,且有两题和上一次重复,对于最后一题,其中知识点包含String类中split()等方法、Integer类中parseInt()等方法的用法,LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则,ChronoUnit类中DAYS、WEEKS、MONTHS等单位的用法等,题目难度一般,而对于第二题来说,其中包含了格式化输入,类的继承以及连接,同时还涉及到了集合等知识的使用,难度相较于其他题目高出了不少。
下面本人将分享自己的设计思路和分析、踩坑心得、主要困难以及改进建议及最后的总结。
(二)设计与分析:
题目集2的7-1:
创建学生类,包含
属性:学号(String)、姓名(String)、语文成绩(int)、数学成绩(int)、物理成绩(int)
方法:计算总分、计算平均分
输入5个学生的信息,将每个学生的信息封装在一个学生对象中。
按输入顺序依次输出5个学生的总分、平均分(精确到小数点后两位,舍去部分按四舍五入规则计入最后一位)。
浮点数保留小数的相关知识可参考:
https://blog.csdn.net/huaishuming/article/details/17752365
注意:未用学生类对象封装数据的,本题计0分
输入格式:
5个学生信息,每个学生信息格式:
学号+英文空格+姓名+英文空格+语文成绩+英文空格+数学成绩+英文空格+物理成绩
例如:
22201311 张琳 80 80 80
22201312 黄昊 66 82 81
22201313 李少辰 77 76 80
22201314 袁婷 62 79 90
22201315 朱哲一 74 98 94
输出格式:
5个学生信息,每个学生信息格式:
学号+英文空格+姓名+英文空格+总成绩+英文空格+平均分
例如:
22201311 张琳 240 80.00
22201312 黄昊 229 76.33
22201313 李少辰 233 77.67
22201314 袁婷 231 77.00
22201315 朱哲一 266 88.67
源码:
import java.util.Scanner; class studentone { private String id; private String name; private int language; private int math; private int physics; public void set(String id,String name,int language,int math,int physics) { this.id = id; this.name = name; this.language = language; this.math = math; this.physics = physics; } public String getid() { return id; } public String getname() { return name; } //求总分 public int num(int language,int math,int physics) { int num1 = language + math + physics; return num1; } //求平均分 public float average(int language,int math,int physics) { float average1 = (float)(language + math + physics)/3; return average1; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); studentone[] stu = new studentone[5]; String id1; String name1; int[] num2 = new int[5]; float[] average2 = new float[5]; int language1; int math1; int physics1; for(int i = 0;i < 5;i++) { stu[i] = new studentone(); id1 = scanner.next(); name1 = scanner.next(); language1 = scanner.nextInt(); math1 = scanner.nextInt(); physics1 = scanner.nextInt(); stu[i].set(id1,name1,language1,math1,physics1); num2[i] = stu[i].num(language1,math1,physics1); average2[i] = stu[i].average(language1,math1,physics1); } for(int j = 0;j < 5;j++) { System.out.print(stu[j].getid() + " " + stu[j].getname() + " " + num2[j] + " " + String.format("%.2f",average2[j])); if(j < 4) { System.out.println(""); } } } }
本题需只需要设计一个学生类,相应类图如下:
在学生类中包含有id、name、language、math、physics等属性,同时有set()和get()等方法来设置和获取其中的属性值,后面还包含了两个方法num()和average()来求总分和平均值,这题相对来说较为简单,唯一需要注意的地方是输出结果的要求,平均分四舍五入如保留小数点后两位,可以用到String.format()的方法。
题目集2的7-2:
创建成绩类,包含:
属性:平时成绩(int)、期末成绩(int)
方法:计算总成绩(计算规则:平时成绩*0.4+期末成绩*0.6,保留整数部分,小数部分直接丢弃)
创建学生类,包含:
属性:学号(String)、姓名(String)、语文成绩(成绩类)、数学成绩(成绩类)、物理成绩(成绩类)
方法:计算总分、计算平均分
输入3个学生的信息,将每个学生的信息封装在一个学生对象中。
按输入顺序依次输出3个学生的总分、平均分(精确到小数点后两位,舍去部分按四舍五入规则计入最后一位)。
浮点数保留小数的相关知识可参考:https://blog.csdn.net/huaishuming/article/details/17752365
注意:未用学生类对象封装数据的,本题计0分
输入格式:
依次输入3个学生的每门课成绩,每个学生成绩信息格式:
学号+英文空格+姓名+英文空格+课程名+英文空格+平时成绩+英文空格+期末成绩
注:3个学生的课程顺序可能会不一致
例如:
22201311 张琳 语文 70 80
22201311 张琳 数学 85 89
22201311 张琳 物理 75 83
22201312 黄昊 语文 66 78
22201312 黄昊 数学 76 82
22201312 黄昊 物理 83 82
22201313 李少辰 语文 86 76
22201313 李少辰 数学 78 76
22201313 李少辰 物理 87 76
输出格式:
3个学生信息,每个学生信息格式:
学号+英文空格+姓名+英文空格+总成绩+英文空格+平均分
例如:
22201311 张琳 242 80.67
22201312 黄昊 234 78.00
22201313 李少辰 236 78.67
源码:
import java.util.Scanner; class scoretwo { private int usual; private int end; public void set(int usual,int end) { this.usual = usual; this.end = end; } public int getusual() { return usual; } public int getend() { return end; } public int num(int usual,int end) { int num1 = (int)(usual*0.4 + end*0.6); return num1; } } class studentone { private String id; private String name; private int language; private int math; private int physics; public void set(String id,String name) { this.id = id; this.name = name; } public void set1(int language,int math,int physics) { this.language = language; this.math = math; this.physics = physics; } public String getid() { return id; } public String getname() { return name; } //求总分 public int num(int language,int math,int physics) { int num1 = language + math + physics; return num1; } //求平均分 public float average(int language,int math,int physics) { float average1 = (float)(language + math + physics)/3; return average1; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); studentone[] stu = new studentone[3]; scoretwo[] score1 = new scoretwo[9]; String discipline,id1 = "",name1 = ""; int usual; int end; int[] num1 = new int[9]; int[] num2 = new int[3]; float[] usualaverage = new float[3]; float[] endaverage = new float[3]; for(int i = 0;i <3;i++) { stu[i] = new studentone(); for(int j = 0;j < 3;j++) { score1[i*3+j] = new scoretwo(); id1 = scanner.next(); name1 = scanner.next(); discipline = scanner.next(); usual = scanner.nextInt(); end = scanner.nextInt(); score1[i*3+j].set(usual,end); num1[i*3+j] = score1[i*3+j].num(usual,end); } stu[i].set(id1,name1); stu[i].set1(num1[0],num1[1],num1[2]); usualaverage[i] = (float)(score1[i*3].getusual() + score1[i*3+1].getusual() + score1[i*3+2].getusual())/3; endaverage[i] = (float)(score1[i*3].getend() + score1[i*3+1].getend() + score1[i*3+2].getend())/3; num2[i] = num1[i*3] + num1[i*3+1] + num1[i*3+2]; } for(int k = 0;k < 3;k++) { System.out.print(stu[k].getid() + " " + stu[k].getname() + " " + num2[k] + " " + String.format("%.2f",usualaverage[k]) + " " + String.format("%.2f",endaverage[k]) + " " + String.format("%.2f",(float)num2[k]/3)); if(k < 2) { System.out.println(""); } } } }
本题需要设计两个类,成绩类和学生类(可以沿用7-1中的学生类),相应类图如下:
首先沿用上一题7-1中的学会类,不需要做出修改,在创建的一个成绩类中包含两个私有属性usual、end,对这两个属性都有set和get方法,同时还设置有计算总成绩的方法num(),其他没有特殊的地方,和第一题相差不大。
题目集2的7-7:
某饭店提供4种菜,每种菜品的基础价格如下:
西红柿炒蛋 15
清炒土豆丝 12
麻婆豆腐 12
油淋生菜 9
设计点菜计价程序,根据输入的订单,计算并输出总价格。
订单由一条或多条点菜记录组成,每条记录一行,最后以"end"结束
每条点菜记录包含:菜名、份额两个信息。
份额可选项包括:1、2、3,分别代表小、中、大份)
不同份额菜价的计算方法:
小份菜的价格=菜品的基础价格。
中份菜的价格=菜品的基础价格1.5。
小份菜的价格=菜品的基础价格2。
如果计算出现小数,按四舍五入的规则进行处理。
参考以下类的模板进行设计:
菜品类:对应菜谱上一道菜的信息。
Dish {
String name;//菜品名称
int unit_price; //单价
int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
}
菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
Menu {
Dish[] dishs ;//菜品数组,保存所有菜品信息
Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。
}
点菜记录类:保存订单上的一道菜品记录
Record {
Dish d;//菜品
int portion;//份额(1/2/3代表小/中/大份)
int getPrice()//计价,计算本条记录的价格
}
订单类:保存用户点的所有菜的信息。
Order {
Record[] records;//保存订单上每一道的记录
int getTotalPrice()//计算订单的总价
Record addARecord(String dishName,int portion)
//添加一条菜品信息到订单中。
}
输入格式:
每条点菜记录的格式:
菜名+空格(英文)+份额
注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。
最后一条记录以“end”结束。
输出格式:
订单上所有菜品的总价(整数数值),每份菜
如果订单中包含不能识别的菜名,则在总价之前输出“** does not exist”,**是不能识别的菜名
源码:
import java.util.Scanner; public class Main { public static void main(String[] rrgs) { Scanner scanner = new Scanner(System.in); Dish[] dishs = new Dish[4]; for(int i1 = 0;i1 < 4;i1++) { dishs[i1] = new Dish(); } dishs[0].setname("西红柿炒蛋"); dishs[1].setname("清炒土豆丝"); dishs[2].setname("麻婆豆腐"); dishs[3].setname("油淋生菜"); dishs[0].setprice(15); dishs[1].setprice(12); dishs[2].setprice(12); dishs[3].setprice(9); Menu menu = new Menu(); menu.setdishs(dishs); String name; Dish[] names = new Dish[10000]; int[] portions = new int[10000]; int portion,n = 0; long num = 0; for(int i = 0;i < 10000;i++) { name = scanner.next(); if(name.equals("end")) { i = 10000; } else { n++; names[i] = new Dish(); names[i].setname(name); portion = scanner.nextInt(); portions[i] = portion; } } Record[] records = new Record[n]; Record[] records0 = new Record[4]; for(int j0 = 0;j0 < 4;j0++) { records0[j0] = new Record(); records0[j0].setdish(dishs[j0]); } for(int j = 0;j < n;j++) { records[j] = new Record(); records[j].setdish(names[j]); records[j].setportion(portions[j]); } Dish[] dishs1 = new Dish[n]; Order order = new Order(); order.setrecords(records); for(int k = 0;k < n;k++) { dishs1[k] = new Dish(); dishs1[k] = records[k].getdish(); Dish dish = new Dish(); dish = menu.searthdish(dishs1[k].getname()); if(dish.getname().equals("西红柿炒蛋")) num += records0[0].getprice(records[k].getportion()); else if(dish.getname().equals("清炒土豆丝")) num += records0[1].getprice(records[k].getportion()); else if(dish.getname().equals("麻婆豆腐")) num += records0[2].getprice(records[k].getportion()); else if(dish.getname().equals("油淋生菜")) num += records0[3].getprice(records[k].getportion()); else System.out.println(records[k].getdish().getname() + " does not exist"); } System.out.println(num); } } class Dish { private String name; private long price ; public void setname(String name) { this.name = name; } public void setprice(int price) { this.price = price; } public String getname() { return name; } public long getprice() { return price; } public long getprice(int f) { if(f == 1) return price; else if(f == 2) return Math.round((float)price*1.5); else if(f == 3) return price*2; else return 0; } } class Menu { private Dish[] dishs = new Dish[4]; public void setdishs(Dish[] dishs) { this.dishs = dishs; } public Dish searthdish(String dishname) { for(int i = 0;i < 4;i++) { if(dishname.equals(dishs[i].getname())) { return dishs[i]; } } Dish no = new Dish(); no.setname(dishname); return no; } } class Record { Dish d = new Dish(); private int portion; public void setdish(Dish d) { this.d = d; } public void setportion(int portion) { this.portion = portion; } public Dish getdish() { return d; } public int getportion() { return portion; } public long getprice(int portion) { return d.getprice(portion); } } class Order { Record[] records; public void setrecords(Record[] records) { this.records = new Record[records.length]; } public int getTotalPrice(Record[] records) { int TotalPrice = 0; for(int i = 0;i < records.length;i++) { TotalPrice += records[i].getprice(records[i].getportion()); } return TotalPrice; } public Record addRecord(String dishname,int portion) { Record a = new Record(); Dish b = new Dish(); b.setname(dishname); a.setdish(b); a.setportion(portion); return a; } }
本题需要设计四个类,菜品类、菜谱类、点菜记录类、订单类,相应类图如下:
首先创建一个菜品类包含私有属性name、unit_price,其中还包含getPrice()//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
其次需要创建一个菜谱类,其中对应菜谱,包含饭店提供的所有菜的信息,其中包含菜品数组,保存所有菜品信息,其中包含searthDish()方法来根据菜名在菜谱中查找菜品信息,返回Dish对象。
在创建一个点菜记录类来保存订单上的一道菜品记录,其中包含私有属性d、portion,还存在一个方法getPrice()来记录本条的价格。
最后还需要创建一个订单类,其中包含私有records,其中还包含方法getTotalPrice()来计算订单的总价以及Record addARecord()来添加一条菜品信息到订单中。
本题需要注意最后总的价格是只保留整数部分所以可以用到Math.round()方法,但是要注意其返回值是long类型,在主函数中,开始需要初始化菜谱类,其中包含题目中给出的所有菜名及价格,还有需要注意在对于输入格式错误的判断以及输入菜名错误的判断。
题目集3的7-7:
某高校课程从性质上分为:必修课、选修课,从考核方式上分为:考试、考察。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。
考察的总成绩直接等于期末成绩
必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。
课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
课程性质输入项:必修、选修
考核方式输入选项:考试、考察
课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
以上信息的相关约束:
1)平时成绩和期末成绩的权重默认为0.3、0.7
2)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】
3)学号由8位数字组成
4)姓名不超过10个字符
5)课程名称不超过10个字符
6)不特别输入班级信息,班级号是学号的前6位。
2、输出:
输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。
为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。
1)学生课程总成绩平均分按学号由低到高排序输出
格式:学号+英文空格+姓名+英文空格+总成绩平均分
如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"
2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出
格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分
如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"
3)班级所有课程总成绩平均分按班级由低到高排序输出
格式:班级号+英文空格+总成绩平均分
如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"
异常情况:
1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"
以上两种情况如果同时出现,按第一种情况输出结果。
3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"
4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"
5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。
信息约束:
1)成绩平均分只取整数部分,小数部分丢弃
源码:
import java.text.Collator; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int coursenumber = 0,studentnumber = 0,examinationnumber = 0,inspectnumber = 0; int[] sc1 = new int[10]; String[][] sc = new String[30][]; sc[0] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sc[1] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sc[2] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sc[3] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sc[4] = new String[]{" "," "," "," "," "," "," "," "," "," "}; int[] sg1 = new int[10]; String[][] sg = new String[30][]; sg[0] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sg[1] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sg[2] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sg[3] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sg[4] = new String[]{" "," "," "," "," "," "," "," "," "," "}; Course[] courses = new Course[30]; Student[] students = new Student[30]; Examination[] examinations = new Examination[30]; Inspect[] inspects = new Inspect[30]; int i = 0,k = 1,b = 0; while(k != 0) { String message = scanner.nextLine(); if(message.equals("end")) { k = 0; } else { if(judgment0(message)) { String[] split = message.split(" "); if(isNumeric(split[0])) { if(studentnumber == 0 && split[0].length() == 8) { int b2 = -1; students[studentnumber] = new Student(); students[studentnumber].setid(split[0]); students[studentnumber].setname(split[1]); for(int i2 = 0;i2 < coursenumber;i2++) { if(courses[i2].getname().equals(split[2])) { b2 = i2; sc[studentnumber][sc1[studentnumber]] = split[2]; } } if(b2 == -1) { System.out.println(split[2] + " does not exist"); } else { if((courses[b2].getway().equals("考试") && split.length == 4) || (courses[b2].getway().equals("考察") && split.length == 5)) { System.out.println(split[0] + " " + split[1] + " : access mode mismatch"); } else { if(courses[b2].getway().equals("考试")) { sg[studentnumber][sg1[studentnumber]] = split[3] + " " + split[4]; sg1[studentnumber]++; examinations[examinationnumber] = new Examination(); examinations[examinationnumber].setcourse(split[2]); examinations[examinationnumber].setusual(Integer.parseInt(split[3])); examinations[examinationnumber].setend(Integer.parseInt(split[4])); examinationnumber++; } else { sg[studentnumber][sg1[studentnumber]] = split[3]; sg1[studentnumber]++; inspects[inspectnumber] = new Inspect(); inspects[inspectnumber].setcourse(split[2]); inspects[inspectnumber].setend(Integer.parseInt(split[3])); inspectnumber++; } sc1[studentnumber]++; } } studentnumber++; } else { int b3 = -1; for(int i3 = 0;i3 < studentnumber;i3++) { if(split[0].equals(students[i3].getid()) && split[1].equals(students[i3].getname())) b3 = i3; } if(b3 != -1) { int b4 = -1; for(int i4 = 0;i4 < sc1[b3];i4++) { if(split[2].equals(sc[b3][i4])) b4 = 1; } if(b4 == -1) { int b5 = -1; for(int i5 = 0;i5 < coursenumber;i5++) { if(courses[i5].getname().equals(split[2])) { b5 = i5; sc[b3][sc1[b3]] = split[2]; } } if(b5 == -1) { System.out.println(split[2] + " does not exist"); } else { if((courses[b5].getway().equals("考试") && split.length == 4) || (courses[b5].getway().equals("考察") && split.length == 5)) { System.out.println(split[0] + " " + split[1] + " : access mode mismatch"); } else { if(courses[b5].getway().equals("考试")) { sg[b3][sg1[b3]] = split[3] + " " + split[4]; sg1[b3]++; examinations[examinationnumber] = new Examination(); examinations[examinationnumber].setcourse(split[2]); examinations[examinationnumber].setusual(Integer.parseInt(split[3])); examinations[examinationnumber].setend(Integer.parseInt(split[4])); examinationnumber++; } else { sg[b3][sg1[b3]] = split[3]; sg1[b3]++; inspects[inspectnumber] = new Inspect(); inspects[inspectnumber].setcourse(split[2]); inspects[inspectnumber].setend(Integer.parseInt(split[3])); inspectnumber++; } sc1[b3]++; } } } } else { int b6 = -1; students[studentnumber] = new Student(); students[studentnumber].setid(split[0]); students[studentnumber].setname(split[1]); for(int i6 = 0;i6 < coursenumber;i6++) { if(courses[i6].getname().equals(split[2])) { b6 = i6; sc[studentnumber][sc1[studentnumber]] = split[2]; } } if(b6 == -1) { System.out.println(split[2] + " does not exist"); } else { if((courses[b6].getway().equals("考试") && split.length == 4) || (courses[b6].getway().equals("考察") && split.length == 5)) { System.out.println(split[0] + " " + split[1] + " : access mode mismatch"); } else { if(courses[b6].getway().equals("考试")) { sg[studentnumber][sg1[studentnumber]] = split[3] + " " + split[4]; sg1[studentnumber]++; examinations[examinationnumber] = new Examination(); examinations[examinationnumber].setcourse(split[2]); examinations[examinationnumber].setusual(Integer.parseInt(split[3])); examinations[examinationnumber].setend(Integer.parseInt(split[4])); examinationnumber++; } else { sg[studentnumber][sg1[studentnumber]] = split[3]; sg1[studentnumber]++; inspects[inspectnumber] = new Inspect(); inspects[inspectnumber].setcourse(split[2]); inspects[inspectnumber].setend(Integer.parseInt(split[3])); inspectnumber++; } sc1[studentnumber]++; } } studentnumber++; } } } else { if(coursenumber == 0) { if(split[1].equals("必修") && split[2].equals("考察")) { System.out.println(split[0] + " : course type & access mode mismatch"); } else { courses[coursenumber] = new Course(); courses[coursenumber].setname(split[0]); courses[coursenumber].setnature(split[1]); courses[coursenumber].setway(split[2]); coursenumber++; } } else { if(split[1].equals("必修") && split[2].equals("考察")) { System.out.println(split[0] + " : course type & access mode mismatch"); } else { int b1 = -1; for(int i1 = 0;i1 < coursenumber;i1++) { if(split[0].equals(courses[i1].getname())) b1 = 1; } if(b1 == -1) { courses[coursenumber] = new Course(); courses[coursenumber].setname(split[0]); courses[coursenumber].setnature(split[1]); courses[coursenumber].setway(split[2]); coursenumber++; } } } } } else { b = 1; } } } for(int i7 = 0;i7 < studentnumber;i7++) { students[i7].setcourse(sc[i7]); students[i7].setgrade(sg[i7]); } if(b == 1) { System.out.println("wrong format"); } int c = 0; int[] x = new int[10]; if(studentnumber != 0) { Student[][] class1 = new Student[3][]; Student classc = new Student(); class1[0] = new Student[5]; class1[1] = new Student[5]; class1[2] = new Student[5]; class1[0][0] = new Student(); class1[0][0] = students[0]; c++; x[0] = 1; for(int i11 = 1;i11 < studentnumber;i11++) { int b8 = -1; for(int i12 = c-1;i12 >= 0;i12--) { if(Integer.parseInt(students[i11].getid().substring(4,6)) == Integer.parseInt(class1[i12][0].getid().substring(4,6))) b8 = i12; } if(b8 == -1) { class1[c][0] = students[i11]; for(int i13 = c-1;i13 >=0;i13--) { if(Integer.parseInt(students[i11].getid().substring(4,6)) < Integer.parseInt(class1[i13][0].getid().substring(4,6))) { classc = class1[i13][0]; class1[i13][0] = students[i11]; class1[i13+1][0] = classc; } } x[c]++; c++; } } for(int i8 = 1;i8 < studentnumber;i8++) { int b7 = -1; for(int i9 = c-1;i9 >= 0;i9--) { if((Integer.parseInt(students[i8].getid().substring(4,6)) == Integer.parseInt(class1[i9][0].getid().substring(4,6))) && (Integer.parseInt(students[i8].getid().substring(6,8)) != Integer.parseInt(class1[i9][0].getid().substring(6,8)))) b7 = i9; } if(b7 != -1) { class1[b7][x[b7]] = students[i8]; for(int i10 = x[b7]-1;i10 >= 0;i10--) { if(Integer.parseInt(students[i8].getid().substring(6,8)) < Integer.parseInt(class1[b7][i10].getid().substring(6,8))) { classc = class1[b7][i10]; class1[b7][i10] = class1[b7][i10+1]; class1[b7][i10+1] = classc; } } x[b7]++; } } for(int i14 = 0;i14 < c;i14++) { for(int i15 = 0;i15 < x[i14];i15++) { if((int)class1[i14][i15].getaverage() != 0) System.out.println(class1[i14][i15].getid() + " " + class1[i14][i15].getname() + " " + (int)class1[i14][i15].getaverage()); else System.out.println(class1[i14][i15].getid() + " " + class1[i14][i15].getname() + " did not take any exams"); } } ArrayList<Coursep> listCourse=new ArrayList<>(); for(int i18 = 0;i18 < coursenumber;i18++) { Coursep course=new Coursep(courses[i18].getname(),courses[i18].getnature(),courses[i18].getway()); listCourse.add(course); } Collections.sort(listCourse); int[][] average = new int[coursenumber][]; for(int i21=0;i21<listCourse.size();i21++){ int j = 0; average[i21] = new int[3]; Coursep course=listCourse.get(i21); if(course.getTestType().equals("考试")) { for(int i16 = 0;i16 < examinationnumber;i16++) { if(examinations[i16].getcourse().equals(course.getCourseName())) { average[i21][0] += examinations[i16].getusual(); average[i21][1] += examinations[i16].getend(); average[i21][2] += examinations[i16].getnum(); j++; } } if(average[i21][0] != 0 || average[i21][1] != 0 || average[i21][2] != 0) { System.out.println(course.getCourseName() + " " + (int)((double)average[i21][0]/j) + " " + (int)((double)average[i21][1]/j) + " " + (int)((double)average[i21][2]/j)); } else { System.out.println(course.getCourseName() + " has no grades yet"); } } if(course.getTestType().equals("考察")) { for(int i17 = 0;i17 < inspectnumber;i17++) { if(inspects[i17].getcourse().equals(course.getCourseName())) { average[i21][0] += inspects[i17].getend(); average[i21][1] += inspects[i17].getnum(); j++; } } if(average[i21][0] != 0 || average[i21][1] != 0) { System.out.println(course.getCourseName() + " " + (int)((double)average[i21][0]/j) + " " + (int)((double)average[i21][1]/j)); } else { System.out.println(course.getCourseName() + " has no grades yet"); } } } double[] b6 = new double[c]; for(int i19 = 0;i19 < c;i19++) { for(int i20 = 0;i20 < x[i19];i20++) { b6[i19] += class1[i19][i20].getaverage(); } if((int)b6[i19] != 0) { System.out.println(class1[i19][0].getid().substring(0,6) + " " + (int)(b6[i19]/x[i19])); } else { System.out.println(class1[i19][0].getid().substring(0,6) + " has no grades yet"); } } } else { ArrayList<Coursep> listCourse=new ArrayList<>(); for(int i18 = 0;i18 < coursenumber;i18++) { Coursep course=new Coursep(courses[i18].getname(),courses[i18].getnature(),courses[i18].getway()); listCourse.add(course); } for(int i19 = 0;i19 < listCourse.size();i19++) { Coursep course=listCourse.get(i19); System.out.println(course.getCourseName() + " has no grades yet"); } Collections.sort(listCourse); } } public static boolean isNumeric(String str){ for (int i = str.length();--i>=0;){ if (!Character.isDigit(str.charAt(i))){ return false; } } return true; } public static boolean judgment0(String message) { return message.matches(".{0,10} (必修|选修) (考试|考察)|[0-9]{8} .{0,10} .{0,10} ([0-9]|[1-9][0-9]|100) ([0-9]|[1-9][0-9]|100)|[0-9]{8} .{0,10} .{0,10} ([0-9]|[1-9][0-9]|100)"); } } class Coursep implements Comparable<Coursep>{ String courseName; String type; String testType; public Coursep() { } public Coursep(String courseName,String type,String testType) { this.courseName=courseName; this.type=type; this.testType=testType; } public String getCourseName(){ return courseName; } public String getType(){ return type; } public String getTestType(){ return testType; } @Override public int compareTo(Coursep o) { Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA); return compare.compare(courseName,o.getCourseName()); } } class Student { private String id; private String name; private String[] course; private String[] grade; public void setid(String id) { this.id = id; } public void setname(String name) { this.name = name; } public void setcourse(String[] course) { this.course = course; } public void setgrade(String[] grade) { this.grade = grade; } public String getid() { return id; } public String getname() { return name; } public int getnum() { int s = 0; int num = 0; for(int i = 0;i < grade.length;i++) if(!grade[i].equals(" ")) { s++; String[] split1 = grade[i].split(" "); if(split1.length == 1) { num += Integer.parseInt(split1[0]); } else { num += (int)(Integer.parseInt(split1[0])*0.3 + Integer.parseInt(split1[1])*0.7); } } return num; } public double getaverage() { int s = 0; int num = 0; for(int i = 0;i < grade.length;i++) { if(!grade[i].equals(" ")) { s++; String[] split1 = grade[i].split(" "); if(split1.length == 1) { num += Integer.parseInt(split1[0]); } else { num += (int)(Integer.parseInt(split1[0])*0.3 + Integer.parseInt(split1[1])*0.7); } } } return ((double)num/s); } } class Course { private String name; private String nature; private String way; public void setname(String name) { this.name = name; } public void setnature(String nature) { this.nature = nature; } public void setway(String way) { this.way = way; } public String getname() { return name; } public String getnature() { return nature; } public String getway() { return way; } } class Grade { private String course; public void setcourse(String course) { this.course = course; } public String getcourse() { return course; } } class Examination extends Grade { private int usual; private int end; public void setusual(int usual) { this.usual = usual; } public void setend(int end) { this.end = end; } public int getusual() { return usual; } public int getend() { return end; } public int getnum() { int num = (int)(usual*0.3 + end*0.7); return num; } } class Inspect extends Grade { private int end; public void setend(int end) { this.end = end; } public int getend() { return end; } public int getnum() { return end; } }
(源码还存在一些缺陷,只能通过本分测试点)
本题需要设计五个类,学生类、课程类、成绩类以及两个继承类考试成绩类,考察成绩类图如下:
首先创建一个学生类,其中包含私密属性id、name、course、grade,还包含对于这些私密属性的set和get方法,其中还包含getnum()方法来获取学生总成绩的方法,在这个方法中用到了Integer.parseInt()的方法来进行类型的转换以及sqlit()方法来分割字符串,同时还有getaverage()方法来获取平均分,其中用到的方法和getnum()相同。
其次创建一个课程类,其中包含私密属性name、nature、way,其中只包含对于各个属性的set和get方法。
再之后创建一个成绩类,其中包含私有属性course,还有两个类一个是考试成绩一个是考察成绩分别继承成绩类,考试成绩中有两个私有属性usual、end,其方法是求最后的总成绩,考察成绩中只有一个私有属性end,方法有或得最后总成绩的方法getnum()。
在主函数中也存在着一些方法,来帮助完成数据的输入和输出,放置在源码主函数的最后面。
(三)踩坑心得:
1.错误截图:
本题为题目集2的第三题,使用正常的嵌套循环方式编写
for(int i = 0;i < n;i++) { for(j = i+1;j < n;j++) { if(number[i] == number[j]) { sign = 1; System.out.print("YES"); System.exit(0); } } }
的话,会报错运行超时,这时可以使用哈希表。
class Solution { public boolean containsDuplicate(int[] nums) { Set<Integer> set = new HashSet<>(); for (int i = 0; i < nums.length; i++) { if (set.contains(nums[i])) return true; else set.add(nums[i]); } return false; } }
2.错误截图:
本题为题目集2的第七题,需要注意对于输入的判断不止仅限于范围
public int judgmentcorrect() { int leap = judgmentleap(); String str1 = date.substring(4,5); String str2 = date.substring(7,8); String str3 = date.substring(0,4); String str4 = date.substring(5,7); String str5 = date.substring(8); int month = Integer.parseInt(date.substring(5,7)); int day = Integer.parseInt(date.substring(8)); if(str1.equals("-") == false || str2.equals("-") == false || Date.isNumeric(str3) == false || Date.isNumeric(str4) == false || Date.isNumeric(str5) == false) { return 0; } else { if(month > 0 && month < 13) { if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if(day > 0 && day < 32) return 1; else return 0; } else if(month == 4 || month == 6 || month == 9 || month == 11) { if(day > 0 && day < 31) return 1; else return 0; } else if(month == 2 && leap == 1) { if(day > 0 && day < 30) return 1; else return 0; } else if(month == 2 && leap == 0) { if(day > 0 && day < 29) return 1; else return 0; } else return 0; } } return 0; }
记得需要对输入的格式也一起进行判断
if(str1.equals("-") == false || str2.equals("-") == false || Date.isNumeric(str3) == false || Date.isNumeric(str4) == false || Date.isNumeric(str5) == false) { return 0; }
(四)主要困难以及改进建议:
1.对于题目集2的最后一题(日期)的一些方法
public int judgmentleap() { if(Date.isNumeric(date.substring(0,4)) == false) { return 0; } int year = Integer.parseInt(date.substring(0,4)); if(year%4 == 0) { if(year%100 == 0) { if(year%400 == 0) return 1; else return 0; } else return 1; } else return 0; } public static boolean isNumeric(String str){ for (int i = str.length();--i>=0;){ if (!Character.isDigit(str.charAt(i))){ return false; } } return true; } public int judgmentcorrect() { int leap = judgmentleap(); String str1 = date.substring(4,5); String str2 = date.substring(7,8); String str3 = date.substring(0,4); String str4 = date.substring(5,7); String str5 = date.substring(8); if(str1.equals("-") == false || str2.equals("-") == false || Date.isNumeric(str3) == false || Date.isNumeric(str4) == false || Date.isNumeric(str5) == false) { return 0; } else { int month = Integer.parseInt(date.substring(5,7)); int day = Integer.parseInt(date.substring(8)); if(month > 0 && month < 13) { if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if(day > 0 && day < 32) return 1; else return 0; } else if(month == 4 || month == 6 || month == 9 || month == 11) { if(day > 0 && day < 31) return 1; else return 0; } else if(month == 2 && leap == 1) { if(day > 0 && day < 30) return 1; else return 0; } else if(month == 2 && leap == 0) { if(day > 0 && day < 29) return 1; else return 0; } else return 0; } } return 0; } public int judgmentyearday() { int leap = judgmentleap(); int month = Integer.parseInt(date.substring(5,7)); int day = Integer.parseInt(date.substring(8)); if(leap == 1) { int[] start = {0,31,60,91,121,152,182,213,244,274,305,335}; return start[month-1] + day; } else { int[] start = {0,31,59,90,120,151,181,212,243,273,304,334}; return start[month-1] + day; } } public int judgmentmonthday() { int day = Integer.parseInt(date.substring(8)); return day; } public int judgmentweekday() { int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(5,7)); int day = Integer.parseInt(date.substring(8)); int week; if (month == 1 || month == 2) { month += 12; year--; } week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7; return week; } public int totalday() { int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(5,7)); int day = Integer.parseInt(date.substring(8)); int total = 0; int[] start = {0,31,28,31,30,31,30,31,31,30,31,30,31}; for(int i = 1;i < year;i++) if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0) total += 366; else total += 365; if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) start[2] = 29; for(int j = 1;j < month;j++) total += start[j]; total += day; return total; }
以上方法在java8中其实就已经加入了,可以直接使用,不需要如此复杂,在此需要进行一定的改进简写,下面拿题目集3中的相似题来对比
public boolean judgment(LocalDate localDate) { if(localDate.isLeapYear()) { return date.matches("[0-9]{4}-(0[13578]-(0[1-9]|[12][0-9]|3[01])|1[02]-(0[1-9]|[12][0-9]|3[01])|02-(0[1-9]|1[0-9]|2[0-9])|0[469]-(0[1-9]|[12][0-9]|30)|11-(0[1-9]|[12][0-9]|30))"); } else { return date.matches("[0-9]{4}-(0[13578]-(0[1-9]|[12][0-9]|3[01])|1[02]-(0[1-9]|[12][0-9]|3[01])|02-(0[1-9]|1[0-9]|2[0-8])|0[469]-(0[1-9]|[12][0-9]|30)|11-(0[1-9]|[12][0-9]|30))"); } } public boolean isAfter(LocalDate localDate1,LocalDate localDate2) { return localDate1.isAfter(localDate2); } public long days(LocalDate localDate1,LocalDate localDate2) { return Math.abs(ChronoUnit.DAYS.between(localDate1,localDate2)); } public long weeks(LocalDate localDate1,LocalDate localDate2) { return Math.abs(ChronoUnit.WEEKS.between(localDate1,localDate2)); }
发现会方便很多,同时对于输入格式的判断也可以使用正则表达式来判断。
2.对于题目集3的第二题来说,开始使用的方法是“笨方法”对于一些类的对象的创建
Scanner scanner = new Scanner(System.in); int coursenumber = 0,studentnumber = 0,examinationnumber = 0,inspectnumber = 0; int[] sc1 = new int[10]; String[][] sc = new String[30][]; sc[0] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sc[1] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sc[2] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sc[3] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sc[4] = new String[]{" "," "," "," "," "," "," "," "," "," "}; int[] sg1 = new int[10]; String[][] sg = new String[30][]; sg[0] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sg[1] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sg[2] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sg[3] = new String[]{" "," "," "," "," "," "," "," "," "," "}; sg[4] = new String[]{" "," "," "," "," "," "," "," "," "," "}; Course[] courses = new Course[30]; Student[] students = new Student[30]; Examination[] examinations = new Examination[30]; Inspect[] inspects = new Inspect[30];
后面发现其实可以学习类与类之间的连接以及集合的使用
class Coursep implements Comparable<Coursep>{ String courseName; String type; String testType; public Coursep() { } public Coursep(String courseName,String type,String testType) { this.courseName=courseName; this.type=type; this.testType=testType; } public String getCourseName(){ return courseName; } public String getType(){ return type; } public String getTestType(){ return testType; } @Override public int compareTo(Coursep o) { Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA); return compare.compare(courseName,o.getCourseName()); } }
ArrayList<Coursep> listCourse=new ArrayList<>(); for(int i18 = 0;i18 < coursenumber;i18++) { Coursep course=new Coursep(courses[i18].getname(),courses[i18].getnature(),courses[i18].getway()); listCourse.add(course); }
同时这样还能写类与类之间的排序方法。
(五)总结:
对于本阶段的三个题目集,对于java程序的编写可以由初步到学会更多java库中本来就存有的一些方法,对于类有了进一步了解,到后面对于类之间的相互取用关系也有了更深的了解,对于题目集3的第二题需要编写大量的代码,对于类之间的相互关系也要了解的非常清楚才能正确的写出完整的程序,经过长时间的尝试也有了一定的进展,对于以后的学习,我还需要更深入的了解类与类之间的各种关系以及学会集合的使用,多使用与学习java库中已经加入的方法函数,减少代码量以及复杂度,总得来说,我清楚的认识到了java为什么是面向对象的程序语言,也知道了其和之前学习的C语言的区别。