BLOG-2
- 前言
- 知识点:期中考试主要考察了,继承,抽象类,多态,Comparable接口
- 题量 :还可以
- 难度:期中考试很简单但是菜单题目很难
- 设计与分析
- 菜单4
- 类图






2.源代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 类名称 对象数组名[] = new 类名称[长度];
Dish d[] = new Dish[2];
d[0] = new Dish("麻婆豆腐", 12);
d[1] = new Dish("油淋生菜", 9);
//error: d[0].name="麻婆豆腐";d[0].unit_price=12;
// d[1].name="油淋生菜";d[1].unit_price=9;
//text: System.out.println(d[1].name+" "+d[1].unit_price);
Scanner scanner = new Scanner(System.in);
String useless = scanner.nextLine();
int din = 0;
int tab = 0;
String[] re = useless.split(" ", 2);
int pricc = Integer.valueOf(re[1]);
if (pricc >= 400)
System.out.println(re[0] + " price out of range 400");
else {
d[din] = new Dish(re[0], pricc);
din++;
}
useless = scanner.nextLine();
String[] re1 = useless.split(" ", 3);
pricc = Integer.valueOf(re1[1]);
if (pricc >= 400)
System.out.println(re1[0] + " price out of range 400");
else {
d[din] = new Dish(re1[0], pricc);
din++;
}
String a = scanner.nextLine();// ***
while (a.indexOf("end") == -1) {
String[] result1 = a.split(" ", 4);
if (a.indexOf("table") == -1) {
System.out.println("wrong format");
return;
}
String[] time = result1[3].split("/");
int hour = Integer.valueOf(time[0]);
int min = Integer.valueOf(time[1]);
int se = Integer.valueOf(time[2]);
String[] time1 = result1[2].split("/");
int year = Integer.valueOf(time1[0]);
int month = Integer.valueOf(time1[1]);
int day = Integer.valueOf(time1[2]);
double sale = 0;
int dayy = 0;
// String myString = "hello123"; //创建一个字符串变量myString
char[] my; // 创建一个字符数组myCharArray
my = result1[1].toCharArray(); // 将字符串变量转换为字符数组
// for(int i=0; i<myCharArray.length; i++) {
// System.out.print(myCharArray[i] + " "); //将每个字符打印出来
// }
if (my[0] >= 'a' && my[0] <= 'z') {
System.out.println("wrong format");
return;
}
if (my[0] >= 'A' && my[0] <= 'Z') {
System.out.println("wrong format");
return;
}
int number = Integer.valueOf(result1[1]);
if (result1[3].length() != 8 || number > 55 || number <= 0) {
System.out.println("wrong format");
return;
}
if (judge(year) == 0) {
System.out.println("not a valid time period");
return;
}
// if (Prt(year, month, dayy) >= 1 && Prt(year, month, dayy) <= 5)
// sale = onsale(hour * 3600 + min * 60 + se);
// else
sale = 1.00;
System.out.println(result1[0] + " " + result1[1] + ": ");
a = scanner.nextLine();
int i = 0;
int s = 0;
Order o[] = new Order[1000];
int p1 = 0;
// Order p1 = new Order();
int p = 0;
int[] cc = new int[100];
int cindex = 0;
while (a.indexOf("end") == -1 && a.indexOf("table") == -1) {
if (a.indexOf("delete") != -1) {
int x = Integer.valueOf(a.substring(0, 1));
cc[cindex] = x;
cindex++;
int flag = 0;
if (x == 0) {
System.out.println("niho");
}
for (int db = 0; db < cindex - 1; db++) {
if (cc[db] == x) {
System.out.println("deduplication " + x);
flag = 1;
break;
}
}
if (x > i)
System.out.println("delete error;");
if (x <= i && flag == 0)
s = s - o[x - 1].price;
} else {
String[] result = a.split(" ", 4);
int j;
for (j = 0; j < din; j++) {
if (a.indexOf(d[j].name) != -1) {
p = d[j].unit_price;
break;
}
}
if (j == din)
System.out.println(result[1] + " does not exist");
else {
int n = Integer.valueOf(result[2]);
int m = Integer.valueOf(result[3]);
int juu = 0;
if (n == 1)
p1 = p * m;
if (n == 2) {
double loss = p * 1.5 * m;
// double loss = p*1.5*m;
p1 = (int) (loss + 0.5);
}
if (n == 3)
p1 = p * 2 * m;
o[i] = new Order(result[1], p1);
if (n >= 4) {
System.out.println(n + " portion out of range 4");
juu = 1;
}
if (m > 15) {
System.out.println(result[0] + " num out of range 16");
juu = 1;
}
if (juu == 0) {
s = s + p1;
System.out.println(result[0] + " " + result[1] + " " + p1);
}
i++;
}
}
a = scanner.nextLine();
if (a.indexOf("table") != -1) {
tab = 1;
break;
}
}
if (sale != 0) {
System.out.print(result1[0] + " " + result1[1] + ": ");
System.out.print(s + " ");
double sxd = s * sale * 1.0;
if (Prt(year, month, dayy) >= 1 && Prt(year, month, dayy) <= 5)
sxd = sxd * 0.7 + 0.5;
s = (int) sxd;
System.out.println(s);
} else
System.out.println(result1[0] + " " + result1[1] + " out of opening hours");
}
}
private static int judge(int year) {
// TODO Auto-generated method stub
if (year < 2022 || year > 2023)
return 0;
return 1;
}
public static int Prt(int year, int month, int day) {
int[] x = new int[12];
x[0] = 31;
x[1] = 28;
x[2] = 31;
x[3] = 30;
x[4] = 31;
x[5] = 30;
x[6] = 31;
x[7] = 31;
x[8] = 30;
x[9] = 31;
x[10] = 30;
x[11] = 31;
if (LeapYear(year) == 1)
x[1] = 29;
int s1 = 0;
for (int i = 0; i < month - 1; i++)
s1 = s1 + x[i];
s1 = s1 + day;
int c;
// c = [Y-1] + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D
c = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + s1) % 7;
// Y是年份数,D是这一天在这一年中的累积天数,也就是这一天在这一年中是第几天。
if (c == 0)
c = 7;
return c;
}
public static int LeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0)
return 1;
if (year % 400 == 0)
return 1;
return 0;
}
public static double onsale(int x) {
if (x >= 61200 && x <= 73800)
return 0.8;
if (x >= 37800 && x <= 52200)
return 0.6;
return 0;
}
}
class Dish {
String name;
int unit_price;
public Dish() {
}
public Dish(String name, int unit_price) {
this.name = name;
this.unit_price = unit_price;
}
}
class Order {
String name;
int price;
// public Order{
// }
public Order(String name, int price) {
this.name = name;
this.price = price;
}
}
class Table {
Order order;
int num;
int year;
int month;
int day;
int hour;
int min;
int sec;
int week;
public Table(){
}
public boolean check(){
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month >= 1 && month <= 12) {
//判断是否为闰年
if ((year % 100 == 0 && year % 400 == 0) || year % 4 == 0) {
if (month == 2 && day <= 29) return true;
else {
if (day <= days[month - 1]) return true;
}
} else {
if (day <= days[month - 1])
return true;
}
}
return false;
}
boolean checkorder(int x){
class Order {
Record[] records = new Record[100];//保存订单上每一道的记录
int i=0,s=0;//s为点菜数
int year;
int month;
int day;
public Order(){
}
int getTotalPrice(){
int num = 0;
for(i=0;i<s;i++){
if(records[i].life==0)
num = num + records[i].getPrice();
}
return num;
}//计算订单的总价
int ygetTotalPrice(){
int num = 0;
for(i=0;i<s;i++){
if(records[i].life==0)
num = num + records[i].ygetPrice();
}
return num;
}//计算订单的原总价
public Record addARecord(int orderNum,String dishName,int portion,int num,Menu menu,int week,String name ,String tel){
for (i=0;i<s;i++){
if(dishName==records[i].d.name&&portion==records[i].portion) {
records[i].num = records[i].num + num;
s++;
return records[i];
}
}
if(i==s) {
records[i] = new Record();
if(menu.searthDish(dishName)!=null) {
records[i].d = menu.searthDish(dishName);
records[i].orderNum = orderNum;
records[i].d.name = dishName;
records[i].portion = portion;
records[i].num = num;
records[i].week = week;
records[i].name = name;
records[i].tel = tel;
s++;
}
else {
records[i].d = new Dish();
records[i].d.name=dishName;
records[i].life=6;//订单的菜不存在
s++;
}
return records[i];
}
return null;
}//添加一条菜品信息到订单中。
public void delARecordByOrderNum(int orderNum){
for (i=0;i<s;i++){
if(records[i].orderNum==orderNum){
if(records[i].life==1)
records[i].life = 2;
else
records[i].life = 1;
}
}
}//根据序号删除一条记录
public int findRecordByNum(int orderNum){
for (i=0;i<s;i++){
if (records[i].orderNum==orderNum)
return i;
}
return -1;
}//根据序号查找一条记录
}
for(int j=0;j<=order.s-1;j++){
if(x<=order.records[j].orderNum)
return false;
}
return true;
}
}
-
- 菜单5
- 类图






