數據持久化 PlayerPrefs相關 PlayerPrefs是Unity游戲引擎中的一個類,用於在游戲中存儲和訪問玩家的偏好設置和數據。它可以用來保存玩家的游戲進度、設置選項、最高分數等信息。PlayerPrefs將數據存儲在本地文件中,因此可以在游戲重新啟動時保持數據的持久性。 //Player ...
(1)前言:在pta第4.5.6次大作業和期中考試所包含的知識點主要有:理解類的概念,創建類和實例化對象。定義方法,方法的參數傳遞和返回值。使用條件語句(if-else)、迴圈語句(for、while)等來控制菜單存儲和用戶輸入。用於存儲菜單選項和處理用戶的選擇的數組或類數組。題目的難度根據題目不同也有所不同,第四題的菜單題只需要做基本的菜單顯示和用戶輸入處理,隨後的5.6兩道題目要求考慮異常輸入的處理並需要使用更複雜的數據結構來實現菜單功能。題量相對適中,第4道pta題目可能較為簡單,兩道大題主要是針對對類的設計和方法實現,另外兩道題目針對遍歷排序和日期比較考察了部分基礎知識和庫函數知識。第5.6道題目大幅度增加複雜度了而減少題目數量,其編程設計需要考慮更多的設計和實現細節。
(2) 設計與分析:
菜品包含特色口味輸出類的代碼時間複雜度大體上是O(n),其中n為輸入菜品記錄的數量。在程式中定義了
菜品:dishname菜名,price單價,Kind:菜的種類
菜單:包含菜品數組
桌子類:table_number: 桌號orders: 已點菜品列表
用戶類:name: 用戶名,pay用戶需付價格
菜品記錄類:order_number: 序號items: 已點菜品列表
這些類的屬性和方法都比較簡單,因此這段代碼的整體邏輯清晰,易於理解和調試。在程式的主函數中,使用了Scanner類從用戶的輸入讀取輸入信息,根據Match判斷的返回值判斷其輸入格式是否正確後轉入相應的if語句中執行相應的操作。各自調用相關類的各種方法結合Dish中存儲的菜品價格和菜品類型計算所點菜品的價格最後得到訂單總價和用戶所需要付的價格後統一輸出,其中折扣判斷在桌號數據行輸入後日期折扣判斷已經完成,而後在計算時根據是否是特色菜結合日期判斷的折扣值計算價格。通過將不同的功能分別封裝在不同的類中,可以使得程式結構更加清晰,並且易於擴展和維護。
對於多種錯誤輸入的菜品輸入的題目,由於其判斷代碼出現失誤,導致輸入語句在接收後不知道語句進入了哪個判斷語句,完全無法判斷錯誤輸入。最後總結為思路錯誤,無法適應錯誤輸入的數據並完成相應的輸出判斷。最後寫的代碼全部作廢,寫不下去了。
關於期中考試的實驗題,之前的實驗題都是根據給出的類來完善主函數,而這次考試則是根據主函數來完善類中數據介面,導致有些措手不及。
而對於第四次pta題目,對於第五第六次的題目而言實在是小巫見大巫,沒有可比性。
(3)踩坑心得:在對輸入數據的處理過程中,一定要選擇合適的輸入數據判斷方法,一定要能夠兼顧完成對正常數據的輸入和異常數據的判斷和處理輸出,以應對題目需求。
Exception in thread "main" java.lang.NullPointerException: Cannot load from object array because "cou.stu" is null at Main.Match(Main.java:178) at Main.main(Main.java:278):在main函數中對類的聲明後沒有初始化便開始調用,使得指針嘗試將值存儲到一個空對象數組時出錯,為了避免此類情況的發生,對於對象類數組的調用一定要先確保其已經被正確初始化並分配了足夠的空間。
(4)主要困難以及改進建議:對於對輸入數據的處理與判別尤其需要細心並且細緻討論各類情況,防止非法輸入導致程式無法正常運行。
(5)總結:關於此次解題經驗,我總結了部分要點:
1.明確題目要求與意義:根據題目說明和所提供的示例輸入輸出等輔助信息理解題目要求與目的。
2.理清解題思路再寫代碼:在理解了題意後,先理清題目的解題思路,避免在編寫代碼的過程中出現錯誤。
3.考慮邊界條件和異常情況:在編寫題目時要考慮到各種可能的輸入情況,包括邊界值和異常情況,以保證代碼的穩定性。
4.不斷學習更新知識:題目的編寫過程也是對自己知識的回顧和鞏固,通過編寫題目可以不斷學習並更新自己的知識體系,只有這樣才能適應多變的題型。
總的來說,這次寫題目對我來說是一次很有收穫的經歷。通過這次寫題目,我鍛煉了自己的問題設計和解決能力,並提高了對編程知識的理解和應用能力。希望在以後的學習和實踐中能夠繼續加油,不斷提升自己的技術水平。
以下附特色口味菜品輸入源碼:
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Calendar;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;
public class Main{
public static class Dish
{
String name;//菜品名稱
int level=-1;
int kind=-1;
int unit_price; //單價
String[][] tastes = {
{"不辣","微辣","稍辣", "辣", "很辣", "爆辣"},
{"不酸","微酸","稍酸","酸","很酸"},
{"不甜","微甜","稍甜","甜"}
};
String[] tastprint={"spicy","acidity","sweetness"};
int getPrice(int portion)
{
int x=0;
double temp=1;
double prise=0;
if(portion==2)
{temp=1.5;}
else if(portion==3)
{temp=2;}
prise=this.unit_price*temp;
x=Math.round((float)prise);
return x;
}
//計算菜品價格的方法,輸入參數是點菜的份額(輸入數據只能是1/2/3,代表小/中/大份) }
//菜譜類:對應菜譜,包含飯店提供的所有菜的信息。
}
public static class Menu {
Dish[] dishs=new Dish[10];//菜品數組,保存所有菜品信息
int count = 0;
Dish searthDish(String dishName)//根據菜名在菜譜中查找菜品信息,返回Dish對象。
{
for(int i=0;i<count;i++)
{
if(dishs[i].name!=null) {
if(dishs[i].name.equals(dishName)) {
return dishs[i];
}
}
}
// System.out.print(dishName+" does not exist\n");
return null;
}
int searthdish(String dishName)//根據菜名在菜譜中查找菜品信息,返回Dish對象。
{
for(int i=0;i<count;i++)
{
if(dishs[i].name!=null) {
if(dishs[i].name.equals(dishName)) {
return i;
}
}
}
// System.out.print(dishName+" does not exist\n");
return -6;
}
void addDish(String[] word)//添加一道菜品信息
{
int unit_price=0;String dishName;
if (this.dishs[count] == null)
{
this.dishs[count] = new Dish();
}
dishName=word[0];
if(word.length==4)
{
unit_price=Integer.parseInt(word[2]);
}
else
{
if(word.length==2){
unit_price=Integer.parseInt(word[1]);}
else
{System.out.print("wrong format\n");
return;}
}
if (count > 1)
{
int i = searthdish(dishName);
if (i != -6)
{
this.dishs[i].name = dishName;
this.dishs[i].unit_price = unit_price;
}
else {
this.dishs[count].name = dishName;
this.dishs[count].unit_price = unit_price;
switch (word[1]) {
case "川菜":
this.dishs[count].kind=0;
break;
case "晉菜" :
this.dishs[count].kind=1;
break;
case "浙菜":
this.dishs[count].kind=2;
break;
}
count++;
}
}
else
{
this.dishs[count].name = dishName;
this.dishs[count].unit_price = unit_price;
switch (word[1]) {
case "川菜":
this.dishs[count].kind=0;
break;
case "晉菜" :
this.dishs[count].kind=1;
break;
case "浙菜":
this.dishs[count].kind=2;
break;
}
count++;
}
}
}
//點菜記錄類:保存訂單上的一道菜品記錄
public static class Record {
int orderNum;//序號
int orderX=0;//訂單份數
Dish d;//菜品\\
int replace=0;
int replaced=0;
int portion=0;//份額(1/2/3代表小*1/中*1.5/大份*2)\\
int getPrice()//計價,計算本條記錄的價格\\
{
int ds=d.getPrice(portion);
return ds;
}
}
public static class User
{
String name,tele;
int pay=0;
}
//訂單類:保存用戶點的所有菜的信息。
public static class Order {
User user=new User();
Record[] records=new Record[50];//保存訂單上每一道的記錄
int ordercount=0;
int temp=0;
int[] time=new int[8];
int[] taste={0,0,0};
int[] tastecount={0,0,0};
int getTotalPrice(int i)//計算訂單的總價
{
int price=0;
if(i<ordercount){
if(records[i]!=null)
{price=price+records[i].getPrice()*records[i].orderX;}
}
return price;
}
void addARecord(int judge,int orderNum,String dishName,int portion,int num,int tense,Menu menu)//添加一條菜品信息到訂單中。
{
if(this.records[this.ordercount]==null)
{this.records[this.ordercount]=new Record();}
this.records[this.ordercount].replace=judge;
this.records[this.ordercount].orderNum=orderNum;
this.records[this.ordercount].orderX=num;
this.records[this.ordercount].portion=portion;
if (menu.searthDish(dishName)!= null)
{
if (this.records[this.ordercount].replace!= 0)//代點菜
{
this.records[this.ordercount].d = new Dish();
this.records[this.ordercount].d =menu.searthDish(dishName);
if(this.records[this.ordercount].d.kind!=-1) {
this.records[this.ordercount].d.level=tense;
if(this.records[this.ordercount].d.tastes[this.records[this.ordercount].d.kind].length<=tense)
{
System.out.print(this.records[this.ordercount].d.tastprint[this.records[this.ordercount].d.kind]+" num out of range : "+tense+"\n");
this.ordercount--;
}
else
{
System.out.print(orderNum + " table " + (this.time[0]) + " pay for table " + this.records[this.ordercount].replace+ " " + this.records[this.ordercount].getPrice() * this.records[this.ordercount].orderX + "\n");
}
}
else
{
System.out.print(orderNum + " table " + (this.time[0]) + " pay for table " + this.records[this.ordercount].replace + " " + this.records[this.ordercount].getPrice() * this.records[this.ordercount].orderX + "\n");
}
}
else//自點菜
{
this.records[this.ordercount].d = new Dish();
this.records[this.ordercount].d=menu.searthDish(dishName);
if(this.records[this.ordercount].d.kind!=-1) {
this.records[this.ordercount].d.level=tense;
if(this.records[this.ordercount].d.tastes[this.records[this.ordercount].d.kind].length<=tense)
{
System.out.print(this.records[this.ordercount].d.tastprint[this.records[this.ordercount].d.kind]+" num out of range :"+tense+"\n");
this.ordercount--;
}
else
{
this.taste[this.records[this.ordercount].d.kind]=this.taste[this.records[this.ordercount].d.kind]+tense*this.records[this.ordercount].orderX;
this.tastecount[this.records[this.ordercount].d.kind]=this.tastecount[this.records[this.ordercount].d.kind]+this.records[this.ordercount].orderX;
// System.out.print(" "+this.taste[this.records[this.ordercount].d.kind]+" "+this.records[this.ordercount].orderX+"\n\n");
System.out.print(this.records[this.ordercount].orderNum + " " + this.records[this.ordercount].d.name + " " + this.records[this.ordercount].getPrice() * this.records[this.ordercount].orderX + "\n");
}
}
else
{
System.out.print(this.records[this.ordercount].orderNum + " " + this.records[this.ordercount].d.name + " " + this.records[this.ordercount].getPrice() * this.records[this.ordercount].orderX + "\n");
}
}
this.ordercount++;
}
else if (menu.searthDish(dishName) == null)
{
System.out.print(dishName + " does not exist\n");
}
}
void delARecordByOrderNum(int orderNum,Menu menu)//根據序號刪除一條記錄
{
if(records[orderNum]!=null)
{
if(records[orderNum].d!=null) {
if (menu.searthDish(records[orderNum].d.name) != null) {
records[orderNum].orderX = 0;
records[orderNum].d.kind=-1;
}
}
else
{
System.out.print("delete error;\n");
}
}
else
System.out.print("delete error;\n");
}
int addtable(String str,long[] num)
{
for (int j = 0; j < 8; j++)
{
if (j == 1)
{
this.time[j+1] =(int)num[j+1];
j++;
}
else
{
this.time[j] =(int)num[j];
}
}
String[] word=str.split("\\s+");
this.user.name=word[3];
this.user.tele=word[4];
if(word[4].length()!=11)
{System.out.print("wrong format\n");
return 0;}
if (judgetime(this.time,(int)num[0])==0) {
System.out.print("table " + num[0]+ " out of opening hours\n");
return 0;
}
else
{
System.out.print("table " + num[0] + ": \n");
return 1;
}
}
}//Order
public static int Match(String x)
{
int num=99;
String regex1="\\S{1,10} \\d{1,2}$";
String regex11="\\S{1,10} \\p{IsHan}{1,10} \\d{1,2} T$";
String regex2="table \\d{1} : [a-z]{1,10} \\d{1,11} \\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}/\\d{1,2}/\\d{1,2}$";
String regex3="\\d{1} \\S{1,10} [1-3] \\d{1,2}$";//zz普通菜
String regex4="\\d{1} \\d{1} \\S{1,10} [1-3] \\d{1,2}$";//dd普通菜
String regex31="\\d{1} \\S{1,10} \\d{1} [1-3] \\d{1,2}$";//zz特色菜
String regex41="\\d{1} \\d{1,2} \\S{1,10} \\d{1} [1-3] \\d{1,2}$";//dd特色菜
String regex32="\\d{1,2} \\S{1,10} \\d{1} [1-3] \\d{1,2}$";//zz
String regex42="\\d{1,2} \\d{1,2} \\S{1,10} \\d{1} [1-3] \\d{1,2}$";//dd
String regex5="\\d{1} delete$";
Pattern pattern1=Pattern.compile(regex1);
Pattern pattern11=Pattern.compile(regex11);//T
Pattern pattern2=Pattern.compile(regex2);
Pattern pattern3=Pattern.compile(regex3);
Pattern pattern4=Pattern.compile(regex4);
Pattern pattern31=Pattern.compile(regex31);
Pattern pattern41=Pattern.compile(regex41);
Pattern pattern32=Pattern.compile(regex32);
Pattern pattern42=Pattern.compile(regex42);
Pattern pattern5=Pattern.compile(regex5);
Matcher matcher1=pattern1.matcher(x);
Matcher matcher11=pattern11.matcher(x);//T
Matcher matcher2=pattern2.matcher(x);
Matcher matcher3=pattern3.matcher(x);
Matcher matcher4=pattern4.matcher(x);
Matcher matcher5=pattern5.matcher(x);
Matcher matcher31=pattern31.matcher(x);
Matcher matcher41=pattern41.matcher(x);
if(matcher1.matches())
{
num=0;}//麻婆豆腐 12
else if(matcher2.matches())
{
num=1;}
else if(matcher3.matches())
{
num=2;}//3 麻辣雞絲 1 2
else if(matcher4.matches())
{
num=3;}//1 4 麻婆豆腐 1 1代付菜
else if(matcher5.matches())
{
num=4;}//1 delete
else if(matcher11.matches())
{
num=11;
}
else if(matcher31.matches())
{
num=31;
}
else if(matcher41.matches())
{
num=41;
}
return num;
}
public static double judgetime(int[] time,int table)
{
/*周一至周五營業時間與折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其餘時間不營業。
周末全價,營業時間:9:30-21:30*/
double n=0;
double temp=0;
Calendar calendar = Calendar.getInstance();
calendar.set(time[2],(time[3]-1),time[4]);
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
//獲取星期幾,周日為1,周一為2
n=time[5]*3600+time[6]*60+time[7];
//17:00-20:30 61200-73800
//10:30-14:30 37800-52200
if(weekday>1&&weekday<7)
{
if (n>=37800&&n<=52200)
{
temp=0.6;
}
else if(n>=61200&&n<=73800)
{
temp=0.8;
}
else
{
temp=0;
}
}
//9:30-21:30 34200-77400
else if(weekday==1||weekday==7)
{
if (n>=34200&&n<=77400)
{
temp=1;
}
else
{
temp=0;
}
}
return temp;
}
public static boolean isTimeInRange(LocalDateTime time, LocalTime startTime, LocalTime endTime) {
LocalTime givenTime = time.toLocalTime();
return givenTime.isAfter(startTime) && givenTime.isBefore(endTime);
}
public static double discountnum(Order order,double judge,int num)
{
double dis=0;
if(order.records[num].d.kind!=-1)
{
if(judge==0.8||judge==0.6)
{dis=0.7;}
else if(judge==1)
{dis=1;}
}
else
{
dis=judge;
}
return dis;
}
/*
周一至周五營業時間晚上(17:00-20:30)8折,
周一至周五中午(10:30--14:30)6折,其餘時間不營業。
周末全價,營業時間:9:30-21:30
例如:麻婆豆腐 川菜 9 T
菜價的計算方法:
周一至周五 7折, 周末全價。
*/
public static void dishout(Order order)
{
Dish dish=new Dish();
int aver=0;
if(order.tastecount[0]!=0)
{
aver=(int)Math.round((double)order.taste[0]/(double)order.tastecount[0]);
//System.out.print(" \n\ntaste"+order.taste[0]+" count"+order.tastecount[0]+" aver"+aver+"\n\n");
System.out.print(" 川菜 "+order.tastecount[0]+" "+dish.tastes[0][aver]);
}
if(order.tastecount[1]!=0)
{
aver=(int)Math.round((double)order.taste[1]/(double)order.tastecount[1]);//
System.out.print(" 晉菜 "+order.tastecount[1]+" "+dish.tastes[1][aver]);
}
if(order.tastecount[2]!=0)
{
aver=(int)Math.round((double)order.taste[2]/(double)order.tastecount[2]);//
System.out.print(" 浙菜 "+order.tastecount[2]+" "+dish.tastes[2][aver]);
}
if(order.tastecount[0]==0&&order.tastecount[1]==0&&order.tastecount[2]==0)
{
System.out.print(" ");
}
}
public static void userout(Order[] order,int[] tablex,double[] price, int x)
{
User[] users=new User[20];
int count=0;
int judge=0;
String[] word=new String[20];
for (int i = 0; i < x; i++)
{
if (count == 0)
{
word[count]=order[tablex[i]].user.name;
count++;
}
else
{
for (int j = 0; j < count; j++) {
if (order[tablex[i]].user.name.equals(word[j])) {
judge=1;
break;
}
}
if (judge == 0) {
word[count]=order[tablex[i]].user.name;
count++;
} else if (judge == 1) {
judge = 0;
}
}
}
// Arrays.sort(word);
Arrays.sort(word, Comparator.nullsLast(Comparator.naturalOrder()));
for (int i = 0; i < count; i++)
{
if(users[i]==null)
{users[i]=new User();}
for (int j = 0; j < x; j++)
{
if(word[i].equals(order[tablex[j]].user.name))
{
users[i].name=order[tablex[j]].user.name;
users[i].tele=order[tablex[j]].user.tele;
users[i].pay=users[i].pay+(int)price[j];
}
}
}
System.out.print("\n");
for(int i=0;i<count;i++)
{
System.out.print(users[i].name+" "+users[i].tele+" "+users[i].pay);
if(i+1<count){
System.out.print("\n");}
}
}
public static void main(String[] args)
{
Order[] order=new Order[20];
Menu menu=new Menu();
User[] user=new User[20];
Scanner scan=new Scanner(System.in);
String regexf="\\p{IsHan}{1,10}";
String regexn="\\d{1,12}";
Pattern patternf=Pattern.compile(regexf);
Pattern patternx=Pattern.compile(regexn);
String str;//輸入數據存儲
String dishname = null;
int n=0;//判斷輸入格式返回值
int table=0;//桌號計數
long[] num=new long[9];//臨時點菜存儲數據
int[] tablex=new int[10];
int[][] delet=new int[10][2];
int del=0;//刪除單號數據
int[] price=new int[10];//總價存儲數組
double[] disprice=new double[10];
double dis=0,dis1=0;
while(true)
{
str=scan.nextLine();
if(str.equals("end"))
{
for(int i=0;i<table;i++)
{
dis1=judgetime(order[tablex[i]].time,i);
if (dis1!=0)//輸出價格