花了一個小時做好了
父類 package?com.liuxiaodao; public?class?Vehicle?{ public?int?carNumber;//車序列 public?String?carName;?//車名字 public?int?rent;//租金 public?int?personCapacity;//載客量 public?double?freightVolume;//載貨量 public?String?getCarName()?{ return?carName; } public?void?setCarName(String?carName)?{ this.carName?=?carName; } public?int?getRent()?{ return?rent; } public?void?setRent(int?rent)?{ this.rent?=?rent; } public?int?getCarNumber()?{ return?carNumber; } public?void?setCarNumber(int?carNumber)?{ this.carNumber?=?carNumber; } public?int?getPersonCapacity()?{ return?personCapacity; } public?void?setPersonCapacity(int?personCapacity)?{ this.personCapacity?=?personCapacity; } public?double?getFreightVolume()?{ return?freightVolume; } public?void?setFreightVolume(double?freightVolume)?{ this.freightVolume?=?freightVolume; } public?void?show()?{ //?TODO?Auto-generated?method?stub } } Passengercar類 package?com.liuxiaodao; public?class?Passengercar?extends?Vehicle?{ public?Passengercar(int?carNumber,String?carName,int?rent,int?personCapacity)?{ this.setCarNumber(carNumber); this.setCarName(carName); this.setRent(rent); this.setPersonCapacity(personCapacity); } public?void?show()?{ System.out.println(?"?"+carNumber?+?"\t"+carName?+"\t"?+?rent?+"一天"?+?"\t\t載客量"?+?personCapacity+"人"); } } ?Pika類 public?class?Pika?extends?Vehicle{ public?Pika(int?carNumber,String?carName,int?rent,int?personCapacity,double?freightVolume)?{ this.setCarName(carName); this.setCarNumber(carNumber); this.setRent(rent); this.setPersonCapacity(personCapacity); this.setFreightVolume(freightVolume);} public?void?show()?{ System.out.println("?"+carNumber?+?"\t"+carName?+"\t"?+?rent?+?"一天"?+?"\t\t載客量"?+?personCapacity+?"人"+?"載貨量"?+freightVolume+"噸"); }}
Truck類
public class Truck extends Vehicle {
public Truck(int carNumber,String carName,int rent,double freightVolume) {
this.setCarName(carName);
this.setCarNumber(carNumber);
this.setRent(rent);
this.setFreightVolume(freightVolume);
}
public void show() {
System.out.println(" "+carNumber +"\t"+ carName +"\t" + rent +"一天" + "\t\t載貨量" + freightVolume+"噸");
}
}
Text類
public class Text {
public static void main(String[] args) {
// TODO Auto-generated method stub
Vehicle a = new Passengercar(1,"奧迪A4",500,4);
Vehicle b = new Passengercar(2,"寶馬3系",400,4);
Vehicle c = new Pika(3,"皮卡雪6",450,4,2);
Vehicle d = new Passengercar(4,"金龍",800,20);
Vehicle e = new Truck(5,"松花江",400,4);
Vehicle f = new Truck(6,"依維柯",1000,20);
Vehicle vehicle[] = {a,b,c,d,e,f,};
System.out.println("歡迎使用小叨租車系統(tǒng):");
System.out.println("你是否需要租車:1是 0否");
Scanner input = new Scanner(System.in);//使用Scanner來實現(xiàn)用戶輸入
int in =input.nextInt();
if(in == 1) {
System.out.println("您可租車的類型及其價目表:");
System.out.println("編 號"+"\t? 類 型"+"\t日租金/天"+"\t\t容量");
a.show();
b.show();
c.show();
d.show();
e.show();
f.show();
System.out.println("請輸入您租車的數(shù)量:");
Scanner input2 = new Scanner(System.in);
int num = input2.nextInt();
System.out.println("請輸入您要借租的天數(shù)");
Scanner input3 = new Scanner(System.in);
int day =input3.nextInt();
for(int i =0;i<num;i++) {
System.out.println("請輸入第"+ (i+1) +"輛車的序號");
Scanner input4 = new Scanner(System.in);
int xuhao = input4.nextInt();
vehicle[i] =vehicle[xuhao-1];
}
System.out.println("統(tǒng)計完成\n您的賬單如下:");
System.out.println("可以載人的有:");
for(int n=0;n<num;n++) {
if(vehicle[n].personCapacity != 0) {
System.out.println(vehicle[n].carName);
}
}
System.out.println("可以載貨的有:");
for(int m=0;m<num;m++) {
if(vehicle[m].freightVolume !=0) {
System.out.println(vehicle[m].carName);
}
}
//統(tǒng)計賬單金額數(shù)目
int sumPerson = 0;
double sumFreight = 0;
double sumRent = 0;
for(int x=0;x<num;x++) {
sumPerson =vehicle[x].personCapacity+sumPerson;
sumFreight = vehicle[x].freightVolume+sumFreight;
sumRent = vehicle[x].rent+sumRent;
}
System.out.println("總載客量:"+sumPerson);
System.out.println("總載貨量:"+sumFreight);
System.out.println("總價格:"+sumRent*day);
}else if(in == 0) {
System.out.println("再見,歡迎您下次使用!");
}
}
}
2019-03-30
vehicle[i] =vehicle[xuhao-1];
這段是錯,會覆蓋掉原有保存的汽車數(shù)據(jù)
應(yīng)該再創(chuàng)建一個數(shù)組
all[] cars = new all[num];
然后在下面把vehicle[i] =vehicle[xuhao-1];替換成cars[i] = all[xuhao-1];
才沒有問題
2019-03-26
你并不需要每次都去new一個Scanner對象,就用第一次實例化的哪個對象給變量賦值就可以了。如:
int a =input.nextInt();
int b=input.nextInt();
等等都是可以的。
2019-03-21
父類無需get/setter方法也可完成,沒有私有變量
2019-03-14
這個輸入的序號超過數(shù)組長度不行啊 會報錯
2019-03-11
vehicle[i] =vehicle[xuhao-1];
這樣真的沒有問題嗎?
2019-03-08
請問大神,你完成這個項目的思路是怎樣的,是和老師說的一樣嗎?如果不是你的思路是怎樣的?請指教。我想不到這么多代碼,寫著寫著就亂了
2019-03-04
public
?Passengercar(
int
?carNumber,String?carName,
int
?rent,
int
?personCapacity)?{??????
this
.setCarNumber(carNumber);??????
this
.setCarName(carName);??????
this
.setRent(rent);????
this
.setPersonCapacity(personCapacity); }?
兄弟,這個是重載嗎?還是啥,以前的課有講嗎
2019-03-04
tqltql。