我有一个递归运行的数组,它找到数组上的最小数字。我运行程序并在 (Assignment9.java:36) if (previousMin > numbers[endIndex]) 和 (Assignment9.java:20) double min = findMin(numbers, 0, numbers.length); 上收到 ArrayIndexOutOfBoundsException 错误;我知道为什么通常会出现这个问题,但我找不到我的代码的修复程序。我不知道我的实际代码是否有效,因为我无法运行该程序。有什么建议么..
import java.io.*;
import java.text.*;
public class Assignment9
{
public static void main(String[] args) throws IOException
{
int [] numbers = new int[100];
InputStreamReader streamR = new InputStreamReader(System.in);
BufferedReader inFile = new BufferedReader(streamR);
String reader = inFile.readLine();
double min = findMin(numbers, 0, numbers.length);
System.out.print ("The minimum number is " + min + ('\n'));
}
public static int findMin (int [] numbers, int startIndex, int endIndex)
{
if (startIndex == endIndex)
{
return numbers[startIndex];
}
else
{
double previousMin = findMin (numbers, startIndex, endIndex - 1);
if (previousMin > numbers[endIndex])
return numbers[endIndex];
else
return numbers[endIndex];
}
}