2.代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 类名称 对象数组名[] = new 类名称[长度];
Dish d[] = new Dish[2];
d[0] = new Dish("麻婆豆腐", 12);
d[1] = new Dish("油淋生菜", 9);
//error: d[0].name="麻婆豆腐";d[0].unit_price=12;
// d[1].name="油淋生菜";d[1].unit_price=9;
//text: System.out.println(d[1].name+" "+d[1].unit_price);
Scanner scanner = new Scanner(System.in);
String useless = scanner.nextLine();
int din = 0;
int tab = 0;
String[] re = useless.split(" ", 2);
int pricc = Integer.valueOf(re[1]);
if (pricc >= 400)
System.out.println(re[0] + " price out of range 400");
else {
d[din] = new Dish(re[0], pricc);
din++;
}
useless = scanner.nextLine();
String[] re1 = useless.split(" ", 3);
pricc = Integer.valueOf(re1[1]);
if (pricc >= 400)
System.out.println(re1[0] + " price out of range 400");
else {
d[din] = new Dish(re1[0], pricc);
din++;
}
String a = scanner.nextLine();// ***
while (a.indexOf("end") == -1) {
String[] result1 = a.split(" ", 4);
if (a.indexOf("table") == -1) {
System.out.println("wrong format");
return;
}
String[] time = result1[3].split("/");
int hour = Integer.valueOf(time[0]);
int min = Integer.valueOf(time[1]);
int se = Integer.valueOf(time[2]);
String[] time1 = result1[2].split("/");
int year = Integer.valueOf(time1[0]);
int month = Integer.valueOf(time1[1]);
int day = Integer.valueOf(time1[2]);
double sale = 0;
int dayy = 0;
// String myString = "hello123"; //创建一个字符串变量myString
char[] my; // 创建一个字符数组myCharArray
my = result1[1].toCharArray(); // 将字符串变量转换为字符数组
// for(int i=0; i<myCharArray.length; i++) {
// System.out.print(myCharArray[i] + " "); //将每个字符打印出来
// }
if (my[0] >= 'a' && my[0] <= 'z') {
System.out.println("wrong format");
return;
}
if (my[0] >= 'A' && my[0] <= 'Z') {
System.out.println("wrong format");
return;
}
int number = Integer.valueOf(result1[1]);
if (result1[3].length() != 8 || number > 55 || number <= 0) {
System.out.println("wrong format");
return;
}
if (judge(year) == 0) {
System.out.println("not a valid time period");
return;
}
// if (Prt(year, month, dayy) >= 1 && Prt(year, month, dayy) <= 5)
// sale = onsale(hour * 3600 + min * 60 + se);
// else
sale = 1.00;
System.out.println(result1[0] + " " + result1[1] + ": ");
a = scanner.nextLine();
int i = 0;
int s = 0;
Order o[] = new Order[1000];
int p1 = 0;
// Order p1 = new Order();
int p = 0;
int[] cc = new int[100];
int cindex = 0;
while (a.indexOf("end") == -1 && a.indexOf("table") == -1) {
if (a.indexOf("delete") != -1) {
int x = Integer.valueOf(a.substring(0, 1));
cc[cindex] = x;
cindex++;
int flag = 0;
if (x == 0) {
System.out.println("niho");
}
for (int db = 0; db < cindex - 1; db++) {
if (cc[db] == x) {
System.out.println("deduplication " + x);
flag = 1;
break;
}
}
if (x > i)
System.out.println("delete error;");
if (x <= i && flag == 0)
s = s - o[x - 1].price;
} else {
String[] result = a.split(" ", 4);
int j;
for (j = 0; j < din; j++) {
if (a.indexOf(d[j].name) != -1) {
p = d[j].unit_price;
break;
}
}
if (j == din)
System.out.println(result[1] + " does not exist");
else {
int n = Integer.valueOf(result[2]);
int m = Integer.valueOf(result[3]);
int juu = 0;
if (n == 1)
p1 = p * m;
if (n == 2) {
double loss = p * 1.5 * m;
// double loss = p*1.5*m;
p1 = (int) (loss + 0.5);
}
if (n == 3)
p1 = p * 2 * m;
o[i] = new Order(result[1], p1);
if (n >= 4) {
System.out.println(n + " portion out of range 4");
juu = 1;
}
if (m > 15) {
System.out.println(result[0] + " num out of range 16");
juu = 1;
}
if (juu == 0) {
s = s + p1;
System.out.println(result[0] + " " + result[1] + " " + p1);
}
i++;
}
}
a = scanner.nextLine();
if (a.indexOf("table") != -1) {
tab = 1;
break;
}
}
if (sale != 0) {
System.out.print(result1[0] + " " + result1[1] + ": ");
System.out.print(s + " ");
double sxd = s * sale * 1.0;
if (Prt(year, month, dayy) >= 1 && Prt(year, month, dayy) <= 5)
sxd = sxd * 0.7 + 0.5;
s = (int) sxd;
System.out.println(s);
} else
System.out.println(result1[0] + " " + result1[1] + " out of opening hours");
}
}
private static int judge(int year) {
// TODO Auto-generated method stub
if (year < 2022 || year > 2023)
return 0;
return 1;
}
public static int Prt(int year, int month, int day) {
int[] x = new int[12];
x[0] = 31;
x[1] = 28;
x[2] = 31;
x[3] = 30;
x[4] = 31;
x[5] = 30;
x[6] = 31;
x[7] = 31;
x[8] = 30;
x[9] = 31;
x[10] = 30;
x[11] = 31;
if (LeapYear(year) == 1)
x[1] = 29;
int s1 = 0;
for (int i = 0; i < month - 1; i++)
s1 = s1 + x[i];
s1 = s1 + day;
int c;
// c = [Y-1] + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D
c = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + s1) % 7;
// Y是年份数,D是这一天在这一年中的累积天数,也就是这一天在这一年中是第几天。
if (c == 0)
c = 7;
return c;
}
public static int LeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0)
return 1;
if (year % 400 == 0)
return 1;
return 0;
}
public static double onsale(int x) {
if (x >= 61200 && x <= 73800)
return 0.8;
if (x >= 37800 && x <= 52200)
return 0.6;
return 0;
}
}
class Dish {
String name;
int unit_price;
public Dish() {
}
public Dish(String name, int unit_price) {
this.name = name;
this.unit_price = unit_price;
}
}
class Order {
String name;
int price;
// public Order{
// }
public Order(String name, int price) {
this.name = name;
this.price = price;
}
}
class Table {
Order order;
int num;
int year;
int month;
int day;
int hour;
int min;
int sec;
int week;
public Table(){
}
public boolean check(){
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month >= 1 && month <= 12) {
//判断是否为闰年
if ((year % 100 == 0 && year % 400 == 0) || year % 4 == 0) {
if (month == 2 && day <= 29) return true;
else {
if (day <= days[month - 1]) return true;
}
} else {
if (day <= days[month - 1])
return true;
}
}
return false;
}
boolean checkorder(int x){
class Order {
Record[] records = new Record[100];//保存订单上每一道的记录
int i=0,s=0;//s为点菜数
int year;
int month;
int day;
public Order(){
}
int getTotalPrice(){
int num = 0;
for(i=0;i<s;i++){
if(records[i].life==0)
num = num + records[i].getPrice();
}
return num;
}//计算订单的总价
int ygetTotalPrice(){
int num = 0;
for(i=0;i<s;i++){
if(records[i].life==0)
num = num + records[i].ygetPrice();
}
return num;
}//计算订单的原总价
public Record addARecord(int orderNum,String dishName,int portion,int num,Menu menu,int week,String name ,String tel){
for (i=0;i<s;i++){
if(dishName==records[i].d.name&&portion==records[i].portion) {
records[i].num = records[i].num + num;
s++;
return records[i];
}
}
if(i==s) {
records[i] = new Record();
if(menu.searthDish(dishName)!=null) {
records[i].d = menu.searthDish(dishName);
records[i].orderNum = orderNum;
records[i].d.name = dishName;
records[i].portion = portion;
records[i].num = num;
records[i].week = week;
records[i].name = name;
records[i].tel = tel;
s++;
}
else {
records[i].d = new Dish();
records[i].d.name=dishName;
records[i].life=6;//订单的菜不存在
s++;
}
return records[i];
}
return null;
}//添加一条菜品信息到订单中。
public void delARecordByOrderNum(int orderNum){
for (i=0;i<s;i++){
if(records[i].orderNum==orderNum){
if(records[i].life==1)
records[i].life = 2;
else
records[i].life = 1;
}
}
}//根据序号删除一条记录
public int findRecordByNum(int orderNum){
for (i=0;i<s;i++){
if (records[i].orderNum==orderNum)
return i;
}
return -1;
}//根据序号查找一条记录
}
for(int j=0;j<=order.s-1;j++){
if(x<=order.records[j].orderNum)
return false;
}
return true;
}
}
-
- 菜单6
1.类图






