HYi12
2019-06-11 16:49
程序已经执行完“租车天数”的输入了,但紧接着那一段代码(加粗)一直报错,感觉语法应该没有问题,有朋友能帮我看下是什么原因吗? //父类 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 = "马自达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 ); }}//执行语句
package com.imooc;
import java.util.Scanner;
public class Test {
static int[] carNumList;
static int duration; //总租车天数
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("您可租车的类型及其价格表:");
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() { //输入租车数据
System.out.println("请输入您要租车的数量:");
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("请输入租车的天数:");
Scanner input4 = new Scanner(System.in);
duration = input4.nextInt();
for(int j = 0; j <= (num - 1); j++) { //建立与其他列表对应关系
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("欢迎来到达达租车系统!");
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()必须为静态方法才能直接调用
rentCar();
bill();
}
}
}
好像是因为数组没分配空间就赋值才报错的?是这个问题吗
Java入门第二季 升级版
530644 学习 · 6091 问题
相似问题