求问各位大佬,我这个为什么编译器一直抛出数组下标越界的异常?该怎么解决呢
package imooc.two;
import java.util.Scanner;
public class DadaTest {
static Scanner reader = new Scanner(System.in);
static Car []cars = {new CargoCar("比亚迪", "byd", 300, 5),
new CargoCar("奔驰", "ben", 450, 4),
new PassengerCar("奥迪", "A4", 300, 5),
new PassengerCar("大众", "F4", 450, 4),
new CargoAndPassengerCar("宝马", "A4", 300, 5, 3.5),
new CargoAndPassengerCar("雪佛兰", "F4", 450, 4, 2.6)};
/**
* 输出可租汽车信息
*/
public void printCar(){
System.out.println("序号\t汽车名称\t\t租金\t容量");
int i = 1;
for (Car car : cars) {
if(car instanceof CargoCar){
CargoCar car1 = (CargoCar)car;
System.out.println(i+"\t"+car1.name+car1.type+"\t\t"+car1.rentPerDay+"\t"+car1.getCargoCapacity()+"吨");
}
else if(car instanceof PassengerCar){
PassengerCar car1 = (PassengerCar)car;
System.out.println(i+"\t"+car1.name+car1.type+"\t\t"+car1.rentPerDay+"\t"+car1.getSeatCapacity()+"人");
}
else if(car instanceof CargoAndPassengerCar){
CargoAndPassengerCar car1 = (CargoAndPassengerCar)car;
System.out.println(i+"\t"+car1.name+car1.type+"\t\t"+car1.rentPerDay+"\t"+car1.getSeatCapacity()+"人"+" "+car1.getCargoCapacity()+"吨");
}
i++;
}
}
/**
* 主函数
* @param args
*/
public static void main(String[] args) {
//实例化对象
DadaTest test = new DadaTest();
System.out.println("欢迎使用答答租车系统!");
System.out.println("您是否要租车:1-是 0-否");
if(reader.nextInt() == 0)
System.exit(1);
else{
System.out.println("您可租车的类型及其价目表:");
//列出可租车信息
test.printCar();
//输入租车的数量
System.out.println("请输入您要租汽车的数量:");
int num = reader.nextInt();
//输入租车的序号,即选定车辆
int []numArr =new int[num];
for(int i = 1; i <= num; i++){
System.out.println("请输入第"+ i + "辆车的序号:");
numArr[i-1] = reader.nextInt();
}
/*for (int i = 0; i < numArr.length; i++) {
System.out.print(numArr[i] + " ");
}*/
System.out.println("您的账单:");
System.out.println("****可载人的车有:");
test.print1(numArr);
System.out.println("****可载货的车有:");
test.print2(numArr);
System.out.println("****租车总价格:" + test.print3(numArr));
}
}
/**
* 输出载客的汽车名称
* @param arr
*/
public void print1(int []arr){
for (int i = 0; i <= arr.length; i++) {
if(cars[arr[i]-1] instanceof PassengerCar || cars[arr[i]-1] instanceof CargoAndPassengerCar){
PassengerCar tempcar = (PassengerCar)cars[arr[i] - 1];
System.out.print(tempcar.name+tempcar.type+" ");
}
}
System.out.println();
}
/**
* 输出载物的汽车名称
* @param arr
*/
public void print2(int []arr){
for (int i = 0; i <= arr.length; i++) {
if(cars[arr[i] - 1] instanceof CargoCar || cars[arr[i]-1] instanceof CargoAndPassengerCar){
CargoCar tempcar = (CargoCar)cars[arr[i] - 1];
System.out.print(tempcar.name+tempcar.type+" ");
}
}
System.out.println();
}
/**
* 输出总账单金额
* @param arr
* @return
*/
public double print3(int []arr){
double rent = 0;
for (int j = 0; j <= arr.length; j++) {
rent += cars[arr[j] - 1].rentPerDay;
}
return rent;
}
}
package imooc.two;
public class Car {
protected String name;
protected String type;
protected double rentPerDay;
//public int getSeatCapacity(){};
//public double getCargoCapacity(){};
}
package imooc.two;
public class PassengerCar extends Car{
private int seatCapacity; //载货量
public int getSeatCapacity() {
return seatCapacity;
}
public void setSeatCapacity(int seatCapacity) {
this.seatCapacity = seatCapacity;
}
public PassengerCar(String name, String type, double rentPerDay, int seatCapacity){
this.name = name;
this.type = type;
this.rentPerDay = rentPerDay;
this.seatCapacity = seatCapacity;
}
}
package imooc.two;
public class CargoCar extends Car {
private double cargoCapacity; //载货量
public CargoCar(String name, String type, double rentPerDay, double CargoCapacity){
this.name = name;
this.type = type;
this.rentPerDay = rentPerDay;
this.cargoCapacity = CargoCapacity;
}
public double getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
}
package imooc.two;
public class CargoAndPassengerCar extends Car {
private int seatCapacity; //载货量
private double cargoCapacity; //载货量
public CargoAndPassengerCar(String name, String type,
double rentPerDay, int seatCapacity, double cargoCapacity){
this.name = name;
this.type = type;
this.rentPerDay = rentPerDay;
this.seatCapacity = seatCapacity;
this.cargoCapacity = cargoCapacity;
}
public int getSeatCapacity() {
return seatCapacity;
}
public void setSeatCapacity(int seatCapacity) {
this.seatCapacity = seatCapacity;
}
public double getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
}