package?project.Car;
public?abstract?class?Car?{
??String?name;
??double?price;
??int?person,?cargo;
??
??int?getCargo()?{
?? return?0;
??}
??int?getPerson()?{
?? return?0;
??}
}
public?class?CargoCar?extends?Car{???????????????????????????????????//裝貨
private?int?cargo;
public?CargoCar(String?name,?double?price,?int?cargo)?{
this.name?=?name;
this.price?=?price;
this.setCargo(cargo);
}
public?int?getCargo()?{
return?cargo;
}
public?void?setCargo(int?cargo)?{
this.cargo?=?cargo;
}
}
public?class?PassengerCar?extends?Car?{???????????????????????????????????????????????//載人
private?int?person;
public?PassengerCar(String?name,?double?price,?int?person)?{
this.name?=?name;
this.price?=?price;
this.setPerson(person);
}
public?int?getPerson()?{
return?person;
}
public?void?setPerson(int?person)?{
this.person?=?person;
}
}
public?class?PickupCar?extends?Car{?????????????????????????????//皮卡
private?int?person;
private?int?cargo;
public?PickupCar(String?name,?double?price,?int?person,?int?cargo)?{
this.name?=?name;
this.price?=?price;
this.person?=?person;
this.cargo?=?cargo;
}
public?int?getPerson()?{
return?person;
}
public?void?setPerson(int?person)?{
this.person?=?person;
}
public?int?getCargo()?{
return?cargo;
}
public?void?setCargo(int?cargo)?{
????????this.cargo?=?cargo;
}
}
package?project.Car;
import?java.util.Scanner;?
public?class?Car_Rent?{
static?double?Price,?num_Cargo;??????//Price為總價格,?num_Cargo為總貨物量
static?int?num_Person;???????????//num_Person為可載總人數(shù)
public?static?void?main(String[]?args)?{
if?(askFirst()?==?1)?{
Car[]?car?=?{new?PassengerCar("奧迪A4",?500,?4),?new?PassengerCar("馬自達6",?400
,?4),?new?PickupCar("皮卡雪6",?450,?4,?2),new?PassengerCar("金龍",?800,?20),?
new?CargoCar("松花江",?400,?4),?new?CargoCar("依維柯",?1000,?20)
};
show(car);
askSecond(car);
}
}
static?int?askFirst()?{?????????//判斷是否租車
System.out.println("歡迎使用租車系統(tǒng):");
System.out.println("您是否要租車:1是?0否");
Scanner?input?=?new?Scanner(System.in);
int?judge?=?input.nextInt();
return?judge;
}
static?void?show(Car[]?car)?{??????//展示價目表,\t制表符,橫向跳8個空格
System.out.println("您可租車的類型及其價目表:\n序號\t汽車名稱\t\t租車\t\t容量");
for(int?i?=?0;?i?<?car.length;?i++)?{
if?(car[i].person?>?0?&&?car[i].cargo?>?0)?{
System.out.println((i+1)+".\t"+car[i].name+"\t\t"+car[i].price+"元/天\t?"+
"載人:"+car[i].person+"人?"+"載貨:"+car[i].cargo+"噸");
}
else?if?(car[i].person?>?0)?{
System.out.println((i+1)+".\t"+car[i].name+"\t\t"+car[i].price+"元/天\t"+
"載人:"+car[i].person+"人?");
}
else?{
System.out.println((i+1)+".\t"+car[i].name+"\t\t"+car[i].price+"元/天\t"+
"載貨:"+car[i].cargo+"噸");
}
}
}
static?void?askSecond(Car[]?car)?{
System.out.println("請輸入您要租汽車的數(shù)量:");
Scanner?input?=?new?Scanner(System.in);
int?num?=?input.nextInt();???????????????????????//num為租車數(shù)量
int[]?recode?=?new?int[num];?????????????????????//recode數(shù)組記錄所租車輛的編號,tag是該數(shù)組編號
int?tag?=?0;?
for?(int?i?=?1;?i?<=?num;?i++){?????????????//輸入所租車輛序號,更新總價格和載貨量,載人數(shù)等數(shù)據(jù)
System.out.println("請輸入第"+i+"輛車的序號:");
int?code?=?input.nextInt();
recode[tag++]?=?code-1;
Price?+=?car[code-1].price;
num_Cargo?+=?car[code-1].getCargo();
num_Person?+=?car[code-1].getPerson();
}
???????? System.out.println("請輸入租車天數(shù):");
???????? int?days?=?input.nextInt();
???????? Price?*=?days;
???????? System.out.println("您的賬單:\n***可載人的車有:");
???????? for(int?temp?=?0;?temp?<?tag;?temp++)?{
???????? if?(car[recode[temp]].getPerson()?>?0)?{????????//輸出能載人的車的name,下面同理
???????? System.out.print(car[recode[temp]].name+"?");
???????? }?
???????? }
???????? System.out.println("共載人:"+num_Person+"人");
???????? System.out.println("***載貨的車有:");
???????? for(int?temp?=?0;?temp?<?tag;?temp++)?{
???????? if?(car[recode[temp]].getCargo()?>?0)?{
???????? System.out.print(car[recode[temp]].name+"?");
???????? }
???????? }
???????? System.out.println("共載貨:"+num_Cargo+"噸");
???????? System.out.println("***租車總價格:"+Price+"元");
}
}

2019-06-03
你前面在CargoCar類,PassengerCar類和PickupCar類里面把person和cargo設置為private,在下面的main方法里面又直接調用了這兩個屬性而不是通過get方法來獲取,所以輸出的結果容量都是0
2019-06-02
程序展示? 載貨怎么都是0啊
2019-05-09
請教一個問題,我把方法寫到主類里面了,你這是都寫到主方法里面的吧?這兩種方式哪個更好一點呢?