分享一下代碼求大佬給一點(diǎn)優(yōu)化思路
import?java.util.Scanner; import?java.lang.System; //車的抽象類 public?abstract?class?car?{ ??String?carName;//車名 ??int?persons;//載客量 ??double?weights;//載貨量 ??int?rent;//租金 ???? ??//給客車使用的構(gòu)造方法 ??public?car(String?carName,int?persons,int?rent)?{ ????super(); ????this.carName=carName; ????this.persons=persons; ????this.rent=rent; ??} ???? ??//給貨車使用的構(gòu)造方法 ??public?car(String?carName,double?weights,int?rent)?{ ????super(); ????this.carName=carName; ????this.weights=weights; ????this.rent=rent; ??} ???? ??//給皮卡使用的構(gòu)造方法 ??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+"噸"); ????} } //租車前臺(tái)系統(tǒng)類 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?("馬自達(dá)",?4,?400),new?passengerCar?("奧迪A4",?4,?500)}; ????System.out.println("歡迎使用嗒嗒租車系統(tǒng):"); ????System.out.println("您是否要租車:1是?0否"); ????Scanner?s1=new?Scanner(System.in); ????int?xz=s1.nextInt();//捕捉用戶選擇 ????if?(xz==1)?{ ????????System.out.println("您可租車的類型及其價(jià)目表:"); ????????System.out.println("序號(hào)???汽車名字???租金???容量"); ????????for?(int?i?=?0;?i?<?cars.length;?i++)?{ ????????????System.out.print(i+"???"); ????????????cars[i].show(); ????????} ????????System.out.println("請(qǐng)輸入您要租汽車的數(shù)量:"); ????????int?sl=s1.nextInt();//捕捉用戶輸入的數(shù)量 ????????int[]?sum=new?int[sl];//用來存儲(chǔ)用戶輸入的車的序號(hào)。 ????????for?(int?i?=?0;?i?<?sl;?i++)?{ ????????????System.out.printf("請(qǐng)輸入第%d輛車的序號(hào):",i+1); ????????????sum[i]=s1.nextInt();//捕捉用戶輸入車的序號(hào)并存入數(shù)組 ????????} ????????System.out.println("請(qǐng)輸入租車天數(shù):"); ????????int?days=s1.nextInt();//捕捉用戶輸入的天數(shù) ????????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;//統(tǒng)計(jì)載客總數(shù) ??????double?weights=0;//統(tǒng)計(jì)載貨總數(shù) ??????int?rents=0;//統(tǒng)計(jì)總金額 ??????System.out.println("***可載人的車有:"); ??????for?(int?i?=?0;?i?<?sum.length;?i++)?{ ??????????????//判斷這個(gè)序號(hào)的車是否是客車不是就跳過這次循環(huán) ??????????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; ??????????????????????//防止皮卡的金額被二次計(jì)算 ??????????????????????if?(sum[i]!=3)?{ ??????????????????????????????rents+=cars[sum[i]-1].rent; ??????????????????????} ??????????????} ??????} ??????System.out.println("共載貨:"+weights+"噸"); ??????System.out.println("租車總價(jià)格:"+rents*days+"元"); ??} }
代碼我都是從eclipse一行一行抄下來的(這個(gè)竟然直接粘貼會(huì)寫成一行),不過在這上面運(yùn)行不了,應(yīng)該是類名不對(duì)(他所有的類名都是HelloWorld的),不過我把運(yùn)行效果截圖了,求大佬幫我看看有什么可以優(yōu)化的地方。
2018-09-19
為什么我的super調(diào)用父類不行啊
2018-09-10
在請(qǐng)輸入第幾輛車的序號(hào)后面 應(yīng)該每一次都是用戶自己輸入的 不是只按圖片結(jié)果那樣顯示的吧?
2018-09-10
類名不得大寫嗎
2018-09-01
666
2018-08-31
你這個(gè)覺得其實(shí)已經(jīng)優(yōu)化過了,較之其他人的代碼,你這個(gè)還是很不錯(cuò)的