分享一下代碼求大佬給一點優化思路
import?java.util.Scanner; import?java.lang.System; //車的抽象類 public?abstract?class?car?{ ??String?carName;//車名 ??int?persons;//載客量 ??double?weights;//載貨量 ??int?rent;//租金 ???? ??//給客車使用的構造方法 ??public?car(String?carName,int?persons,int?rent)?{ ????super(); ????this.carName=carName; ????this.persons=persons; ????this.rent=rent; ??} ???? ??//給貨車使用的構造方法 ??public?car(String?carName,double?weights,int?rent)?{ ????super(); ????this.carName=carName; ????this.weights=weights; ????this.rent=rent; ??} ???? ??//給皮卡使用的構造方法 ??public?car(String?carName,double?weights,int?persons,int?rent)?{ ????super(); ????this.carName=carName; ????this.weights=weights; ????this.persons=persons; ????????this.rent=rent; ??} ???? ????abstract?void?show(); } //客車類 public?class?passengerCar?extends?car?{ ??public?passengerCar(String?carName,int?persons,int?rent)?{ ????super(carName,persons,rent); ??} ???? ??//重寫父類show方法。 ????public?void?show(){ ????????System.out.println(carName+"??"+rent+"元/天??載人:"+persons+"人"); ????} } //貨車類 public?class?TruckCar?extends?car?{ ????public?TruckCar?(String?carName,double?weights,int?rent){ ????????super(carName,weights,rent); ????} ???? ????public?void?show(){ ????????System.out.println(carName+"??"+rent+"元/天??載貨:"+weights+"噸"); ????} } //皮卡類 public?class?pickupCar?extends?car?{ ????public?pickupCar?(String?carName,double?weights,int?persons,int?rent){ ????????super(carName,weights,persons,rent); ????} ???? ????public?void?show(){ ????????System.out.println(carName+"??"+rent+"元/天??載人:"+persons+"人,載貨:"+weights+"噸"); ????} } //租車前臺系統類 public?class?carRentSystem?{ ????public?static?void?main(String[]?args)?{ ????car[]?cars={new?TruckCar?("松花江",4.0,?400),new?TruckCar?("依維柯",20.0,1000),new?pickupCar?("皮卡雪",?4.0,?2,?450),new?passengerCar?("馬自達",?4,?400),new?passengerCar?("奧迪A4",?4,?500)}; ????System.out.println("歡迎使用嗒嗒租車系統:"); ????System.out.println("您是否要租車:1是?0否"); ????Scanner?s1=new?Scanner(System.in); ????int?xz=s1.nextInt();//捕捉用戶選擇 ????if?(xz==1)?{ ????????System.out.println("您可租車的類型及其價目表:"); ????????System.out.println("序號???汽車名字???租金???容量"); ????????for?(int?i?=?0;?i?<?cars.length;?i++)?{ ????????????System.out.print(i+"???"); ????????????cars[i].show(); ????????} ????????System.out.println("請輸入您要租汽車的數量:"); ????????int?sl=s1.nextInt();//捕捉用戶輸入的數量 ????????int[]?sum=new?int[sl];//用來存儲用戶輸入的車的序號。 ????????for?(int?i?=?0;?i?<?sl;?i++)?{ ????????????System.out.printf("請輸入第%d輛車的序號:",i+1); ????????????sum[i]=s1.nextInt();//捕捉用戶輸入車的序號并存入數組 ????????} ????????System.out.println("請輸入租車天數:"); ????????int?days=s1.nextInt();//捕捉用戶輸入的天數 ????????System.out.println("您的賬單:"); ????????new?carRentSystem?().bill(sum,cars,days); ????}else?{ ????????????System.out.println("再見"); ????????????System.exit(0); ????} ??} ?? ??//生存賬單 ??public?void?bill(int[]?sum,car[]?cars,int?days)?{ ??????int?persons=0;//統計載客總數 ??????double?weights=0;//統計載貨總數 ??????int?rents=0;//統計總金額 ??????System.out.println("***可載人的車有:"); ??????for?(int?i?=?0;?i?<?sum.length;?i++)?{ ??????????????//判斷這個序號的車是否是客車不是就跳過這次循環 ??????????if?(sum[i]>2)?{ ??????????????System.out.print(cars[sum[i]-1].carName+"?"); ??????????????persons+=cars[sum[i]-1].persons; ??????????????rents+=cars[sum[i]-1].rent; ??????????}else?{ ??????????????????continue; ??????????} ??????} ??????System.out.println("共載人:"+persons+"人"); ??????System.out.println("***可載貨的車有:");? ??????for(int?i=0;i<?sum.length;?i++)?{? ??????????????if?(sum[i]<4)?{ ??????????????????????System.out.print(cars[sum[i]-1].carName+"?"); ??????????????????????weights+=cars[sum[i]-1].weights; ??????????????????????//防止皮卡的金額被二次計算 ??????????????????????if?(sum[i]!=3)?{ ??????????????????????????????rents+=cars[sum[i]-1].rent; ??????????????????????} ??????????????} ??????} ??????System.out.println("共載貨:"+weights+"噸"); ??????System.out.println("租車總價格:"+rents*days+"元"); ??} }
代碼我都是從eclipse一行一行抄下來的(這個竟然直接粘貼會寫成一行),不過在這上面運行不了,應該是類名不對(他所有的類名都是HelloWorld的),不過我把運行效果截圖了,求大佬幫我看看有什么可以優化的地方。
2018-09-19
為什么我的super調用父類不行啊
2018-09-10
在請輸入第幾輛車的序號后面 應該每一次都是用戶自己輸入的 不是只按圖片結果那樣顯示的吧?
2018-09-10
類名不得大寫嗎
2018-09-01
666
2018-08-31
你這個覺得其實已經優化過了,較之其他人的代碼,你這個還是很不錯的