分享一下代碼求大佬給一點(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ǒ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("您可租車的類型及其價目表:");
????????System.out.println("序號???汽車名字???租金???容量");
????????for?(int?i?=?0;?i?<?cars.length;?i++)?{
????????????System.out.print(i+"???");
????????????cars[i].show();
????????}
????????System.out.println("請輸入您要租汽車的數(shù)量:");
????????int?sl=s1.nextInt();//捕捉用戶輸入的數(shù)量
????????int[]?sum=new?int[sl];//用來存儲用戶輸入的車的序號。
????????for?(int?i?=?0;?i?<?sl;?i++)?{
????????????System.out.printf("請輸入第%d輛車的序號:",i+1);
????????????sum[i]=s1.nextInt();//捕捉用戶輸入車的序號并存入數(shù)組
????????}
????????System.out.println("請輸入租車天數(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)計載客總數(shù)
??????double?weights=0;//統(tǒng)計載貨總數(shù)
??????int?rents=0;//統(tǒng)計總金額
??????System.out.println("***可載人的車有:");
??????for?(int?i?=?0;?i?<?sum.length;?i++)?{
??????????????//判斷這個序號的車是否是客車不是就跳過這次循環(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;
??????????????????????//防止皮卡的金額被二次計算
??????????????????????if?(sum[i]!=3)?{
??????????????????????????????rents+=cars[sum[i]-1].rent;
??????????????????????}
??????????????}
??????}
??????System.out.println("共載貨:"+weights+"噸");
??????System.out.println("租車總價格:"+rents*days+"元");
??}
}代碼我都是從eclipse一行一行抄下來的(這個竟然直接粘貼會寫成一行),不過在這上面運(yùn)行不了,應(yīng)該是類名不對(他所有的類名都是HelloWorld的),不過我把運(yùn)行效果截圖了,求大佬幫我看看有什么可以優(yōu)化的地方。
2019-04-24
樓主想問一下,Car[] cars={}是啥,數(shù)組嗎?
2019-02-10
如果用戶輸入的不是0和1會是什么結(jié)果
2019-02-10
這是我看的最好的一個
2018-11-10
樓主 我super();調(diào)用父類不行啊,報錯
2018-10-31
System.out.println("序號? 汽車名字? 租金? 數(shù)量");????????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
你的生成賬單是確立在有規(guī)律性上的,如果用 isInstance 會不會好些
2018-09-30
下面是我寫的源代碼,感興趣的朋友可以看一下,歡迎交流學(xué)習(xí)。
源代碼放在了【碼云】上,源碼地址:
https://gitee.com/cnsdhh/cnsdhh/tree/master/lang-java/com/cnsdhh/car