分享一下代碼求大佬給一點優化思路
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的),不過我把運行效果截圖了,求大佬幫我看看有什么可以優化的地方。
2019-04-24
樓主想問一下,Car[] cars={}是啥,數組嗎?
2019-02-10
如果用戶輸入的不是0和1會是什么結果
2019-02-10
這是我看的最好的一個
2018-11-10
樓主 我super();調用父類不行啊,報錯
2018-10-31
System.out.println(
"序號? 汽車名字? 租金? 數量"
);
???????
?
for
?(
int
?i?=?
0
;?i?<?cars.length;?i++)?{
????????????
????System.out.print(i+
1+
"???"
);
????????????
????cars[i].show();
????????
}
序號在輸出的時候,改成i+1才對吧,不然按照0,1,2,3的時候,輸入的序號對不上的。
2018-10-11
很贊! 方法的重載那里用的很好
2018-10-09
新手想問一下,樓主你的代碼里,abstract void show()是什么意思啊,看不懂這里,可以解答一下嗎0.0
2018-10-08
記錄一下 以后遇見了看
2018-10-02
你的生成賬單是確立在有規律性上的,如果用 isInstance 會不會好些
2018-09-30
下面是我寫的源代碼,感興趣的朋友可以看一下,歡迎交流學習。
源代碼放在了【碼云】上,源碼地址:
https://gitee.com/cnsdhh/cnsdhh/tree/master/lang-java/com/cnsdhh/car