Java 将负整数解析为数组时程序出错

Java 将负整数解析为数组时程序出错,java,arrays,parsing,Java,Arrays,Parsing,我的一项作业有一个奇怪的问题。我试图从用户输入中获取整数并将其存储在数组中。之后,将对它们运行四个递归方法,以找到这些数字的不同特征。但是,每当我尝试在任何索引中使用负整数运行程序时,程序都会停止响应 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Assignment9 { public static void main(S

我的一项作业有一个奇怪的问题。我试图从用户输入中获取整数并将其存储在数组中。之后,将对它们运行四个递归方法,以找到这些数字的不同特征。但是,每当我尝试在任何索引中使用负整数运行程序时,程序都会停止响应

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Assignment9 {
    public static void main(String[] args) throws IOException {
        int index = 0;
        int[] numbers;
        numbers = new int[100];
    InputStreamReader inRead = new InputStreamReader(System.in);
    BufferedReader buffRead = new BufferedReader(inRead);
    String line = buffRead.readLine();

    try {
        while (!line.equals("0") && index < 100) {
            numbers[index] = Integer.parseInt(line);
            index++;
            line = buffRead.readLine();

        }
    } catch (IOException exception) {
        System.out.println("Array index out of bound");
    }
`       int min = findMin(numbers, 0, numbers.length - 1);
        int sumAtEven = computeSumAtEvenIndexes(numbers, 0, numbers.length - 1);
        int divByThree = countDivisibleBy3(numbers, 0, numbers.length - 1);
        System.out.println("The minimum number is " + min);
        System.out.println("The sum of numbers at even indexes is " + sumAtEven);
        System.out.println("The count of numbers that are divisible by 3 is " + divByThree);
        System.out.println("The maximum number among numbers that are less than the first number is " + maxLessThanFirst);


    }

    public static int findMin(int[] numbers, int startIndex, int endIndex) {
        if (startIndex == endIndex) {
            return numbers[startIndex];
        } else if (findMin(numbers, startIndex, endIndex - 1) < numbers[endIndex]) {
            return findMin(numbers, startIndex, endIndex - 1);
        } else {
            return numbers[endIndex];
        }

    }

    public static int computeSumAtEvenIndexes(int[] numbers, int startIndex, int endIndex) {
        if (startIndex == endIndex) {
            if (startIndex % 2 == 0) {
                return numbers[startIndex];
            } else return 0;
        } else {
            if (endIndex % 2 == 0) {
                return computeSumAtEvenIndexes(numbers, startIndex, endIndex - 1) + numbers[endIndex];
            } else {
                return computeSumAtEvenIndexes(numbers, startIndex, endIndex - 1);
            }
        }
    }

    public static int countDivisibleBy3(int[] numbers, int startIndex, int endIndex) {
        if (startIndex == endIndex) {
            if (numbers[startIndex] % 3 == 0) {
                return 1;
            } else {
                return 0;
            }
        } else {
            if (numbers[endIndex] == 0) {
                return countDivisibleBy3(numbers, startIndex, endIndex - 1);
            }
            if (numbers[endIndex] % 3 == 0) {
                return countDivisibleBy3(numbers, startIndex, endIndex - 1) + 1;
            } else {
                return countDivisibleBy3(numbers, startIndex, endIndex - 1);
            }
        }
    }

}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
公共课堂作业9{
公共静态void main(字符串[]args)引发IOException{
int指数=0;
int[]数字;
数字=新整数[100];
InputStreamReader inRead=新的InputStreamReader(System.in);
BufferedReader buffRead=新的BufferedReader(读取中);
String line=buffRead.readLine();
试一试{
而(!line.equals(“0”)&&index<100){
数字[索引]=整数.parseInt(行);
索引++;
line=buffRead.readLine();
}
}捕获(IOException异常){
System.out.println(“数组索引超出范围”);
}
`int min=findMin(数字,0,数字.length-1);
int sumAtEven=计算的UMATEVINDEX(数字,0,数字.length-1);
int divByThree=countDivisibleBy3(数字,0,数字.length-1);
System.out.println(“最小数量为”+min);
System.out.println(“偶数索引处的数字之和为”+sumAtEven);
System.out.println(“可被3整除的数字的计数为”+除以3);
System.out.println(“小于第一个数字的数字中的最大数字为”+maxlesthanfirst);
}
公共静态int findMin(int[]数字、int startIndex、int endIndex){
if(startIndex==endIndex){
返回编号[startIndex];
}else if(findMin(numbers,startIndex,endIndex-1)

我相信,这是理解这个问题所必需的代码的唯一相关部分。如果需要额外的代码,请询问。谢谢大家!

替换findMin方法。传递数组,索引为零

public static int findMin(int[] numbers, int index) {
    if (index == numbers.length - 1) 
    {
        return numbers[index];
    }
    else
    {
        return Math.min(numbers[index], findMin(numbers, index + 1));
    }    
}

您的
findMin
方法是双重递归的:

public static int findMin(int[] numbers, int startIndex, int endIndex) {
    if (startIndex == endIndex) {
        return numbers[startIndex];
        // in the next line, we recurse looking for the minimum
    } else if (findMin(numbers, startIndex, endIndex - 1) < numbers[endIndex]) {
        // we've found the minimum, but now we must recurse again to get it!
        return findMin(numbers, startIndex, endIndex - 1);
    } else {
        return numbers[endIndex];
    }
}
现在,对于大小为20的数组,该算法只需要20次调用,对于大小为50的数组,50次调用,对于大小为100的数组,100次调用。您已经递归调用了
findMin
以获取要比较的值,但随后再次调用它以获取要返回的相同值,这一事实应该是一个危险信号。在许多情况下,这种不必要的重复只会是一个小麻烦;在这种情况下,这是灾难性的

哦,我忘了提了。输入负数的原因引发了这个问题:大概你并不总是输入100个数字。当我尝试你的程序时,我从来没有这样做过。这太无聊了,你可以输入一个零来结束这个列表。分配阵列时:

int[] numbers = new int[100];
数组中填充了零。因此,如果您输入十个数字,数组的其余部分将为零,当我们在第一次调用
findMin
时到达这一行:

    if (findMin(numbers, startIndex, endIndex - 1) < numbers[endIndex])
if(findMin(numbers,startIndex,endIndex-1)
最后的数字是零,也就是最小值。所以我们只递归一次,而不是两次。大多数递归调用也会发生这种情况;这将发生在数组末尾零的长尾的所有1上。只有当我们深入到用户实际输入了一个值的条目时,我们才会看到双重递归行为。如果用户只输入大约10个数字,我们就安全了


但是,如果用户输入的是一个负数,那么它就变成了最小值,零也不能保护我们。我们在每次调用时都会执行双递归,除了
numbers[endIndex]
保存负数的调用。仔细想想,我得到2^(x-1)+2^(x-2)-1而不是2^x-1的原因是我总是把负数放在数组的第二个位置。因此,根据最小值的位置,您可以获得比我上面描述的更好甚至更差的性能。

对用户输入的要求过高。为什么不用扫描仪呢?另外,您可以使用ArrayList吗?最后,您需要发布完整的工作代码或至少足够的代码来复制您的问题。@SedrickJefferson我同意。出于某种原因,我只是运行了你发布的代码,没有问题。“停止响应”表示你永远不会离开while循环。您需要使用调试器来找出原因,或者可以打印
    if (findMin(numbers, startIndex, endIndex - 1) < numbers[endIndex])