JAVA作业总结
目录:
- 前言
- 设计与分析
- 踩坑心得
- 主要困难以及改进建议
- 总结
一、前言
1.第四次作业相对于后面的两次题目来说比较简单,题目代码量少,解题思路简单,涉及的类少,运用了Java中的数学类math使用了abs()
方法,考察点为Java语法的基本运用。
2.第五次 作业难度上升,加深了我对Java的进一步了解,也为后续的题目集提供了参考思路。在完成作业后,需要进行充分的测试来验证程序的正确性。可以考虑边界条件、异常输入等情况,确保程序能够正确处理各种情况。主要考察异常情况处理运用
3.第六次作业难度比第五次作业难度难度低,将程序模块化,使得每个模块负责特定的功能。增加了菜的口味处理。
二、设计与分析
1.菜单计价程序-2
import java.util.Scanner; public class Main { public static void main(String [] args) { Scanner input = new Scanner(System.in); Menu menu= new Menu(); Order order=new Order(); String dishInfo; String dishName; int i = 0; int b = 0,c = 0; dishInfo=input.nextLine(); while (dishInfo!=null&&!dishInfo.equals("end")) { String[] news=dishInfo.split(" "); int flag=0; if(news.length==2&&!news[1].equals("delete")) { int unit_price=0; dishName=news[0]; unit_price=Integer.parseInt(news[1]); menu.addDish(dishName,unit_price); } if(news.length==4) { int orderNum=0; int portion=0; int count1=0; dishName=news[1]; orderNum=Integer.parseInt(news[0]); portion=Integer.parseInt(news[2]); count1=Integer.parseInt(news[3]); if(menu.searchDish(dishName,menu.dishes)!=null) { order.addARecord(orderNum,dishName,portion,count1,menu.dishes); System.out.println(order.records[i].orderNum+" "+order.records[i].d.name+" "+order.records[i].getPrice()); i++; } else { flag=1; } if(flag==1) { System.out.println(dishName+" does not exist"); } } if(news[1].equals("delete")) { b = Integer.parseInt(news[0]); c += order.delARecordByOrderNum(b); } dishInfo=input.nextLine(); } System.out.println(order.getTotalPrice()-c); } } class Dish{ String name; int unit_price; public Dish(String name, int unit_price) { this.name=name; this.unit_price=unit_price; } public Dish() { this.name = " "; this.unit_price = 0; } public int getPrice(int portion) { double price = unit_price*(portion+1)/2.0; return(int) Math.round(price); } } class Menu { Dish[] dishes=new Dish[20]; static int i = 0; static int count=0; public Menu() { } public Menu(String dishName,int unit_price) { dishes[i++]=new Dish( dishName, unit_price); } void addDish(String dishName,int unit_price) { dishes[i++]=new Dish(dishName,unit_price); } Dish searchDish(String dishName,Dish[] dishes) { int j=0; count=i; for(j=count-1;j>=0;j--) { if(dishes[j].name.equals(dishName)) return dishes[j]; } return null; } } class Record { int orderNum; Dish d= new Dish(); int portion,amount; static int count=0; public Record() { } public Record(int orderNum,Dish d, int portion,int amount) { this.orderNum = orderNum; this.d = d; this.portion = portion; this.amount= amount; } public int getPrice() { return d.getPrice(portion)*amount; } } class Order { Record[] records=new Record[100]; static int count=0; int getTotalPrice() { int totalPrice = 0; for (int i = 0; i < count; i++) { totalPrice += records[i].getPrice(); } return totalPrice; } void addARecord(int orderNum, String dishName, int portion, int amount, Dish[] dishes) { Menu menu = new Menu(); Dish d = menu.searchDish(dishName, dishes); if(d != null) { records[count++] = new Record(orderNum, d, portion, amount); } } int delARecordByOrderNum(int orderNum) { if(orderNum<=0||orderNum>count) { System.out.println("delete error;"); return 0; } else { return records[orderNum-1].getPrice(); } } }
此题有些问题,有一个测试点过不了,此题设计了四个类,Dish类:该类表示菜品,包括菜品名称(name)和单价(unit_price),用了两个构造函数,一个用于初始化菜品名称和单价,另一个为空构造函数。getPrice方法用于计算该菜品在指定份数时的价格。Menu类:该类表示菜单,包括菜品数组dishes。还有添加菜品的方法addDish和查找菜品的方法searchDish。Record类:该类表示订单记录,包括订单号(orderNum)、菜品(dish)、份数(portion)和数量(amount)。用了一个构造函数和getPrice方法用于计算该订单记录的价格。Order类:该类表示订单,包括订单记录数组records。还有获取订单总价的方法getTotalPrice和添加/删除订单记录的方法addARecord和delARecordByOrderNum。
类图
2.菜单计价程序-3
import java.util.Calendar; import java.util.Scanner; public class Main { public static void main(String [] args) { Scanner input = new Scanner(System.in); Menu menu= new Menu(); Table[] tables=new Table[1000]; int tablecount=0,ordercount=0; String dishInfo; String dishName; Dish df; int i = 0; int a1=0,a2=0,a3=0; int b = 0,c = 0; while (true) { dishInfo=input.nextLine(); String[] news=dishInfo.split(" "); if(dishInfo.equals("end")) break; if(news.length==2) { if(news[1].equals("delete")) { b = Integer.parseInt(news[0]); c += tables[tablecount].order.delARecordByOrderNum(b); tables[tablecount].tableprice-=c; } else{ int unit_price=0; dishName=news[0]; unit_price=Integer.parseInt(news[1]); menu.addDish(dishName,unit_price); } } else if(news.length==4) { if(news[0].equals("table")) { tablecount++; ordercount=0; tables[tablecount] = new Table(); tables[tablecount].gettableTime(dishInfo); System.out.println("table " + tablecount+": "); } else{ int orderNum=0; int portion=0; int count1=0; dishName=news[1]; orderNum=Integer.parseInt(news[0]); portion=Integer.parseInt(news[2]); count1=Integer.parseInt(news[3]); if(menu.searchDish(dishName,menu.dishes)!=null) { tables[tablecount].order.addARecord(orderNum,dishName,portion,count1,menu.dishes); System.out.println(tables[tablecount].order.records[i].orderNum+" "+tables[tablecount].order.records[i].d.name+" "+tables[tablecount].order.records[i].getPrice()); tables[tablecount].tableprice +=tables[tablecount].order.records[i].getPrice(); i++; } ordercount++; if(menu.searchDish(dishName,menu.dishes)==null) { System.out.println(dishName+" does not exist"); } } } else if(news.length==5) { a1 = Integer.parseInt(news[1]); a2 = Integer.parseInt(news[3]); a3 = Integer.parseInt(news[4]); tables[tablecount].order.addARecord( a1, news[2], a2, a3,menu.dishes); df = menu.searchDish(news[2],menu.dishes); if (df != null) { tables[tablecount].order.records[i].d.unit_price = df.unit_price; int d = tables[tablecount].order.records[i].getPrice(); System.out.println(news[1] + " table " + tables[tablecount].tablenumber + " pay for table " + news[0] + " " + d); tables[tablecount].tableprice += d; } ordercount++; } } for(int j = 1;j<tablecount+1;j++) { tables[j].getfinalPrice(); } } } class Dish{ String name; int unit_price; public Dish(String name, int unit_price) { this.name=name; this.unit_price=unit_price; } public Dish() { this.name = " "; this.unit_price = 0; } public int getPrice(int portion) { double price = unit_price*(portion+1)/2.0; return(int) Math.round(price); } } class Menu { Dish[] dishes=new Dish[20]; static int i = 0; static int count=0; public Menu() { } public Menu(String dishName,int unit_price) { dishes[i++]=new Dish( dishName, unit_price); } void addDish(String dishName,int unit_price) { dishes[i++]=new Dish(dishName,unit_price); } Dish searchDish(String dishName,Dish[] dishes) { int j=0; count=i; for(j=count-1;j>=0;j--) { if(dishes[j].name.equals(dishName)) return dishes[j]; } return null; } } class Record { int orderNum; Dish d= new Dish(); int portion,amount; static int count=0; public Record() { } public Record(int orderNum,Dish d, int portion,int amount) { this.orderNum = orderNum; this.d = d; this.portion = portion; this.amount= amount; } public int getPrice() { return d.getPrice(portion)*amount; } } class Order { Record[] records=new Record[100]; static int count=0; int getTotalPrice() { int totalPrice = 0; for (int i = 0; i < count; i++) { totalPrice += records[i].getPrice(); } return totalPrice; } void addARecord(int orderNum, String dishName, int portion, int amount, Dish[] dishes) { Menu menu = new Menu(); Dish d = menu.searchDish(dishName, dishes); if(d != null) { records[count++] = new Record(orderNum, d, portion, amount); } } int delARecordByOrderNum(int orderNum) { if(orderNum<=0||orderNum>count) { System.out.println("delete error;"); return 0; } else { return records[orderNum-1].getPrice(); } } } class Table { int tablenumber; String tabletime; int year; int month; int day; int hour; int minute; int miao; int week; long tableprice; double discount=0; Order order=new Order(); void gettableTime(String tabletime){ this.tabletime = tabletime; processtime(); getdiscountPrice(); } void processtime() { String []tt=tabletime.split(" "); tablenumber = Integer.parseInt(tt[1]); String[] tt1 = tt[2].split("/"); String[] tt2 = tt[3].split("/"); year = Integer.parseInt(tt1[0]); month = Integer.parseInt(tt1[1]); day = Integer.parseInt(tt1[2]); hour = Integer.parseInt(tt2[0]); minute = Integer.parseInt(tt2[1]); miao = Integer.parseInt(tt2[2]); Calendar calendar = Calendar.getInstance(); calendar.set(year, (month-1), day); week = calendar.get(Calendar.DAY_OF_WEEK); if(week==1) week = 7; else week--; } void getdiscountPrice() { if(week>=1&&week<=5) { if(hour>17&&hour<20) discount=0.8F; else if(hour==17&&minute==00&&miao==00) discount=0.8F; else if(hour==20&&minute<30) discount=0.8F; else if(hour==20&&minute==30&&miao==0) discount=0.8F; else if(hour>=11&&hour<=13||hour==10&&minute>=30) discount=0.6F; else if(hour==14&&minute<30) discount=0.6F; else if(hour==14&&minute==30&&miao==0) discount=0.6F; } else { if(hour>=10&&hour<=20) discount= 1.0F; else if(hour==9&&minute>=30)discount= 1.0F; else if(hour==21&&minute<30||hour==21&&minute==30&&miao==0) discount= 1.0F; } } void getfinalPrice() { if(discount>0){ tableprice = Math.round(tableprice*discount); System.out.println("table " + tablenumber + ": " + tableprice); }else { System.out.println("table " + tablenumber + " out of opening hours"); } } }
此题还有一部分测试点没过,我定义了菜品类 Dish
,菜单类 Menu
,记录类 Record
,订单类 Order
,以及餐桌类 Table
。其中,菜菜单类用于存储菜品信息并提供搜索功能,记录类用于记录订单中的菜品信息和价格,订单类用于管理订单信息,餐桌类用于处理时间和折扣计算,并最终得出账单金额。程序的大致流程为,首先输入菜品信息和订单信息,并将菜品信息存储在菜单中。然后根据输入的命令处理订单信息,包括添加菜品记录、删除菜品记录等操作。最后处理时间信息,计算折扣,并计算最终账单金额。对时间和折扣的处理,通过 Calendar
类来获取具体的日期和星期信息,并根据具体的时间段计算折扣。
圈复杂度:
菜单计价程序-4
此题我对异常情况处理的不太好,也没有针对异常情况处理设置类,在针对输入菜单记录的语句处理有问题,导致菜单记录输入没有输出。
类图
圈复杂度:
菜单计价程序-5
import java.util.Scanner; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) throws ParseException { Menu menu = new Menu(); Scanner input = new Scanner(System.in); String dishInfo = input.nextLine(); while (!dishInfo.startsWith("table")) { String[] news = dishInfo.split(" "); if (news.length == 2) { String dishname = news[0]; int unit_price = Integer.parseInt(news[1]); if (menu.searchDish(dishname) == null) { menu.addDish(dishname, unit_price); } } else if (news.length == 4 && dishInfo.endsWith("T")) { String dishname = news[0]; String dishtype = news[1]; int unit_price = Integer.parseInt(news[2]); Map<String, String> map = new HashMap<String, String>() { { put("川菜", "Chuan"); put("晋菜", "Jin"); put("浙菜", "Zhe"); } }; DishType dishType = DishType.valueOf(map.get(dishtype)); if (menu.searchDish(dishname) == null) { menu.addDish(dishname, unit_price, dishType); } } else { System.out.println("wrong format"); } dishInfo = input.nextLine(); } ArrayList<Table> tables = new ArrayList<>(); ArrayList<String> names = new ArrayList<>(); int tablecount = 0; String name = null; String phone = null; Date date = null; Date time = null; boolean islegaltime = true; boolean islegalnews = true; String orderLine = dishInfo; while (!orderLine.equals("end")) { String[] orderInfo = orderLine.split(" "); if (orderLine.startsWith("table")) { islegalnews = true; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); SimpleDateFormat timeFormat = new SimpleDateFormat("HH/mm/ss"); tablecount = Integer.parseInt(orderInfo[1]); name = orderInfo[3]; phone = orderInfo[4]; try { date = dateFormat.parse(orderInfo[5]); time = timeFormat.parse(orderInfo[6]); } catch (Exception e) { islegalnews = false; System.out.println("wrong format"); orderLine = input.nextLine(); continue; } String regex = "^1(80|81|89|33|35|36)\\d{8}$"; Table table = new Table(tablecount, name, phone, date, time); tables.add(table); if (name.length() > 10 || !phone.matches(regex)) { islegalnews = false; System.out.println("wrong format"); orderLine = input.nextLine(); continue; } if (!names.contains(name)) { names.add(name); } if (table.getdiscount(true) == 0) { islegaltime = false; System.out.println("table " + table.tablecount + " out of opening hours"); } else { System.out.println("table " + table.tablecount + ": "); } } else { if (islegalnews) { int orderNum; try { orderNum = Integer.parseInt(orderInfo[0]); } catch (Exception e) { System.out.println("wrong format"); orderLine = input.nextLine(); continue; } if (orderLine.endsWith("delete")) { if (!tables.get(tablecount - 1).delRecordByOrderNum(orderNum)) { System.out.println("delete error"); } } else { if (orderInfo.length == 4) { String dishName = orderInfo[1]; int portion = Integer.parseInt(orderInfo[2]); int amount = Integer.parseInt(orderInfo[3]); Dish dish = menu.searchDish(dishName); if (dish == null) { System.out.println(dishName + " does not exist"); orderLine = input.nextLine(); continue; } Record record = new Record(tablecount, orderNum, dish, portion, amount); tables.get(tablecount - 1).addRecord(record); if (islegaltime) { System.out.println(record.print(tablecount)); } } else if (orderInfo.length == 5) { String dishName = orderInfo[1]; int stylenum = Integer.parseInt(orderInfo[2]); int portion = Integer.parseInt(orderInfo[3]); int amount = Integer.parseInt(orderInfo[4]); Dish dish = menu.searchDish(dishName); if (dish == null) { System.out.println(dishName + " does not exist"); orderLine = input.nextLine(); continue; } Record record = new Record(tablecount, orderNum, dish, stylenum, portion, amount); tables.get(tablecount - 1).addRecord(record); if (islegaltime) { System.out.println(record.print(tablecount)); } } else if (orderInfo.length == 6) { int othernumber = Integer.parseInt(orderInfo[1]); String dishName = orderInfo[2]; int stylenum = Integer.parseInt(orderInfo[3]); int portion = Integer.parseInt(orderInfo[4]); int amount = Integer.parseInt(orderInfo[5]); Dish dish = menu.searchDish(dishName); if (dish == null) { System.out.println(dishName + " does not exist"); orderLine = input.nextLine(); continue; } Record record1 = new Record(othernumber, orderNum, dish, stylenum, portion, amount); Record record2 = new Record(othernumber, orderNum, dish, stylenum, 0, amount); tables.get(tablecount - 1).addRecord(record1); tables.get(othernumber - 1).addRecord(record2); if (islegaltime) { System.out.println(record1.print(tablecount)); } } else { System.out.println("wrong format"); } } } } orderLine = input.nextLine(); } input.close(); for (Table table : tables) { if (table.flag && table.getTotalPrice() != 0) { System.out.println(table.printInfo()); } } names.sort(new Comparator<String>() { public int compare(String order1, String order2) { return order1.compareTo(order2); } }); for (String costumName : names) { int sum = 0; String costumPhone = null; for (Table table : tables) { if (table.name.equals(costumName)) { sum += table.getCheckedPrice(); costumPhone = table.phone; } } if (sum != 0) { System.out.println(costumName + " " + costumPhone + " " + sum); } } } } enum DishType { Chuan, Jin, Zhe, } class Dish { String name; int unit_price; DishType type; public Dish(String name, int unit_price, DishType type) { this.name = name; this.unit_price = unit_price; this.type = type; } public Dish() { this.name = " "; this.unit_price = 0; } public Dish(String name, int unit_price) { this.name = name; this.unit_price = unit_price; } public String toString() { return name; } } class Menu { public ArrayList<Dish> dishes = new ArrayList<>(); public Dish searchDish(String dishName) { for (Dish dish : dishes) { if (dish.name.equals(dishName)) { return dish; } } return null; } void addDish(String dishName, int unit_price) { dishes.add(new Dish(dishName, unit_price)); } void addDish(String dishName, int unit_price, DishType type) { dishes.add(new Dish(dishName, unit_price, type)); } } class Record { int orderNum; Dish dish=new Dish(); int portion, amount;//份量,份额; int stylenum; boolean flag; int othernumber; public Record(int othernumber, int orderNum, Dish dish, int portion, int amount) { this.orderNum = orderNum; this.dish = dish; this.portion = portion; this.amount = amount; this.stylenum = -1; this.flag = true; this.othernumber = othernumber; } public Record(int othernumber, int orderNum, Dish dish, int stylenum, int portion, int amount) { this.orderNum = orderNum; this.dish = dish; this.portion = portion; this.amount = amount; this.stylenum = stylenum; this.flag = check_stylenum(); this.othernumber = othernumber; } int getPrice() { if (!flag) return 0; double price = dish.unit_price*amount*(portion+1)/2.0; return(int) Math.round(price); } int getCheckedPrice(double discount) { return (int) Math.round(getPrice() * discount); } boolean check_stylenum() { switch (dish.type) { case Chuan: if (stylenum > 5 || stylenum < 0) { return false; } else { return true; } case Jin: if (stylenum > 4 || stylenum < 0) { return false; } else { return true; } case Zhe: if (stylenum > 3 || stylenum < 0) { return false; } else { return true; } default: return true; } } public String print(int tablecount) { if (flag == false) { switch (dish.type) { case Chuan: return "spicy num out of range :" + stylenum; case Jin: return "acidity num out of range :" + stylenum; case Zhe: return "sweetness num out of range :" + stylenum; default: return null; } } else { if (othernumber == tablecount) { return orderNum + " " + dish.toString() + " " + getPrice(); } return orderNum + " table " + tablecount + " pay for table " + othernumber + " " + getPrice(); } } public String toString() { return "Record [orderNum=" + orderNum + ", dish=" + dish + ", portion=" + portion + ", amount=" + amount + ", stylenum=" + stylenum + ", flag=" + flag + ", othernumber=" + othernumber + "]"; } } class Table { ArrayList<Record> records = new ArrayList<>(); int tablecount; String name; String phone; Date date; Date time; boolean flag; public Table(int tablecount, String name, String phone, Date date, Date time) { this.name = name; this.phone = phone; this.date = date; this.time = time; this.tablecount = tablecount; this.flag = true; } double getdiscount(boolean Special) throws ParseException { double discount = 0; SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm"); Calendar cal = Calendar.getInstance(); cal.setTime(date); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); // 营业时间 if (dayOfWeek == 1 || dayOfWeek == 7) { if (time.after(sdfTime.parse("9:29")) && time.before(sdfTime.parse("21:31"))) { discount = 1; } } else { if (time.after(sdfTime.parse("16:59")) && time.before(sdfTime.parse("20:31"))) { if (Special) { discount = 0.7; } else { discount = 0.8; } } else if (time.after(sdfTime.parse("10:29")) && time.before(sdfTime.parse("14:31"))) { if (Special) { discount = 0.7; } else { discount = 0.6; } } } if (discount == 0) { flag = false; } return discount; } int getTotalPrice() { int sum = 0; for (Record record : records) { sum += record.getPrice(); } return sum; } int getCheckedPrice() throws ParseException { int sum = 0; for (Record record : records) { if (record.stylenum != -1) { sum += record.getCheckedPrice(getdiscount(true)); } else { sum += record.getCheckedPrice(getdiscount(false)); } } return sum; } String getAvestylenum(DishType type) { String[] spicy = { "不辣", "微辣", "稍辣", "辣", "很辣", "爆辣" }; String[] acidity = { "不酸", "微酸", "稍酸", "酸", "很酸" }; String[] sweetness = { "不甜", "微甜", "稍甜", "甜" }; double sum = 0; double num = 0; for (Record record : records) { if (record.dish.type == type) { if (record.flag && tablecount == record.othernumber) { num += record.amount; sum += record.stylenum * record.amount; } } } if (num == 0) { return ""; } int ave = (int) Math.round(sum / num); switch (type) { case Chuan: return " 川菜 " + (int) num + " " + spicy[average]; case Jin: return " 晋菜 " + (int) num + " " + acidity[average]; case Zhe: return " 浙菜 " + (int) num + " " + sweetness[average]; default: return null; } } void addRecord(Record record) { records.add(record); } public String printInfo() throws ParseException { String chuan = getAvestylenum(DishType.Chuan); String jin = getAvestylenum(DishType.Jin); String zhe = getAvestylenum(DishType.Zhe); if (chuan == "" && jin == "" && zhe == "") { return "table " + tablecount + ": " + getTotalPrice() + " " + getCheckedPrice() + " "; } else { return "table " + tablecount + ": " + getTotalPrice() + " " + getCheckedPrice() + chuan + jin + zhe; } } boolean delRecordByOrderNum(int orderNum) { return records.removeIf(record -> record.orderNum == orderNum); } Record findRecordByOrderNum(int orderNum) { for (Record record : records) { if (record.orderNum == orderNum) { return record; } } return null; } }
整个程序设计了菜品类、菜单类、记录类、桌子类。菜品类包括菜品名称、单价、菜系类型等属性和方法,用于表示各种菜品信息。菜单类包括菜品列表和相关操作方法,用于管理所有菜品的增删改查操作。记录类包括订单号、菜品信息、份数、数量等属性和方法,用于表示顾客点菜的记录信息。桌子类包括桌号、顾客姓名、联系方式、就餐日期、就餐时间、订单记录等属性和方法,用于表示每张桌子的就餐信息和订单记录。
类图:
圈复杂度
期中考试题目:
期中考试的题目总体难度不高,上课的时候也分析过此类问题。考试的时候代码出现报错,虽然能立马反应出来是哪里的问题但是修改花的时间较长,导致后来都i没有多长时间去完成别的题目。平常完成作业,敲代码的速度也比较慢,没有在规定的时间内完成题目作答。后来考试结束后重新看了题目。成功完成,考试的题目难度不大,但是由于我队继承多态的问题不熟练,再加上敲代码的速度慢,不能及时完成。
三、踩坑心得
1.菜单计价程序2,刚开始我没有考虑到输入多个重复菜时以第一个出现的菜单为主,然后在处理删除记录的时候也没有考虑删除记录相同的情况,导致代码报错,重新设计了删除记录的方法后,问题大致解决,但仍然存在问题。
我的错误示例:
错误的删除记录方法设计
int delARecordByOrderNum(int orderNum) { int i=0; if(orderNum<0) { return 0; } for (i=0;i< count;i++) { if(records[i].orderNum==orderNum) return records[i].getPrice(); } System.out.println("delete error"); return 0; } }
设计导致错误的原因可能是方法名表明的是删除操作,但实际上返回的是被删除记录的价格,这样设计容易引起误解。然后删除记录,应该在找到符合条件的记录后进行删除操作,对于订单号小于0的情况,直接返回0,没有给出具体的错误信息或提示,这样不太利于调试和排查问题。
改进后的删除记录的方法设计:
int delARecordByOrderNum(int orderNum) { if(orderNum<0||orderNum>count) { System.out.println("delete error;"); return 0; } else { return records[orderNum-1].getPrice(); } }
这样设计对于订单号小于0或大于记录总数的情况进行了判断,避免了数组越界的可能性。
仍需要改进的是通过返回价格来实现删除操作,并没有进行记录的删除,可能导致误解。缺乏详细的错误处理机制,只是简单地打印了一条错误信息,并返回了0。
2.菜单计价程序3
多次修改仍然存在问题,后来补练,仍然存在问题。测试案例七偶尔会出现运行超时的情况。问题是,当删除相同的记录后,题目不在输出,我想极有可能是我删除记录的方法设计的还有问题。
3.菜单计价程序4
对异常情况处理不太熟悉,不太会使用使用try...catch语句可以进行异常处理,使用try块,异常被抛出时,使用catch块来处理这些异常。
4.菜单计价程序5
此题存在输入多道相同的菜以最后一次输入为准的问题,然后在计算菜价的时候也存在问题,所以部分测试点没过,具体是哪个问题我没有找出来,这次在运用了异常处理机制,对异常情况进行处理。
5.期中考试
求圆的周长或者面积这种问题是最基础的问题,在之前课堂测试的时候有写过,能做出来,但没有按照题目要求,后续的题目也是,设计类的时候没有按照题目给的类图设计类和类的方法。
四、主要困难及改进建议
总的来说我的代码可复用性不高,出现新的迭代,大多需要重新设计类和类的方法,否则会出现很多错误。对代码应该进行适当的模块化,将功能相近的代码封装成方法,提高代码的可读性和可维护性,代码记得注释吧,当时菜单计价程序写的什么没怎么注释,再加上时间拉的很长,每天登进pta看会,写点,时间久了就忘记了,经常要上下翻翻看看。出现输出结果不是理想结果时,在知道可能是哪个方法,哪一不符出错的情况下,找错误的代码都要找半天,排查起来很麻烦。给别人看,阅读起来也很困难。
五、总结
这是本学期Java第二阶段的学习,能看到自己获得了知识,但更多的是自己的不足,而且不足之处很明显。学习的东西虽然基础,但是掌握却需要大量联系。从最初适应Java语言变成灵活运用Java语言,再到后面运用多个类编写程序,还有很长的路需要自己去一步步践行。作为初学者,发现还有许多东西需要去掌握:
1.基础语法的巩固,在进行Java编程中,时常脱离不出C语言的习惯,导致出现语法错误,甚至不记得如何进行编写,这需要在练习中,不要只会对之前自己写的代码复制粘贴,还要留意写法。
2.多学习使用eclipse,这个编译软件很好用,功能很多,自动报错的功能对于代码的修改很有帮助,写pta不能只在那上面写代码,灵活运用eclipse对自己编写修改代码很有帮助。当然idea也好用,不过eclipse用习惯了。
3.不同类之间的关系,许多时候正是不知如何进行将类互相呼应,导致代码亢长,产生垃圾代码,这不利于学习;当能够了解,并运用类与类之间的关系,则能解决大多数问题。
加油吧