JAVA第1-3次作业总结
前言:
Java是一门非常重要的计算机语言,也是非常广泛使用的编程语言之一。学习Java对我的职业发展和技术能力提升都有着很大的帮助。在我学习Java的过程中,我积累了一些心得体会,这些经验对我学习Java产生了很大的影响。
首先,我需要不断地练习。练习是我学好Java的关键。在本次的习题集里,我同样认识到了许多只有通过练习才能够学习到的知识。
其次,我需要找到适合自己的学习方法。每个人的学习方法都不同,所以我需要找到适合自己的学习方式。我采用的是多种学习方式结合起来的方式,包括看书、刷题、写代码等等。不同的方法可以让我更全面地理解Java 的知识,并提高我的编程技能。
另外,我还需要不断地更新自己的知识。Java 是一门发展迅速的编程语言,新的版本和技术层出不穷。因此,在我学习Java的过程中,我需要保持持续学习的态度,跟上最新的发展。我会定期了解最新版的 Java 语言规范,并研究一些新的开源工具、框架以及库等等。
最后,我也需要不断去拓宽自己的视野。学习Java只是我的个人能力提升之一,我需要关注整个行业的发展趋势,了解各种技术、创新和尝试,掌握一些与 Java 并行关联的高端知识和前沿技术。这将协助我更好地为未来的职业生涯做准备并不断提升自己的价值。
总而言之,学习Java需要不断的练习,找到适合自己的学习方法,更新自己的知识和拓宽自己的视野,这些都是非常重要的。只有在不断锤炼中,才能紧跟着行业最新发展潮流,提高自己的竞争力,同时为行业的进步和发展贡献出自己的力量。
目录:
一:三次作业遇到的BUG(踩坑警告)
二:代码思路与解决方法
三:作业总结与代码改进
四:一些感想
一:三次作业遇到的BUG(踩坑警告)
1:OOP训练集1
7-1 身体质量指数(BMI)测算
7-2 长度质量计量单位换算
7-3 奇数求和
7-4 房产税费计算2022
7-5 游戏角色选择
7-6 学号识别
7-7 判断三角形类型
7-8 巴比伦法求平方根近似值
7-9 二进制数值提取
2:OOP训练集2
7-1 菜单计价程序-1
7-2 菜单计价程序-2
7-3 jmu-java-日期类的基本使用
需要导入
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
7-4 小明走格子
如果直接使用Scanner则必然面临超时的问题,所以需要使用快读快写方法。
代码实现:
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)throws Exception
{
BufferedReader b1 = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(b1.readLine());
int[] whole = new int[10000];
int[] manner = new int[10000];
for(int i=0;i<n;i++)
{
whole[i] = Integer.parseInt(b1.readLine());
}
manner[0] = 1;
manner[1] = 1;
manner[2] = 2;
manner[3] = 4;
manner[4] = 8;
for(int i=0;i<n;i++)
{
if(whole[i]==0) System.out.println(0);
else if(whole[i]>=1&&whole[i]<=4)
{
int Index1 = whole[i];
System.out.println(manner[Index1]);
}
else if(whole[i]>=5)
{
int j=5;
for(;j<=whole[i];j++)
manner[j] = 2 * manner[j - 1] - manner[j - 5];
System.out.println(manner[j-1]);
}
}
}
}
3:OOP训练集3
7-1 菜单计价程序-3
7-2 有重复的数据
7-3 去掉重复的数据
7-4 单词统计与排序
7-5 面向对象编程(封装性)
7-6 GPS测绘中度分秒转换
不能用System.out.printf("%d°%d′%.6f″ = %.6f", du, fen, miao, decimal);
而要用System.out.print(du+"°"+fen+"′"+(float)miao+"″ = ");
原因是前面的方面限制的输出的位数。
7-7 判断两个日期的先后,计算间隔天数、周数
对于这类需要使用日期类来解决的题目,首先需要
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
然后再根据题目要求作答。
二:代码思路与解决方法

