可以運行,但是為什么說Systen.out.println((i+1)+"."+carsForRent[i].toString());出現異常?
public class Rent {
public static void main(String[] args)throws FileNotFoundException {
Car car = new Car();
Car[] carsForRent = {new Manned("奧迪A4",500,4),new Manned("馬自達6",400,4),new MannedTruck("皮卡雪6",450,4,2),new Manned("金龍",800,20),new Truck("松花江",400,4),new Truck("依維柯",1000,20)};
System.out.println("歡迎使用答答租車系統:");
System.out.println("您是否要租車:1是 0否");
Scanner scan = new Scanner(System.in);?
int input = scan.nextInt();
if(input == 1){
System.out.println("您可租車的類型及其價目表:");
System.out.println("序號 \t\t汽車名稱 \t\t租金 \t\t容量\t");
for(int i=0;i<=carsForRent.length;i++){
System.out.println((i+1) +". " + carsForRent[i].toString());
}
}
}
}
}
2019-05-29
樓上說的對,你想想最后一次是不是i=6,因為你carForRent[i]下標是從0開始的,長度是6,,最后一次是carForRent[6],數組下標越界了,ArraysIndexOutOfBoundException
2019-05-28
<= 改成 < ,因為,carsForRent最后一個數據的index是carsForRent.length - 1