我一直在这个基本的java程序,当我需要5用户输入的值存储到一个数组,将其发送到的方法,并找到和显示的最低值。
该方案是很简单的,它运行,但是当我进入最后一个号码,我得到的错误:
例外在线程 “主” java.lang.ArrayIndexOutOfBoundsException:在minNumber.main 4(minNumber:14)
救命?
import java.util.*;
class minNumber {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int numberArray[] = new int[4];
int findLowest;
for (int i = 0; i <= numberArray.length; i++){
System.out.println("Enter a value for slot "+(i+1)+ ":");
numberArray[i] = input.nextInt();
}
findLowest = getMin(numberArray);
displayOutput(findLowest);
}
public static int getMin(int num[]){
int lowestNum = 0;
for (int j = 0; j <= num.length; j++){
if (num[j] < num[j+1]){
lowestNum = num[j];
}
}
return lowestNum;
}
public static void displayOutput(int lowest){
System.out.println("The lowest number is: "+lowest);
}
}