代码实现:
import java.time.DayOfWeek; import java.util.Scanner; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.LocalTime; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int count = 1; //计算循环进行了多少次 String wholeJudge = ""; //判断何时进入下一个循环 Dish[] dishes = new Dish[100]; int menuCount = 0; //计算菜单数量 while(input.hasNext()){ //记录菜单信息 wholeJudge = input.next(); if(wholeJudge.equals("table")) break; //输入table后进入下一个模块 if(wholeJudge.equals("end")) break; //空菜单 if(count%2 != 0){ dishes[menuCount] = new Dish(); //创建一个新的对象 dishes[menuCount].getDishName(wholeJudge); //获取菜名 }else{ dishes[menuCount].getPrice(Integer.parseInt(wholeJudge));//获取单价 menuCount++; //菜单数量加一 } count++; } /*菜单模块结束*/ System.out.println("table 1: "); //输出 "table 1" Table[] tables = new Table[100]; //创建餐桌对象数组 int tableCount = input.nextInt(); //记录餐桌数量且餐桌数量从1开始 tables[tableCount] = new Table(); //创建一个餐桌对象 tables[tableCount].getTableNumber(tableCount);//获取桌号 wholeJudge = input.next(); tables[tableCount].getDate(wholeJudge); //获取日期 wholeJudge = input.next(); tables[tableCount].getTime(wholeJudge); //获取时间 /*第一张餐桌的时间信息记录完毕 */ count = 1; //记录循环进行了几次 boolean existDish = true; Order[] order = new Order[100]; //创建订单对象数组 int orderCount = 0; //记录订单数量 order[orderCount] = new Order(); //创建一个订单对象 double everyPriceMain = 0; //每份菜的价格 double evertTablePrice = 0; //每桌菜的总价 int deleteNumber = 0; //删除订单编号 while(input.hasNext()){ wholeJudge = input.next(); if(wholeJudge.equals("end")){ for(int i=1;i<=tableCount;i++){ for(int j=0;j<orderCount;j++){ //System.out.println(order[j].setEveryPrice()); tables[i].getWholePrice(order[j].setEveryPrice()); } tables[i].showTable(); //输出每桌的总价 } break; } if(wholeJudge.equals("table")){ tableCount++; //餐桌数量加一 tables[tableCount] = new Table(); wholeJudge = input.next(); tables[tableCount].getDate(wholeJudge); //获取日期 wholeJudge = input.next(); tables[tableCount].getTime(wholeJudge); //获取时间 } if( count%4 == 1) { order[orderCount].getOrderNumber(Integer.parseInt(wholeJudge));//获取订单编号 deleteNumber = Integer.parseInt(wholeJudge); count++; } else if( count%4 == 2) { /*输入delete*/ if(wholeJudge.equals("delete")){ count = 1; if(deleteNumber>orderCount-1){ System.out.println("delete error"); }else { order[deleteNumber-1].getPortion(0); } } /*未输入delete*/ else { order[orderCount].getOrderName(wholeJudge); count++; int i = 0; //判断是否循环到底 for (i = menuCount - 1; i >= 0; i--) { if (dishes[i].isDish(order[orderCount].orderName)) { order[orderCount].getUnitPrice(dishes[i].price); //获取相对应的菜品的单价 existDish = true; break; } } if(i==-1) { System.out.println(wholeJudge+" does not exist"); existDish = false; } } } else if( count%4 == 3){ order[orderCount].getPortion(Integer.parseInt(wholeJudge)); count++; }else if( count%4 == 0){ order[orderCount].getNumberOfDish(Integer.parseInt(wholeJudge));//获取份数 everyPriceMain = order[orderCount].setEveryPrice(); //输出每份菜的价格 if(existDish) { System.out.print(order[orderCount].orderNumber + " " + order[orderCount].orderName + " "); System.out.printf("%.0f\n", everyPriceMain); } count++; orderCount++; order[orderCount] = new Order(); //创建一个订单对象 } } } } class Dish{ public String DishName; public int price; public String getDishName(String newDishName){ DishName = newDishName; return DishName; } public int getPrice(int newPrice){ price = newPrice; return price; } public boolean isDish(String theName){ return theName.equals(DishName); } } class Order{ public int orderNumber; public String orderName; private int portion; private int numberOfDish; private int unitPrice; private double everyPrice; public String getOrderName(String newOrderName){ orderName = newOrderName; return orderName; } public int getPortion(int newPortion){ portion = newPortion; return portion; } public int getOrderNumber(int newOrderNumber){ orderNumber = newOrderNumber; return orderNumber; } public int getNumberOfDish(int newNumberOfDish){ numberOfDish = newNumberOfDish; return numberOfDish; } public int getUnitPrice(int newUnitPrice){ unitPrice = newUnitPrice; return unitPrice; } /*public double deletePrice(){ everyPrice = 0; return everyPrice; }*/ public double setEveryPrice(){ if(portion==1) everyPrice = unitPrice * numberOfDish; if(portion==2) everyPrice = Math.round( unitPrice * 1.5 *numberOfDish); if(portion==3) everyPrice = unitPrice * 2 *numberOfDish; if(portion==0) everyPrice = 0; return everyPrice; } } class Table{ private int tableNumber; private String date; private String time; private double Discount; private double wholePrice; public double getWholePrice(double newWholePrice){ wholePrice += newWholePrice; return wholePrice; } public int getTableNumber(int newTableNumber) { tableNumber = newTableNumber; return tableNumber; } public String getDate(String newDate){ date = newDate; return date; } public String getTime(String newTime){ time = newTime; return time; } public void showTable(){ double nowDiscount = setDiscount(); System.out.print("table "+tableNumber); if(nowDiscount==0) System.out.println(" out of opening hours"); else {System.out.print(": ");System.out.println(Math.round(wholePrice*Discount));} } public double setDiscount(){ String inputDate = date; String[] dateParts = inputDate.split("/"); // 使用正则表达式分割字符串 String formattedDate = dateParts[0] + "/" + String.format("%02d", Integer.parseInt(dateParts[1])) + "/" + String.format("%02d", Integer.parseInt(dateParts[2])); String inputTime = time; String[] timeParts = inputTime.split("/"); // 使用正则表达式分割字符串 String formattedTime = timeParts[0] + ":" + String.format("%02d", Integer.parseInt(timeParts[1])) + ":" + String.format("%02d", Integer.parseInt(timeParts[2])); DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); LocalDate judgeDate = LocalDate.parse(formattedDate,dateFormatter); DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); LocalTime judgeTime = LocalTime.parse(formattedTime,timeFormatter); LocalTime startEvenTime = LocalTime.parse("16:59:59", timeFormatter); LocalTime endEvenTime = LocalTime.parse("20:30:01", timeFormatter); LocalTime startNoonTime = LocalTime.parse("10:29:59", timeFormatter); LocalTime endTNoonTime = LocalTime.parse("14:30:01", timeFormatter); LocalTime weekendStartTime = LocalTime.parse("09:29:59", timeFormatter); LocalTime weekendEndTime = LocalTime.parse("21:00:01", timeFormatter); DayOfWeek dayOfWeek = judgeDate.getDayOfWeek(); int dayOfWeekValue = dayOfWeek.getValue(); if (dayOfWeekValue >= 1 && dayOfWeekValue <= 5) { if(judgeTime.isAfter(startNoonTime)&& judgeTime.isBefore(endTNoonTime)){ Discount = 0.6; }else if(judgeTime.isBefore(endEvenTime)&& judgeTime.isAfter(startEvenTime)){ Discount = 0.8; }else{ Discount = 0; //不在营业时间 } } else { if(judgeTime.isAfter(weekendStartTime)&& judgeTime.isBefore(weekendEndTime)){ Discount = 1; }else{ Discount = 0; //不在营业时间 } } return Discount; } }
代码分析:
这道题目是题目集中难度最大的一道,其主要难点在于多重结构导致代码结构复杂。
因此,在解决这道题目时,我们需要花费更多的时间和精力去理解多重结构所导致的代码复杂性。首先,我们可以通过查看已有的代码逐步理解其整体架构,找出其中存在的多重结构问题,比如大量的嵌套循环或者递归调用等等。
接下来,我们需要重新设计代码结构,将多重结构分解为相对独立的子结构,并着重优化每个子结构的实现方式。在这一过程中,我们可以寻求其他程序员或编程专家的帮助,在他们的指导下找到最佳的解决方案。同时,我们还可以结合一些常见的编程工具,比如调试器、测试框架和代码分析工具等等,从不同角度来审视自己的代码,找到其中可能存在的潜在问题,加以尽快修正。
该点菜系统涉及到菜品(Dish),订单(Order)和餐桌(Table)这三个类。
主函数方面,通过循环和条件判断获取菜单信息、记录餐桌信息、订单信息和价格计算等操作。
Dish 类表示菜品,包含菜名(DishName)和单价(price)属性,提供了获取菜名、获取单价、判断菜品名是否匹配的方法。
Order 类表示订单,包含订单编号、菜名、菜品数量、单价等属性,以及计算每份菜的价格的方法。
Table 类表示餐桌,包含桌号、日期、时间等属性,以及计算折扣、展示每桌的总价等方法。
代点菜信息包含:桌号 序号 菜品名称 份额 分数
代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。
程序最后按输入的先后顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。
每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。
代码分为以下几个部分:
菜单信息的记录:通过 Scanner 对象接收用户输入的菜名和单价,将其存储在 Dish 类的对象数组中。
餐桌信息的记录:通过 Scanner 对象接收用户输入的餐桌数量、日期和时间,将其存储在 Table 类的对象数组中。
订单信息的记录:通过 Scanner 对象接收用户输入的订单编号、菜名、菜品数量等,将其存储在 Order 类的对象数组中。
计算每桌总价:遍历订单数组,将每份菜的价格累加到相应的餐桌对象中,计算每桌的总价。
根据不同时间段提供折扣:根据餐桌上的日期和时间判断折扣比例,将折扣应用于每桌的总价。
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String inputStr = scanner.nextLine();
String[] inputArr = inputStr.split("[,. ]+");
Set<String> inputSet = new HashSet<>(Arrays.asList(inputArr));
List<String> inputList = new ArrayList<>(inputSet);
Collections.sort(inputList, new Comparator<String>(){
public int compare(String str1, String str2){
if(str1.length() != str2.length()){
return str2.length() - str1.length();
}else{
return str1.compareToIgnoreCase(str2);
}
}
});
for(String outputStr : inputList){
System.out.println(outputStr);
}
}
}

浙公网安备 33010602011771号