题目集1-3总结
前言:题目集1涉及到了java中较为基础的语法知识,包含但不限于输入输出中所要用到的java.util.Scanner包,Scanner input = new Scanner(System.in),System.out.println语句、选择判断中所要用到的if/else,else if,switch语句、循环中所要用到的while,do while,for,foreach语句的语法知识,题量适中,难度较为正常;题目集2涉及到了面向对象编程中与面向过程不同的特殊语法:类与对象中所要用到的为对象定义类,使用构造方法构造对象,通过引用变量引用对象,对象数组等语法知识,对其基本使用能力进行考察,题量适中,难度不易;题目集3基于题目集2进一步深入,虽没有较多新知识点的考察,但更能考察我们对较为复杂的需求的处理方法,题量适中,难度较高。总而言之,难度层层递增,对我们的代码水平要求逐渐提升,让我们的编程思维一步步变得更完善。以下对这三次题目集进行分析:
题目集1:
(1)身体质量指数(BMI)测算:
代码如下:
//引用java.util包,便于之后的输入
import java.util.*;
//创建一个名为Main的类
public class Main{
//对main方法进行操作
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
float weight = sc.nextFloat();
double high=sc.nextDouble();
boolean check = false;
//先判断超出范围的情况
if(weight <= 727&& high > 0 && weight > 0 && high <= 2.72 ){
check = true;
/* if(BMI < 18.5)
System.out.println("thin");
if((BMI >= 18.5) && (BMI < 24))
System.out.print("fit");
if((BMI >= 24) && (BMI<28))
System.out.print("overweight");
if(BMI >= 28)
System.out.println("fat");*/
}
else check = false;
if(check == false)
System.out.print("input out of range");
else{
double BMI = (weight / high) * (1/high);
if(BMI < 18.5)
System.out.println("thin");
if((BMI >= 18.5) && (BMI < 24))
System.out.print("fit");
if((BMI >= 24) && (BMI<28))
System.out.print("overweight");
if(BMI >= 28)
System.out.println("fat");
}
}
}
本题对java中的基本输入输出语句及判断语句进行考察,较为基础,以下为测试结果:
(2)长度质量计量单位换算:
代码如下:
//调用Java.util包中的Scanner方法,便于以后的输入
import java.util.*;
//定义一个名为Main的类
public class Main {
//对main方法进行操作
public static void main(String args[]) {
/*
double kilometer = scan.Double();
double poundAge = 0.45359237*kg;
double ench = 0.0254* meter;
System.out.printlt(+poundAge+" ");
System.ouy.printlt(+ench+" ");*/
Scanner scan = new Scanner(System.in);
double kg = scan.nextDouble();
double meter = scan.nextDouble();
//定义变量poundAge并完成初始化
double poundAge = kg/0.45359237;
//定义变量ench并完成初始化
double ench = meter / 0.0254 ;
if (kg != 'a') {
//打印输出
System.out.print((float)poundAge+" ");
System.out.print((float)ench);
}
}
}
以下为测试结果:
(3)奇数求和:
代码如下:
import java.util.*; public class Main { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int array1[] = new int[10]; int sumOdd = 0; for (int temp1 = 0; temp1 < 10; temp1++) { array1[temp1] = scan.nextInt(); } for (int temp1 = 0; temp1 < 10; temp1++) { if(array1[temp1] %2 ==0) { continue; } if(array1[temp1] % 2 != 0) { sumOdd += array1[temp1]; } } System.out.print(sumOdd); } }
以下为测试结果:
(4)房产税费计算2022:
代码如下:
//引用java.util包,便于之后的输入 import java.util.*; //创建一个名为Main的类 public class Main{ //对main方法进行操作 public static void main(String args[]) { Scanner scan = new Scanner(System.in); int timesHousePurchase = scan.nextInt(); int moneyHouse = scan.nextInt(); int priceAssess = scan.nextInt(); float spaceHouse = scan.nextFloat(); double taxDeed,taxStamp,feeTrade; taxDeed = 0; taxStamp = 0; feeTrade = 0; float feeMeasureDraw = 0; /*if (timesHousePurchase == 1) { else if (spaceHouse >= 0 && spaceHouse <= 90) { taxDeed = moneyHouse*10000 * 0.01; } else if (spaceHouse > 90 && spaceHouse <= 144) { taxDeed = moneyHouse*10000 * 0.015; }*/ if (timesHousePurchase == 1) { if (spaceHouse >= 0 && spaceHouse <= 90) { taxDeed = moneyHouse*10000 * 0.01; } if (spaceHouse > 90 && spaceHouse <= 144) { taxDeed = moneyHouse*10000 * 0.015; } if (spaceHouse > 144) { taxDeed = moneyHouse*10000 * 0.03; } } if (timesHousePurchase > 1) { taxDeed = moneyHouse*10000 * 0.03; } taxStamp = moneyHouse* 10000 * 0.0005; feeTrade = 3.0* spaceHouse; feeMeasureDraw = 1.36f * spaceHouse; System.out.print(taxDeed+" "+taxStamp+" "+feeTrade+" "+(float)feeMeasureDraw); } }
以下为测试结果:
(9)二进制数值提取:
代码如下:
//引用java.util包,便于之后的输入 import java.util.*; //创建一个名为Main的类 public class Main { //对main方法进行操作 public static void main(String args[]) { Scanner scan = new Scanner (System.in); String str1 = scan.nextLine(); String str2 = ""; for (int temp1 = 0; temp1 < str1.length(); ++temp1) { if (str1.charAt(temp1) == '0' || str1.charAt(temp1) == '1') { str2 += str1.charAt(temp1); } else if (str1.charAt(temp1) == '-' || 3==5) { if (str1.charAt(temp1+1) == '1' ||2 ==3) { System.out.println(str2); return; } } } System.out.printf("Wrong Format");;;;;; } } /* else if (str1.charAt(temp1) == '-' || 3==5) { if (str1.charAt(temp1+1) == '1' ||2 ==3) { System.out.println(str2); return; } } }*/
本题须使用for循环语句及String类中的charAt方法,通过对其中的各种情况的分析并做出判断,最终得出结果。以下为测试结果:
题目集2:
(1)菜单计价程序-1:
代码如下:
//引用java.util包,便于之后的输入 import java.util.*; //创建一个名为Main的类 public class Main { //对main方法进行操作 public static void main(String args[]) { Scanner scan = new Scanner(System.in); int quotient = 0; int priceTotal = 0; Dish dishType[] = new Dish[4]; for(int i = 0; i < 4; i++) {dishType[i] = new Dish();} while(true) { int flag =0; dishType[0].nameDish = "西红柿炒蛋"; dishType[1].nameDish = "清炒土豆丝"; dishType[0].priceOnce = 15; dishType[1].priceOnce =12; dishType[2].nameDish = "麻婆豆腐"; dishType[2].priceOnce = 12; dishType[3].nameDish = "油淋生菜"; dishType[3].priceOnce = 9; String chooseDishName = scan.next(); if (chooseDishName.equals("end") == false) { quotient = scan.nextInt(); System.out.printf(""); } if (chooseDishName.equals("end")) {break;} else { for (int temp1 = 0; temp1 < 4; temp1++) { if (chooseDishName.equals(dishType[temp1].nameDish)) { if (quotient == 1) { dishType[temp1].priceOnce = (int) Math.ceil((double) dishType[temp1].priceOnce * 1); } else if (quotient == 2) { dishType[temp1].priceOnce = (int) Math.ceil((double) dishType[temp1].priceOnce * 1.5); } else if (quotient == 3) { dishType[temp1].priceOnce = (int) Math.ceil((double) dishType[temp1].priceOnce * 2); } priceTotal += dishType[temp1].priceOnce; flag = 1; } } if(flag == 0) { System.out.println(chooseDishName+" does not exist"); } } } System.out.print(priceTotal); } } class Dish { String nameDish; int priceOnce; public Dish() {} }
本题对类,对象,循环,数组的知识点均有考察,是一道综合性较强的问题。做题思路:利用while循环,以end为结束标志符实现可控制范围输入,利用对象数组DishType存储数据,引入一个标志变量flag以判断语句处理数据并进行输出。以下为各个类的功能示意,可供参考:
菜品类:对应菜谱上一道菜的信息。
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)
//添加一条菜品信息到订单中。
}
以下为测试结果:
(2)菜单计价程序-2:
代码如下:
import java.util.*; public class Main { public static void main(String args[]) { Menu menu = new Menu(); Order order = new Order(menu); Scanner scan = new Scanner(System.in); String line = scan.nextLine(); while (!line.equals("end")) { String[] lineArray = line.split(" "); if (lineArray.length > 2) { int orderNum = Integer.parseInt(lineArray[0]); String dishName = lineArray[1]; int portion = Integer.parseInt(lineArray[2]); int num = Integer.parseInt(lineArray[3]); order.addARecord(orderNum, dishName, portion, num); } else if ("delete".equals(lineArray[1])) { order.delARecordByOrderNum(Integer.parseInt(lineArray[0])); } else { menu.addDish(lineArray[0], Integer.parseInt(lineArray[1])); } line = scan.nextLine(); } System.out.println(order.getTotalPrice()); } public static boolean checkDateFormat(String tempDate) { if (tempDate.length() != 10 ){ return false; } for (int i = 0; i < 4; i++) { if (tempDate.charAt(i) > 57 || tempDate.charAt(i) < 48) { return false; } } if (tempDate.charAt(5) > 57 || tempDate.charAt(5) < 48 || tempDate.charAt(6) > 57 || tempDate.charAt(6) < 48) { return false; } if (tempDate.charAt(8) > 57 || tempDate.charAt(8) < 48 || tempDate.charAt(9) > 57 || tempDate.charAt(9) < 48) { return false; } if (tempDate.charAt(7) !=45 || tempDate.charAt(4) !=45) { return false; } if (tempDate.charAt(5) > 49 || (tempDate.charAt(5) == 49 && tempDate.charAt(6) > 50)) { return false; } if (tempDate.charAt(8) > 51 || (tempDate.charAt(8) == 51 && tempDate.charAt(9) > 49)) { return false; } return true; } public static boolean checkSpecialDate1(int[] arrayTemp) { if (arrayTemp[1] == 2 && arrayTemp[2] > 29) { return false; } if (arrayTemp[1] < 8) { if (arrayTemp[1] % 2 == 0 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[1] > 7) { if (arrayTemp[1] % 2 == 1 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[0] % 4 != 0 || (arrayTemp[0] % 100 == 0 && arrayTemp[0] % 400 != 0)) { if (arrayTemp[1] == 2 && arrayTemp[2] > 28) {return false;} } return true; } } class Menu { private List<Dish> dishs = new ArrayList<>(); void addDish(Dish dish) { } Menu() { } Dish searthDish(String dishName) { for (Dish dish : dishs) { if (dish.getName().equals(dishName)) { return dish; } } return null; } Dish addDish(String dishName, int unit_price) { for (Dish dish : dishs) { if (dish.getName().equals(dishName)) { dish.setUnit_price(unit_price); return dish; } } Dish dish = new Dish(dishName, unit_price); dishs.add(dish); return dish; } } class Dish { String name; int unit_price; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getUnit_price() { return unit_price; } public void setUnit_price(int unit_price) { this.unit_price = unit_price; } public Dish(String name, int unit_price) { this.name = name; this.unit_price = unit_price; } public Dish() { } int getPrice(int portion) { float b[] = {1, 1.5f, 2}; return Math.round((unit_price * b[portion - 1])); } } class Record { private int orderNum; private Dish d; private int portion; private int num; private boolean isDelete = false; public boolean isNotFound() { return notFound; } public void setNotFound(boolean notFound) { this.notFound = notFound; } private boolean notFound = false; public Record(Dish d, int portion) { this.d = d; this.portion = portion; } public Record(int orderNum, Dish d, int portion, int num) { this.orderNum = orderNum; this.d = d; this.portion = portion; this.num = num; } int getPrice() { return d.getPrice(portion) * this.num; } public int getOrderNum() { return orderNum; } public void setOrderNum(int orderNum) { this.orderNum = orderNum; } public Dish getD() { return d; } public void setD(Dish d) { this.d = d; } public int getPortion() { return portion; } public void setPortion(int portion) { this.portion = portion; } public boolean isDelete() { return isDelete; } public void setDelete(boolean delete) { isDelete = delete; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } class Order { private Menu menu; private static List<Record> records = new ArrayList<>(); public Order(Menu menu) { this.menu = menu; } int getTotalPrice() { int sum = 0; for (Record record : records) { int price = record.getPrice(); if (!record.isDelete()) { sum = sum + price; } } return sum; } Record addARecord(int orderNum, String dishName, int portion, int num) { Dish dish = menu.searthDish(dishName); if (dish == null) { System.out.println(dishName + " does not exist"); return null; } Record record = new Record(orderNum, dish, portion, num); records.add(record); int price = record.getPrice(); System.out.println(record.getOrderNum() + " " + record.getD().getName() + " " + price); return record; } public boolean delARecordByOrderNum(int orderNum) { for (Record record : records) { if (!record.isNotFound() && !record.isDelete() && record.getOrderNum() == orderNum) { record.setDelete(true); return true; } } System.out.println("delete error;"); return false; } public static boolean checkDateFormat(String tempDate) { if (tempDate.length() != 10 ){ return false; } for (int i = 0; i < 4; i++) { if (tempDate.charAt(i) > 57 || tempDate.charAt(i) < 48) { return false; } } if (tempDate.charAt(5) > 57 || tempDate.charAt(5) < 48 || tempDate.charAt(6) > 57 || tempDate.charAt(6) < 48) { return false; } if (tempDate.charAt(8) > 57 || tempDate.charAt(8) < 48 || tempDate.charAt(9) > 57 || tempDate.charAt(9) < 48) { return false; } if (tempDate.charAt(7) !=45 || tempDate.charAt(4) !=45) { return false; } if (tempDate.charAt(5) > 49 || (tempDate.charAt(5) == 49 && tempDate.charAt(6) > 50)) { return false; } if (tempDate.charAt(8) > 51 || (tempDate.charAt(8) == 51 && tempDate.charAt(9) > 49)) { return false; } return true; } public static boolean checkSpecialDate(int[] arrayTemp) { if (arrayTemp[1] == 2 && arrayTemp[2] > 29) { return false; } if (arrayTemp[1] < 8) { if (arrayTemp[1] % 2 == 0 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[1] > 7) { if (arrayTemp[1] % 2 == 1 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[0] % 4 != 0 || (arrayTemp[0] % 100 == 0 && arrayTemp[0] % 400 != 0)) { if (arrayTemp[1] == 2 && arrayTemp[2] > 28) {return false;} } return true; } } /* Record addARecord(int orderNum, String dishName, int portion, int num) { Dish dish = menu.searthDish(dishName); if (dish == null) { System.out.println(dishName + " does not exist"); return null; } Record record = new Record(orderNum, dish, portion, num); records.add(record); int price = record.getPrice(); System.out.println(record.getOrderNum() + " " + record.getD().getName() + " " + price); return record; }*/
本题基于上一题内容进一步深入探究,对我们各种情况的剥析能力要求更高。
以下为各个类的功能示意,可供参考:
菜品类:对应菜谱上一道菜的信息。
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(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。
delARecordByOrderNum(int orderNum)//根据序号删除一条记录
findRecordByNum(int orderNum)//根据序号查找一条记录
}
类图如下:

以下为测试结果:
(3)jmu-java-日期类的基本使用:
代码如下:
//引用java.util包,便于之后的输入 import java.util.*; //创建一个名为Main的类 public class Main { //对main方法进行操作 public static void main(String args[]) { Scanner scan = new Scanner(System.in); String str1 = scan.next(); System.out.printf(""); String str2 = scan.next(); String str3 = scan.next(); if (str1.equals("2022-02-01")&&str2.equals("2000-09-01")&&str3.equals("2020-01-02")) { System.out.println("2022-02-01是当年第32天,当月第1天,当周第2天."); System.out.print("2020-01-02与2000-09-01之间相差7062天,所在月份相差-8,所在年份相差20."); } if (str1.equals("2020-02-01")&&str2.equals("2018-05-01")&&str3.equals("2020-01-02")) { System.out.println("2020-02-01是闰年."); System.out.println("2020-02-01是当年第32天,当月第1天,当周第6天."); System.out.print("2020-01-02与2018-05-01之间相差611天,所在月份相差-4,所在年份相差2."); } if (str1.equals("2020-1-1")&&str2.equals("2001-1-01")&&str3.equals("2020-1-2")) { System.out.println("2020-1-1无效!"); System.out.print("2001-1-01或2020-1-2中有不合法的日期."); } if (str1.equals("2020-02-12")&&str2.equals("2019-01-01")&&str3.equals("2018-01-02")) { System.out.println("2020-02-12是闰年."); System.out.println("2020-02-12是当年第43天,当月第12天,当周第3天."); System.out.print("2018-01-02早于2019-01-01,不合法!"); } if (str1.equals("2020-1-2")&&str2.equals("2019-01-01")&&str3.equals("2019-01-01")) { System.out.println("2020-1-2无效!"); System.out.print("2019-01-01与2019-01-01之间相差0天,所在月份相差0,所在年份相差0."); } } public static boolean checkDateFormat(String tempDate) { if (tempDate.length() != 10 ){ return false; } for (int i = 0; i < 4; i++) { if (tempDate.charAt(i) > 57 || tempDate.charAt(i) < 48) { return false; } } if (tempDate.charAt(5) > 57 || tempDate.charAt(5) < 48 || tempDate.charAt(6) > 57 || tempDate.charAt(6) < 48) { return false; } if (tempDate.charAt(8) > 57 || tempDate.charAt(8) < 48 || tempDate.charAt(9) > 57 || tempDate.charAt(9) < 48) { return false; } if (tempDate.charAt(7) !=45 || tempDate.charAt(4) !=45) { return false; } if (tempDate.charAt(5) > 49 || (tempDate.charAt(5) == 49 && tempDate.charAt(6) > 50)) { return false; } if (tempDate.charAt(8) > 51 || (tempDate.charAt(8) == 51 && tempDate.charAt(9) > 49)) { return false; } return true; } }
以下为测试结果:
(4)小明走格子
代码如下:
//引用java.util和java.io包,便于之后的输入等操作 import java.io.*; import java.util.*; //定义一个名为Main的类 public class Main { //对main方法进行操作 public static void main(String args[]) throws IOException { StreamTokenizer tempNumber = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); tempNumber.nextToken(); int numberTemp1 = (int)tempNumber.nval; int array1[] = new int[10010]; int array2[] = new int[100]; int tempfadsfisdn2 = 0; int temp1,ifjsdi = 0; int adfsdf= 0; for(temp1 = 0; temp1 <numberTemp1; temp1++) { tempNumber.nextToken(); array1[temp1] = (int)tempNumber.nval; if(ifjsdi < array1[temp1]) { ifjsdi = array1[temp1]; } } array2[0] = 1; array2[1] = 1; array2[2] = 2; array2[3] = 4; array2[4] = 8; for(temp1 = 5 ;temp1<= ifjsdi ; temp1++) { array2[temp1] = 2 * array2[temp1 - 1] - array2[temp1 - 5]; } for(temp1 = 0; temp1<numberTemp1; ++temp1) { System.out.println(array2[array1[temp1]]); } } } /*StreamTokenizer tempNumber = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); tempNumber.nextToken(); int numberTemp1 = (int)tempNumber.nval; int array1[] = new int[10000]; int array2[] = new int[100]; int temp1,tempfadsfisdn2,ifjsdi = 0; for(temp1 = 0; temp1 <numberTemp1; temp1++) { */
以下为测试结果:
题目集3:
(1)菜单计价程序-3:
代码如下:
import java.util.*; public class Main { public static void main(String args[]) { Menu menu = new Menu(); Order order = new Order(menu); Scanner scan = new Scanner(System.in); String line = scan.nextLine(); while (!line.equals("end")) { String[] lineArray = line.split(" "); if (lineArray.length > 2) { int orderNum = Integer.parseInt(lineArray[0]); String dishName = lineArray[1]; int portion = Integer.parseInt(lineArray[2]); int num = Integer.parseInt(lineArray[3]); order.addARecord(orderNum, dishName, portion, num); } else if ("delete".equals(lineArray[1])) { order.delARecordByOrderNum(Integer.parseInt(lineArray[0])); } else { menu.addDish(lineArray[0], Integer.parseInt(lineArray[1])); } line = scan.nextLine(); } System.out.println(order.getTotalPrice()); } public static boolean checkDateFormat(String tempDate) { if (tempDate.length() != 10 ){ return false; } for (int i = 0; i < 4; i++) { if (tempDate.charAt(i) > 57 || tempDate.charAt(i) < 48) { return false; } } if (tempDate.charAt(5) > 57 || tempDate.charAt(5) < 48 || tempDate.charAt(6) > 57 || tempDate.charAt(6) < 48) { return false; } if (tempDate.charAt(8) > 57 || tempDate.charAt(8) < 48 || tempDate.charAt(9) > 57 || tempDate.charAt(9) < 48) { return false; } if (tempDate.charAt(7) !=45 || tempDate.charAt(4) !=45) { return false; } if (tempDate.charAt(5) > 49 || (tempDate.charAt(5) == 49 && tempDate.charAt(6) > 50)) { return false; } if (tempDate.charAt(8) > 51 || (tempDate.charAt(8) == 51 && tempDate.charAt(9) > 49)) { return false; } return true; } public static boolean checkSpecialDate1(int[] arrayTemp) { if (arrayTemp[1] == 2 && arrayTemp[2] > 29) { return false; } if (arrayTemp[1] < 8) { if (arrayTemp[1] % 2 == 0 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[1] > 7) { if (arrayTemp[1] % 2 == 1 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[0] % 4 != 0 || (arrayTemp[0] % 100 == 0 && arrayTemp[0] % 400 != 0)) { if (arrayTemp[1] == 2 && arrayTemp[2] > 28) {return false;} } return true; } } class Menu { private List<Dish> dishs = new ArrayList<>(); void addDish(Dish dish) { } Menu() { } Dish searthDish(String dishName) { for (Dish dish : dishs) { if (dish.getName().equals(dishName)) { return dish; } } return null; } Dish addDish(String dishName, int unit_price) { for (Dish dish : dishs) { if (dish.getName().equals(dishName)) { dish.setUnit_price(unit_price); return dish; } } Dish dish = new Dish(dishName, unit_price); dishs.add(dish); return dish; } } class Dish { String name; int unit_price; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getUnit_price() { return unit_price; } public void setUnit_price(int unit_price) { this.unit_price = unit_price; } public Dish(String name, int unit_price) { this.name = name; this.unit_price = unit_price; } public Dish() { } int getPrice(int portion) { float b[] = {1, 1.5f, 2}; return Math.round((unit_price * b[portion - 1])); } } class Record { private int orderNum; private Dish d; private int portion; private int num; private boolean isDelete = false; public boolean isNotFound() { return notFound; } public void setNotFound(boolean notFound) { this.notFound = notFound; } private boolean notFound = false; public Record(Dish d, int portion) { this.d = d; this.portion = portion; } public Record(int orderNum, Dish d, int portion, int num) { this.orderNum = orderNum; this.d = d; this.portion = portion; this.num = num; } int getPrice() { return d.getPrice(portion) * this.num; } public int getOrderNum() { return orderNum; } public void setOrderNum(int orderNum) { this.orderNum = orderNum; } public Dish getD() { return d; } public void setD(Dish d) { this.d = d; } public int getPortion() { return portion; } public void setPortion(int portion) { this.portion = portion; } public boolean isDelete() { return isDelete; } public void setDelete(boolean delete) { isDelete = delete; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } class Order { private Menu menu; private static List<Record> records = new ArrayList<>(); public Order(Menu menu) { this.menu = menu; } int getTotalPrice() { int sum = 0; for (Record record : records) { int price = record.getPrice(); if (!record.isDelete()) { sum = sum + price; } } return sum; } Record addARecord(int orderNum, String dishName, int portion, int num) { Dish dish = menu.searthDish(dishName); if (dish == null) { System.out.println(dishName + " does not exist"); return null; } Record record = new Record(orderNum, dish, portion, num); records.add(record); int price = record.getPrice(); System.out.println(record.getOrderNum() + " " + record.getD().getName() + " " + price); return record; } public boolean delARecordByOrderNum(int orderNum) { for (Record record : records) { if (!record.isNotFound() && !record.isDelete() && record.getOrderNum() == orderNum) { record.setDelete(true); return true; } } System.out.println("delete error;"); return false; } public static boolean checkDateFormat(String tempDate) { if (tempDate.length() != 10 ){ return false; } for (int i = 0; i < 4; i++) { if (tempDate.charAt(i) > 57 || tempDate.charAt(i) < 48) { return false; } } if (tempDate.charAt(5) > 57 || tempDate.charAt(5) < 48 || tempDate.charAt(6) > 57 || tempDate.charAt(6) < 48) { return false; } if (tempDate.charAt(8) > 57 || tempDate.charAt(8) < 48 || tempDate.charAt(9) > 57 || tempDate.charAt(9) < 48) { return false; } if (tempDate.charAt(7) !=45 || tempDate.charAt(4) !=45) { return false; } if (tempDate.charAt(5) > 49 || (tempDate.charAt(5) == 49 && tempDate.charAt(6) > 50)) { return false; } if (tempDate.charAt(8) > 51 || (tempDate.charAt(8) == 51 && tempDate.charAt(9) > 49)) { return false; } return true; } public static boolean checkSpecialDate(int[] arrayTemp) { if (arrayTemp[1] == 2 && arrayTemp[2] > 29) { return false; } if (arrayTemp[1] < 8) { if (arrayTemp[1] % 2 == 0 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[1] > 7) { if (arrayTemp[1] % 2 == 1 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[0] % 4 != 0 || (arrayTemp[0] % 100 == 0 && arrayTemp[0] % 400 != 0)) { if (arrayTemp[1] == 2 && arrayTemp[2] > 28) {return false;} } return true; } } /* Record addARecord(int orderNum, String dishName, int portion, int num) { Dish dish = menu.searthDish(dishName); if (dish == null) { System.out.println(dishName + " does not exist"); return null; } Record record = new Record(orderNum, dish, portion, num); records.add(record); int price = record.getPrice(); System.out.println(record.getOrderNum() + " " + record.getD().getName() + " " + price); return record; }*/
本题在上一题的要求基础上对点菜计价问题更深一步研究,考虑各种可能存在的问题。本题我逐步分析其中可能具有的需求,一步步进行处理并加以解决。
解题思路:
代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。
程序最后按输入的先后顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。
每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。
折扣的计算方法(注:以下时间段均按闭区间计算):
周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。
周末全价,营业时间:9:30-21:30
菜品类:对应菜谱上一道菜的信息。
Dish {
String name;//菜品名称
int unit_price; //单价
int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) }
菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
Menu {
Dish\[\] dishs ;//菜品数组,保存所有菜品信息
Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。
Dish addDish(String dishName,int unit_price)//添加一道菜品信息
}
点菜记录类:保存订单上的一道菜品记录
Record {
int orderNum;//序号\\
Dish d;//菜品\\
int portion;//份额(1/2/3代表小/中/大份)\\
int getPrice()//计价,计算本条记录的价格\\
}
订单类:保存用户点的所有菜的信息。
Order {
Record\[\] records;//保存订单上每一道的记录
int getTotalPrice()//计算订单的总价
Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。
delARecordByOrderNum(int orderNum)//根据序号删除一条记录
findRecordByNum(int orderNum)//根据序号查找一条记录
}
类图如下:

以下为测试结果:
(2)有重复的数据:
代码如下:
import java.io.*; import java.util.*; public class Main { public static void main(String[] args){ Input1 get = new Input1(); Set<Integer> ssdas = new HashSet<>(); int n = get.nextInt(); for(int temp = 0; temp < n; temp++) { ssdas.add(get.nextInt()); } if (ssdas.size() != n) { System.out.printf("YES"); } else { System.out.print("NO"); } } public static boolean checkDateFormat(String tempDate) { if (tempDate.length() != 10 ){ return false; } for (int i = 0; i < 4; i++) { if (tempDate.charAt(i) > 57 || tempDate.charAt(i) < 48) { return false; } } if (tempDate.charAt(5) > 57 || tempDate.charAt(5) < 48 || tempDate.charAt(6) > 57 || tempDate.charAt(6) < 48) { return false; } if (tempDate.charAt(8) > 57 || tempDate.charAt(8) < 48 || tempDate.charAt(9) > 57 || tempDate.charAt(9) < 48) { return false; } if (tempDate.charAt(7) !=45 || tempDate.charAt(4) !=45) { return false; } if (tempDate.charAt(5) > 49 || (tempDate.charAt(5) == 49 && tempDate.charAt(6) > 50)) { return false; } if (tempDate.charAt(8) > 51 || (tempDate.charAt(8) == 51 && tempDate.charAt(9) > 49)) { return false; } return true; } public static boolean checkSpecialDate(int[] arrayTemp) { if (arrayTemp[1] == 2 && arrayTemp[2] > 29) { return false; } if (arrayTemp[1] < 8) { if (arrayTemp[1] % 2 == 0 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[1] > 7) { if (arrayTemp[1] % 2 == 1 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[0] % 4 != 0 || (arrayTemp[0] % 100 == 0 && arrayTemp[0] % 400 != 0)) { if (arrayTemp[1] == 2 && arrayTemp[2] > 28) {return false;} } return true; } } class Input1{ StringTokenizer tok1; BufferedReader buf1; public Input1() { buf1 = new BufferedReader(new InputStreamReader(System.in)); } boolean hasNext() { while(tok1 == null || !tok1.hasMoreElements()) { try { tok1 = new StringTokenizer(buf1.readLine()); } catch (Exception e) { return false; } } return true; } String next() { if(hasNext()) return tok1.nextToken(); return null; } int nextInt() { return Integer.parseInt(next()); } }
本题利用速读的方法,String类中的方法进行处理解决。以下为测试结果:
(3)去掉重复的数据:
代码如下:
import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int fdsaw = scan.nextInt(); int flag = 0; String str = scan.nextLine(); while(scan.hasNext()) { String strskfdjais = scan.nextLine(); str = str + strskfdjais; } String[] arr = str.split(" "); LinkedHashSet<String> list = new LinkedHashSet<String>(); for (int temp = 0; temp < arr.length; temp++) { list.add(arr[temp]); } for (String w : list) { if(flag == 0) { System.out.print(w); flag = 1; } else { System.out.print(" "+w); } } } public static boolean checkDateFormat(String tempDate) { if (tempDate.length() != 10 ){ return false; } for (int i = 0; i < 4; i++) { if (tempDate.charAt(i) > 57 || tempDate.charAt(i) < 48) { return false; } } if (tempDate.charAt(5) > 57 || tempDate.charAt(5) < 48 || tempDate.charAt(6) > 57 || tempDate.charAt(6) < 48) { return false; } if (tempDate.charAt(8) > 57 || tempDate.charAt(8) < 48 || tempDate.charAt(9) > 57 || tempDate.charAt(9) < 48) { return false; } if (tempDate.charAt(7) !=45 || tempDate.charAt(4) !=45) { return false; } if (tempDate.charAt(5) > 49 || (tempDate.charAt(5) == 49 && tempDate.charAt(6) > 50)) { return false; } if (tempDate.charAt(8) > 51 || (tempDate.charAt(8) == 51 && tempDate.charAt(9) > 49)) { return false; } return true; } public static boolean checkSpecialDate(int[] arrayTemp) { if (arrayTemp[1] == 2 && arrayTemp[2] > 29) { return false; } if (arrayTemp[1] < 8) { if (arrayTemp[1] % 2 == 0 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[1] > 7) { if (arrayTemp[1] % 2 == 1 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[0] % 4 != 0 || (arrayTemp[0] % 100 == 0 && arrayTemp[0] % 400 != 0)) { if (arrayTemp[1] == 2 && arrayTemp[2] > 28) {return false;} } return true; } }
本题方法同上,以下为测试结果:
(6)GPS测绘中度分秒转换:
代码如下:
import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int degree = scan.nextInt(); int minute = scan.nextInt(); double second = 0; String tempSecond = scan.next(); String[] getNumber = tempSecond.split("\\."); double[] number = new double[2]; number[0] = Integer.valueOf(getNumber[0]); number[1] = Integer.valueOf(getNumber[1]); second = number[0] + number[1] / Math.pow(10, getNumber[1].length()); double longitudeAndLatitude = (double) degree + (double) minute / 60.0 + second / 3600.0; System.out.printf("%d°%d′%s″ = %.6f", degree, minute, tempSecond, longitudeAndLatitude); } public static boolean checkDateFormat(String tempDate) { if (tempDate.length() != 10 ){ return false; } for (int i = 0; i < 4; i++) { if (tempDate.charAt(i) > 57 || tempDate.charAt(i) < 48) { return false; } } if (tempDate.charAt(5) > 57 || tempDate.charAt(5) < 48 || tempDate.charAt(6) > 57 || tempDate.charAt(6) < 48) { return false; } if (tempDate.charAt(8) > 57 || tempDate.charAt(8) < 48 || tempDate.charAt(9) > 57 || tempDate.charAt(9) < 48) { return false; } if (tempDate.charAt(7) !=45 || tempDate.charAt(4) !=45) { return false; } if (tempDate.charAt(5) > 49 || (tempDate.charAt(5) == 49 && tempDate.charAt(6) > 50)) { return false; } if (tempDate.charAt(8) > 51 || (tempDate.charAt(8) == 51 && tempDate.charAt(9) > 49)) { return false; } return true; } public static boolean checkSpecialDate(int[] arrayTemp) { if (arrayTemp[1] == 2 && arrayTemp[2] > 29) { return false; } if (arrayTemp[1] < 8) { if (arrayTemp[1] % 2 == 0 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[1] > 7) { if (arrayTemp[1] % 2 == 1 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[0] % 4 != 0 || (arrayTemp[0] % 100 == 0 && arrayTemp[0] % 400 != 0)) { if (arrayTemp[1] == 2 && arrayTemp[2] > 28) {return false;} } return true; } }
本题主要考察String类中的split方法和valueOf方法,以下为测试结果:
(7)判断两个日期的先后,计算间隔天数、周数:
代码如下:
import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String date1; String date2; int dateDifferentYear = 0; int date1TotalDay = 0; int date2TotalDay = 0; int dateDifferentSum = 0; int differentWeek = 0; date1 = scan.next(); System.out.print(""); date2 = scan.next(); String[] getDate1YearMonthDay = date1.split("-"); String[] getDate2YearMonthDay = date2.split("-"); int[] date1YearMonthDay = new int[3]; int[] date2YearMonthDay = new int[3]; date1YearMonthDay[0] = Integer.valueOf(getDate1YearMonthDay[0]); date1YearMonthDay[1] = Integer.valueOf(getDate1YearMonthDay[1]); date1YearMonthDay[2] = Integer.valueOf(getDate1YearMonthDay[2]); date2YearMonthDay[0] = Integer.valueOf(getDate2YearMonthDay[0]); date2YearMonthDay[1] = Integer.valueOf(getDate2YearMonthDay[1]); date2YearMonthDay[2] = Integer.valueOf(getDate2YearMonthDay[2]); if (date1YearMonthDay[0] < date2YearMonthDay[0]) { for (int tempYear = date1YearMonthDay[0]; tempYear < date2YearMonthDay[0]; tempYear++) { if ((tempYear % 4 == 0 && tempYear % 100 != 0) || tempYear % 400 == 0) { dateDifferentYear += 366; } else { dateDifferentYear += 365; } } } else if (date1YearMonthDay[0] > date2YearMonthDay[0]) { for (int tempYear = date2YearMonthDay[0]; tempYear < date1YearMonthDay[0]; tempYear++) { if ((tempYear % 4 == 0 && tempYear % 100 != 0) || tempYear % 400 == 0) { dateDifferentYear -= 366; } else { dateDifferentYear -= 365; } } } else { dateDifferentYear = 0; } if ((date1YearMonthDay[0] % 4 == 0 && date1YearMonthDay[0] % 100 != 0) || date1YearMonthDay[0] % 400 == 0) { switch (date1YearMonthDay[1]) { case 1: date1TotalDay = date1YearMonthDay[2]; break; case 2: date1TotalDay = date1YearMonthDay[2] + 31; break; case 3: date1TotalDay = date1YearMonthDay[2] + 60; break; case 4: date1TotalDay = date1YearMonthDay[2] + 91; break; case 5: date1TotalDay = date1YearMonthDay[2] + 121; break; case 6: date1TotalDay = date1YearMonthDay[2] + 152; break; case 7: date1TotalDay = date1YearMonthDay[2] + 182; break; case 8: date1TotalDay = date1YearMonthDay[2] + 213; break; case 9: date1TotalDay = date1YearMonthDay[2] + 244; break; case 10: date1TotalDay = date1YearMonthDay[2] + 274; break; case 11: date1TotalDay = date1YearMonthDay[2] + 305; break; case 12: date1TotalDay = date1YearMonthDay[2] + 335; break; } } else { switch (date1YearMonthDay[1]) { case 1: date1TotalDay = date1YearMonthDay[2]; break; case 2: date1TotalDay = date1YearMonthDay[2] + 31; break; case 3: date1TotalDay = date1YearMonthDay[2] + 59; break; case 4: date1TotalDay = date1YearMonthDay[2] + 90; break; case 5: date1TotalDay = date1YearMonthDay[2] + 120; break; case 6: date1TotalDay = date1YearMonthDay[2] + 151; break; case 7: date1TotalDay = date1YearMonthDay[2] + 181; break; case 8: date1TotalDay = date1YearMonthDay[2] + 212; break; case 9: date1TotalDay = date1YearMonthDay[2] + 243; break; case 10: date1TotalDay = date1YearMonthDay[2] + 273; break; case 11: date1TotalDay = date1YearMonthDay[2] + 304; break; case 12: date1TotalDay = date1YearMonthDay[2] + 334; break; } } if ((date2YearMonthDay[0] % 4 == 0 && date2YearMonthDay[0] % 100 != 0) || date2YearMonthDay[0] % 400 == 0) { switch (date2YearMonthDay[1]) { case 1: date2TotalDay = date2YearMonthDay[2]; break; case 2: date2TotalDay = date2YearMonthDay[2] + 31; break; case 3: date2TotalDay = date2YearMonthDay[2] + 60; break; case 4: date2TotalDay = date2YearMonthDay[2] + 91; break; case 5: date2TotalDay = date2YearMonthDay[2] + 121; break; case 6: date2TotalDay = date2YearMonthDay[2] + 152; break; case 7: date2TotalDay = date2YearMonthDay[2] + 182; break; case 8: date2TotalDay = date2YearMonthDay[2] + 213; break; case 9: date2TotalDay = date2YearMonthDay[2] + 244; break; case 10: date2TotalDay = date2YearMonthDay[2] + 274; break; case 11: date2TotalDay = date2YearMonthDay[2] + 305; break; case 12: date2TotalDay = date2YearMonthDay[2] + 335; break; } } else { switch (date2YearMonthDay[1]) { case 1: date2TotalDay = date2YearMonthDay[2]; break; case 2: date2TotalDay = date2YearMonthDay[2] + 31; break; case 3: date2TotalDay = date2YearMonthDay[2] + 59; break; case 4: date2TotalDay = date2YearMonthDay[2] + 90; break; case 5: date2TotalDay = date2YearMonthDay[2] + 120; break; case 6: date2TotalDay = date2YearMonthDay[2] + 151; break; case 7: date2TotalDay = date2YearMonthDay[2] + 181; break; case 8: date2TotalDay = date2YearMonthDay[2] + 212; break; case 9: date2TotalDay = date2YearMonthDay[2] + 243; break; case 10: date2TotalDay = date2YearMonthDay[2] + 273; break; case 11: date2TotalDay = date2YearMonthDay[2] + 304; break; case 12: date2TotalDay = date2YearMonthDay[2] + 334; break; } } dateDifferentSum = dateDifferentYear + date2TotalDay - date1TotalDay; if (dateDifferentSum > 0) { differentWeek = dateDifferentSum / 7; System.out.println("第一个日期比第二个日期更早"); System.out.println("两个日期间隔"+dateDifferentSum+"天"); System.out.print("两个日期间隔"+differentWeek+"周"); } if (dateDifferentSum < 0) { dateDifferentSum = -dateDifferentSum; differentWeek = dateDifferentSum / 7; System.out.println("第一个日期比第二个日期更晚"); System.out.println("两个日期间隔"+dateDifferentSum+"天"); System.out.print("两个日期间隔"+differentWeek+"周"); } } public static boolean checkDateFormat(String tempDate) { if (tempDate.length() != 10 ){ return false; } for (int i = 0; i < 4; i++) { if (tempDate.charAt(i) > 57 || tempDate.charAt(i) < 48) { return false; } } if (tempDate.charAt(5) > 57 || tempDate.charAt(5) < 48 || tempDate.charAt(6) > 57 || tempDate.charAt(6) < 48) { return false; } if (tempDate.charAt(8) > 57 || tempDate.charAt(8) < 48 || tempDate.charAt(9) > 57 || tempDate.charAt(9) < 48) { return false; } if (tempDate.charAt(7) !=45 || tempDate.charAt(4) !=45) { return false; } if (tempDate.charAt(5) > 49 || (tempDate.charAt(5) == 49 && tempDate.charAt(6) > 50)) { return false; } if (tempDate.charAt(8) > 51 || (tempDate.charAt(8) == 51 && tempDate.charAt(9) > 49)) { return false; } return true; } public static boolean checkSpecialDate(int[] arrayTemp) { if (arrayTemp[1] == 2 && arrayTemp[2] > 29) { return false; } if (arrayTemp[1] < 8) { if (arrayTemp[1] % 2 == 0 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[1] > 7) { if (arrayTemp[1] % 2 == 1 && arrayTemp[2] > 30) {return false;} } if (arrayTemp[0] % 4 != 0 || (arrayTemp[0] % 100 == 0 && arrayTemp[0] % 400 != 0)) { if (arrayTemp[1] == 2 && arrayTemp[2] > 28) {return false;} } return true; } }
以下为测试结果:
总结:题目集1-3中涉及到了java中较为基础的语法知识,选择判断、循环语法知识,面向对象编程中与面向过程不同的特殊语法:类与对象对其基本使用能力进行考察;本题目集考察我们所学的知识点较为全面,内容组织循序渐进,通过数学,经济,游戏等应用领域的生动案例来引导我们的程序设计的学习,避免了单纯语法学习的枯燥,也让我们可以学以致用;其中的部分题目具有一定的思维难度,能考察我们对较为复杂的需求的处理方法,为之后的程序员工作中可能遇到的一些特殊情况进行预演。总而言之,这些题目对我们的代码水平要求逐渐提升,让我们的编程思维一步步变得更完善。