我一遍又一遍地看过这段代码,但不知怎的,我得到''数组越界''。我在一个开始了for循环,因为我想拥有Car 1并且从Car 0开始没有意义。但不知何故,我的阵列越界,我唯一的解决方案是在我的阵列上添加+1,但这不是一个好的解决方案。所以我删除了+1。我应该如何调整代码以使其不受限制?
守则:
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
// Ask the user for maximum speed in km / h (integer) at the measuring site
System.out.print( "What is the maximum speed at the measuring site (km / h)? ");
int MaxSpeed = input.nextInt ();
// Ask the user for the number of cars will be fined
System.out.print( "How many cars have driven too fast? ");
int amountCars = input.nextInt ();
// Create three arrays to store the data about cars
String [] licensePlates = new String [amountCars ];
int [] measureSpeed = new int [amountCars];
double [] fines = new double [amountCars];
for (int i = 1; i <= amountCars; i++) {
Scanner scanner = new Scanner(System.in);
do {
// Ask the user by car to the license plate and the measured speeds
System.out.println ( " ");
System.out.println ( "Car " + i);
System.out.print ( "License plate: ");
licensePlates[i] = scanner.nextLine ();
System.out.print ( "measured speed: ");
measureSpeed[i] = scanner.nextInt ();
if (measureSpeed [i] <MaxSpeed) {
System.out.println ( "Please enter a speed above the speed limit.");
}
} while (measureSpeed [i] <MaxSpeed); {
}
}
}
}