有同學(xué)能幫我看一下出現(xiàn)什么問題嗎?卡在最后一步了
程序已經(jīng)執(zhí)行完“租車天數(shù)”的輸入了,但緊接著那一段代碼(加粗)一直報(bào)錯,感覺語法應(yīng)該沒有問題,有朋友能幫我看下是什么原因嗎? //父類 package?com.imooc; ????public?abstract?class?Vehicle?{ ???? public?int?carNum; ???? public?String?carName; ???? public?double?rent; ???? public?int?passengerCapacity; ???? public?int?cargoCapacity; ???? public?abstract?void?chooseNum(int?num);//賦值來分類 ???? public?abstract?void?carList(); ???? }//客車類
package?com.imooc; public?class?Passengercar?extends?Vehicle?{ public?void?chooseNum(int?num)?{ this.carNum?=?num; switch(carNum)?{ case?1: carName?=?"奧迪A6"; rent?=?500; passengerCapacity?=?4; break; case?2: carName?=?"馬自達(dá)6"; rent?=?400; passengerCapacity?=?4; break; case?3: carName?=?"金龍"; rent?=?800; passengerCapacity?=?20; break; } } @Override public?void?carList()?{ //?TODO?Auto-generated?method?stub System.out.println(carNum?+?".?"?+?carName?+?"?"? +?rent?+?"元/天?"?+?"載人:"?+?passengerCapacity?); } } //卡車類package?com.imooc; public?class?Truck?extends?Vehicle?{ @Override public?void?chooseNum(int?num)?{ this.carNum?=?num; //?TODO?Auto-generated?method?stub switch(carNum)?{ case?4: carName?=?"松花江"; rent?=?400; cargoCapacity?=?4; break; case?5: carName?=?"依維柯"; rent?=?1000; cargoCapacity?=?20; break; } } @Override public?void?carList()?{ //?TODO?Auto-generated?method?stub System.out.println(carNum?+?".?"?+?carName?+?"?"? +?rent?+?"元/天?"?+?"載貨:"?+?cargoCapacity?); } } //皮卡類 package?com.imooc;public?class?Pickup?extends?Vehicle?{ @Override public?void?chooseNum(int?num)?{ this.carNum?=?num; //?TODO?Auto-generated?method?stub if(carNum?==?6)?{ carName?=?"皮卡雪6"; rent?=?450; passengerCapacity?=?4; cargoCapacity?=?2; } } @Override public?void?carList()?{ //?TODO?Auto-generated?method?stub System.out.println(carNum?+?".?"?+?carName?+?"?" ? +?rent?+?"元/天?"?+?"載人:"?+?passengerCapacity?+?"?" ? +?"載貨:"?+?cargoCapacity?); }}//執(zhí)行語句
package com.imooc;
import java.util.Scanner;
public class Test {
static int[] carNumList;
static int duration; //總租車天數(shù)
static double[] carCostList; //總租車成本
static Vehicle[] rentList;//租車對象集合
static Vehicle a = new Passengercar();
static Vehicle b = new Passengercar();
static Vehicle c = new Passengercar();
static Vehicle d = new Truck();
static Vehicle e = new Truck();
static Vehicle f = new Pickup();
public static void show() {? ? ? ? ? ? ? ? //打印選車列表
System.out.println("您可租車的類型及其價(jià)格表:");
System.out.println("序號 汽車名稱 租金 容量");
Vehicle showList1 = new Passengercar();//打印客車列表
for(int i = 1; i <= 3; i++) {
showList1.chooseNum(i);
showList1.carList();
}
Vehicle showList2 = new Truck();//打印貨車列表
for(int i = 4; i <= 5; i++) {
showList2.chooseNum(i);
showList2.carList();
}
Vehicle showList3 = new Pickup();//打印皮卡
showList3.chooseNum(6);
showList3.carList();
}
public static void rentCar() { //輸入租車數(shù)據(jù)
System.out.println("請輸入您要租車的數(shù)量:");
Scanner input2 = new Scanner(System.in);
int num = input2.nextInt();
carNumList = new int[num];? ?//保存選車編號
for(int i = 1 ; i <= num ; i++) {
System.out.println("請輸入第" + i + "輛車的序號:");
Scanner input3 = new Scanner(System.in);
int no = input3.nextInt();
carNumList[i - 1] = no;
}
System.out.println("請輸入租車的天數(shù):");
Scanner input4 = new Scanner(System.in);
duration = input4.nextInt();
for(int j = 0; j <= (num - 1); j++) { //建立與其他列表對應(yīng)關(guān)系
switch(carNumList[j]) {
case 1:
a.chooseNum(1);
rentList[j] = a;
carCostList[j] = a.rent;
break;
case 2:
b.chooseNum(2);
rentList[j] = b;
carCostList[j] = b.rent;
break;
case 3:
c.chooseNum(3);
rentList[j] = c;
carCostList[j] = c.rent;
break;
case 4:
d.chooseNum(4);
rentList[j] = d;
carCostList[j] = d.rent;
break;
case 5:
e.chooseNum(5);
rentList[j] = e;
carCostList[j] = e.rent;
break;
case 6:
f.chooseNum(1);
rentList[j] = f;
carCostList[j] = f.rent;
break;
}
}
}
public static void bill(){
System.out.println("您的賬單");
System.out.println("可載人的車為:");
for(Vehicle i : rentList) {
if(i.passengerCapacity != 0) {
System.out.print(i.carName + " ");
}
}
System.out.println("可載貨的車為:");
for(Vehicle i : rentList) {
if(i.cargoCapacity != 0) {
System.out.print(i.carName + " ");
}
}
System.out.println("總租金為:");
double cost = 0;
for(double i : carCostList) {
cost = cost + i;
}
cost = cost * duration;
System.out.println(cost + "元");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("歡迎來到達(dá)達(dá)租車系統(tǒng)!");
Scanner input = new Scanner(System.in);
System.out.println("您是否要租車:1 是 0 否");
int choice = input.nextInt();
if((choice != 1)&&(choice != 0) ) {
System.out.println("輸入有誤!");
}else if(choice == 0) {
System.out.println("歡迎下次光臨!");
}else {
show();//show()必須為靜態(tài)方法才能直接調(diào)用
rentCar();
bill();
}
}
}
2019-06-11
好像是因?yàn)閿?shù)組沒分配空間就賦值才報(bào)錯的?是這個問題嗎