2.代码
import java.util.Scanner; public class Main { public static void main(String[] args) { // 类名称 对象数组名[] = new 类名称[长度]; Dish d[] = new Dish[2]; d[0] = new Dish("麻婆豆腐", 12); d[1] = new Dish("油淋生菜", 9); //error: d[0].name="麻婆豆腐";d[0].unit_price=12; // d[1].name="油淋生菜";d[1].unit_price=9; //text: System.out.println(d[1].name+" "+d[1].unit_price); Scanner scanner = new Scanner(System.in); String useless = scanner.nextLine(); int din = 0; int tab = 0; String[] re = useless.split(" ", 2); int pricc = Integer.valueOf(re[1]); if (pricc >= 400) System.out.println(re[0] + " price out of range 400"); else { d[din] = new Dish(re[0], pricc); din++; } useless = scanner.nextLine(); String[] re1 = useless.split(" ", 3); pricc = Integer.valueOf(re1[1]); if (pricc >= 400) System.out.println(re1[0] + " price out of range 400"); else { d[din] = new Dish(re1[0], pricc); din++; } String a = scanner.nextLine();// *** while (a.indexOf("end") == -1) { String[] result1 = a.split(" ", 4); if (a.indexOf("table") == -1) { System.out.println("wrong format"); return; } String[] time = result1[3].split("/"); int hour = Integer.valueOf(time[0]); int min = Integer.valueOf(time[1]); int se = Integer.valueOf(time[2]); String[] time1 = result1[2].split("/"); int year = Integer.valueOf(time1[0]); int month = Integer.valueOf(time1[1]); int day = Integer.valueOf(time1[2]); double sale = 0; int dayy = 0; // String myString = "hello123"; //创建一个字符串变量myString char[] my; // 创建一个字符数组myCharArray my = result1[1].toCharArray(); // 将字符串变量转换为字符数组 // for(int i=0; i<myCharArray.length; i++) { // System.out.print(myCharArray[i] + " "); //将每个字符打印出来 // } if (my[0] >= 'a' && my[0] <= 'z') { System.out.println("wrong format"); return; } if (my[0] >= 'A' && my[0] <= 'Z') { System.out.println("wrong format"); return; } int number = Integer.valueOf(result1[1]); if (result1[3].length() != 8 || number > 55 || number <= 0) { System.out.println("wrong format"); return; } if (judge(year) == 0) { System.out.println("not a valid time period"); return; } // if (Prt(year, month, dayy) >= 1 && Prt(year, month, dayy) <= 5) // sale = onsale(hour * 3600 + min * 60 + se); // else sale = 1.00; System.out.println(result1[0] + " " + result1[1] + ": "); a = scanner.nextLine(); int i = 0; int s = 0; Order o[] = new Order[1000]; int p1 = 0; // Order p1 = new Order(); int p = 0; int[] cc = new int[100]; int cindex = 0; while (a.indexOf("end") == -1 && a.indexOf("table") == -1) { if (a.indexOf("delete") != -1) { int x = Integer.valueOf(a.substring(0, 1)); cc[cindex] = x; cindex++; int flag = 0; if (x == 0) { System.out.println("niho"); } for (int db = 0; db < cindex - 1; db++) { if (cc[db] == x) { System.out.println("deduplication " + x); flag = 1; break; } } if (x > i) System.out.println("delete error;"); if (x <= i && flag == 0) s = s - o[x - 1].price; } else { String[] result = a.split(" ", 4); int j; for (j = 0; j < din; j++) { if (a.indexOf(d[j].name) != -1) { p = d[j].unit_price; break; } } if (j == din) System.out.println(result[1] + " does not exist"); else { int n = Integer.valueOf(result[2]); int m = Integer.valueOf(result[3]); int juu = 0; if (n == 1) p1 = p * m; if (n == 2) { double loss = p * 1.5 * m; // double loss = p*1.5*m; p1 = (int) (loss + 0.5); } if (n == 3) p1 = p * 2 * m; o[i] = new Order(result[1], p1); if (n >= 4) { System.out.println(n + " portion out of range 4"); juu = 1; } if (m > 15) { System.out.println(result[0] + " num out of range 16"); juu = 1; } if (juu == 0) { s = s + p1; System.out.println(result[0] + " " + result[1] + " " + p1); } i++; } } a = scanner.nextLine(); if (a.indexOf("table") != -1) { tab = 1; break; } } if (sale != 0) { System.out.print(result1[0] + " " + result1[1] + ": "); System.out.print(s + " "); double sxd = s * sale * 1.0; if (Prt(year, month, dayy) >= 1 && Prt(year, month, dayy) <= 5) sxd = sxd * 0.7 + 0.5; s = (int) sxd; System.out.println(s); } else System.out.println(result1[0] + " " + result1[1] + " out of opening hours"); } } private static int judge(int year) { // TODO Auto-generated method stub if (year < 2022 || year > 2023) return 0; return 1; } public static int Prt(int year, int month, int day) { int[] x = new int[12]; x[0] = 31; x[1] = 28; x[2] = 31; x[3] = 30; x[4] = 31; x[5] = 30; x[6] = 31; x[7] = 31; x[8] = 30; x[9] = 31; x[10] = 30; x[11] = 31; if (LeapYear(year) == 1) x[1] = 29; int s1 = 0; for (int i = 0; i < month - 1; i++) s1 = s1 + x[i]; s1 = s1 + day; int c; // c = [Y-1] + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D c = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + s1) % 7; // Y是年份数,D是这一天在这一年中的累积天数,也就是这一天在这一年中是第几天。 if (c == 0) c = 7; return c; } public static int LeapYear(int year) { if (year % 4 == 0 && year % 100 != 0) return 1; if (year % 400 == 0) return 1; return 0; } public static double onsale(int x) { if (x >= 61200 && x <= 73800) return 0.8; if (x >= 37800 && x <= 52200) return 0.6; return 0; } } class Dish { String name; int unit_price; public Dish() { } public Dish(String name, int unit_price) { this.name = name; this.unit_price = unit_price; } } class Order { String name; int price; // public Order{ // } public Order(String name, int price) { this.name = name; this.price = price; } } class Table { Order order; int num; int year; int month; int day; int hour; int min; int sec; int week; public Table(){ } public boolean check(){ int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month >= 1 && month <= 12) { //判断是否为闰年 if ((year % 100 == 0 && year % 400 == 0) || year % 4 == 0) { if (month == 2 && day <= 29) return true; else { if (day <= days[month - 1]) return true; } } else { if (day <= days[month - 1]) return true; } } return false; } boolean checkorder(int x){ class Order { Record[] records = new Record[100];//保存订单上每一道的记录 int i=0,s=0;//s为点菜数 int year; int month; int day; public Order(){ } int getTotalPrice(){ int num = 0; for(i=0;i<s;i++){ if(records[i].life==0) num = num + records[i].getPrice(); } return num; }//计算订单的总价 int ygetTotalPrice(){ int num = 0; for(i=0;i<s;i++){ if(records[i].life==0) num = num + records[i].ygetPrice(); } return num; }//计算订单的原总价 public Record addARecord(int orderNum,String dishName,int portion,int num,Menu menu,int week,String name ,String tel){ for (i=0;i<s;i++){ if(dishName==records[i].d.name&&portion==records[i].portion) { records[i].num = records[i].num + num; s++; return records[i]; } } if(i==s) { records[i] = new Record(); if(menu.searthDish(dishName)!=null) { records[i].d = menu.searthDish(dishName); records[i].orderNum = orderNum; records[i].d.name = dishName; records[i].portion = portion; records[i].num = num; records[i].week = week; records[i].name = name; records[i].tel = tel; s++; } else { records[i].d = new Dish(); records[i].d.name=dishName; records[i].life=6;//订单的菜不存在 s++; } return records[i]; } return null; }//添加一条菜品信息到订单中。 public void delARecordByOrderNum(int orderNum){ for (i=0;i<s;i++){ if(records[i].orderNum==orderNum){ if(records[i].life==1) records[i].life = 2; else records[i].life = 1; } } }//根据序号删除一条记录 public int findRecordByNum(int orderNum){ for (i=0;i<s;i++){ if (records[i].orderNum==orderNum) return i; } return -1; }//根据序号查找一条记录 } for(int j=0;j<=order.s-1;j++){ if(x<=order.records[j].orderNum) return false; } return true; } }
-
- 期中考试题目
1.圆类
import java.util.Scanner; public class Circle { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); double r; r = in.nextFloat(); double s = 0; if (r <= 0) { System.out.println("Wrong Format"); return; } else s = r * r * 3.1415926f; if (s == 0) System.out.println("0.00"); else System.out.println(String.format("%.2f", s)); } }
2.矩形类
import java.util.Scanner; public class Jz { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); double x1 = in.nextDouble(); double y1 = in.nextDouble(); double x2 = in.nextDouble(); double y2 = in.nextDouble(); double x; double y; x = x1 - x2; if(x<0) x = -1 * x; y = y1 - y2; if(y<0) y = -1* y; double s; s = x * y; System.out.println(String.format("%.2f", s)); } }
3.第三题
import java.util.Scanner; public class Jc { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int choice = in.nextInt(); if(choice == 1) { double r; r = in.nextFloat(); double s = 0; if (r <= 0) { System.out.println("Wrong Format"); return; } else s = r * r * 3.1415926f; if (s == 0) System.out.println("0.00"); else System.out.println(String.format("%.2f", s)); } if(choice == 2) { double x1 = in.nextDouble(); double y1 = in.nextDouble(); double x2 = in.nextDouble(); double y2 = in.nextDouble(); double x; double y; x = x1 - x2; if(x<0) x = -1 * x; y = y1 - y2; if(y<0) y = -1* y; double s; s = x * y; System.out.println(String.format("%.2f", s)); } return; } }
4.第四题
import java.util.Scanner; public class Last { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); Double[] s = new Double[100]; int k = 0; int choice; choice = in.nextInt(); while (choice != 0) { if (choice == 1) { double r; r = in.nextFloat(); if (r <= 0) { System.out.println("Wrong Format"); break; } else { s[k] = r * r * 3.1415926f; k++; } // if (s == 0) // System.out.println("0.00"); // else // System.out.println(String.format("%.2f", s)); } if (choice == 2) { double x1 = in.nextDouble(); double y1 = in.nextDouble(); double x2 = in.nextDouble(); double y2 = in.nextDouble(); double x; double y; x = x1 - x2; if (x < 0) x = -1 * x; y = y1 - y2; if (y < 0) y = -1 * y; s[k] = x * y; k++; // System.out.println(String.format("%.2f", s)); } choice = in.nextInt(); } int i,j; for(i = 0 ; i < k-1 ;i++) { for(j = 0 ;j<k-1-i;j++) { if(s[j]>s[j+1]) { double t; t = s[j]; s[j] = s[j+1]; s[j+1] = t; } } } for(i=0;i<k;i++) { System.out.print(String.format("%.2f", s[i])); System.out.print(" "); } System.out.print("\n"); } }
- 踩坑心得
- 期中考试的踩坑:
1.考试时忘记了Math类中表示Π的方式,然后只能背到3.1415926,这样的话会有有一个测试点过不了,必须还要再往后面背一两位才行
-
- 菜单中的踩坑:
先看一下图片就知道踩了多少坑了


1.没有对字符串做解析,这个问题是我后面(菜单四)才意识到的,导致代码很混乱,很没用条理,而且没有办法通过一些测试点(比如混乱输入之类的),我写的就是一个大循环读到end结束下套个小循环,小循环读到没有Table结束,出来就是读点菜,读到有delet就进行删除操作,但是这么写的话有一个以上的Table就会很难用
2.折扣的计算十分混乱,我一开始用的方法是这样的,给一个乘以折扣(或者中份乘以1.5)的数据+0.5,再对他强制类型转换,这样小数位大于等于5的就会进一,反之则不进(但是这样写很麻烦不说,我菜单一还因为这么写过不了一个测试点),现在回头看看,为什么当初我不用Math.round()....
3.对甜辣度菜系无法处理
- 主要困难以及改进建议
- 困难:期中考试很顺利,我只花了半个小时就做完了满分胶卷了。主要的困难就是来自于菜单,菜单要一口气写出一个体系,可以过所有测试点的体系,但是不是所有测试点都是已知,而且对输入数据要求十分苛刻(有很多测试点都是特殊例子或者不按规定的输入)导致要考虑的特殊情形过多,并且菜单在PTA结束后也没有标准题解或者讲解,导致后面菜单无从下手,尤其是加入了菜系,后面两个分支无论是哪一个都感觉做起来太难下手了
- 改进建议:
- 于我自己:应该多花时间在PTA作业上,这样不至于最后几天赶菜单来不及导致分数过低,应该注意代码简洁,能写类的尽量写类,能写方法的尽量写方法,对数据处理要敏感,对特殊情形处理要有条理按次序
- 于老师:应该及时讲解题目或者发布相关测试点或标准题解,这样不至于让学生做完一次下一次还是这么差,我做了这么多菜单但是因为一直一知半解所以除了低分外更是一点进步没有,其次就是难度可以递进一点出现,比如一道题要么是高分要么就没分了...
- 总结
其实上面该总结的都总结的差不多了,最后还要说的话就是自己在java上面花的时间其实不算多,上课其实听的还挺认真的,但是下课回去因为没有相关练习,自己也不会主动去把上课那些东西再敲一遍,导致其实相关知识掌握的如何我自己都没有什么把握,对“面对对象编程”其实还是不太了解其内核本质,这样就比较可怕了,java比上学期c语言要难的多,但是更加智能,用起来更加方便,而我花点时间却远不如上学期那般,其中有各种原因而搁置了相关的学习,还是要踏踏实实去做的未来!!!
浙公网安备 33010602011771号