1

我收到以下错误,我不知道为什么。我尝试查找它,但我没有找到解决方案。

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 1000000 at question2.main(problem2.java:17)

这是我的代码:

//Each new term in the Fibonacci sequence is generated by adding the previous two terms.
//By starting with 1 and 2, the first 10 terms will be:
//1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
//By considering the terms in the Fibonacci sequence whose values do not exceed four million, 
//find the sum of the even-valued terms.

public class problem2 {
    public static void main(String[] args) {
        int []a = new int[1000000];
        a[0] = 1;
        a[1] = 2;
        int sum=0;

        int i=2;
        while(a[i]<=4000000){
            a[i] = a[i-1] + a[i-2];
            i++;
        }

        for(int j=0;j<i;j++){
            sum = sum + a[j];
        }

        System.out.println("The sum is: " + sum);
        System.out.println("\nThere are " + i + " numbers in the sequence.\n");
        System.out.println("This are all the numbers in the sequence:");

        for(int j=0;j<i;j++){
            if(j+1==i){
                System.out.print(a[j] + ".");
                break;
            }

            System.out.print(a[j] + ", ");
        }
    }
}
4

9 回答 9

2

此错误是由于在达到循环终止条件i之前达到 1000000(数组的大小)造成的。a[i]而不是测试你的值,a[i]你应该使用这样的 for 循环:

    for (i=2; i<a.length; i++){
        a[i] = a[i-1] + a[i-2];
    }

此外,您应该使用 typelong而不是 int 的项目,a因为值变得如此之大以至于它们溢出 int 类型并环绕为负值,例如:

1872856136
1063031469
-1359079691
-296048222
-1655127913
-1951176135
688663248

编辑:事实上,使用包含 1000000 个元素的数组甚至 along都不够大 - 如果你真的需要这么大的值,你将不得不使用BigInteger

于 2012-08-17T12:02:58.737 回答
2

问题不在于int[].

您的 while 循环不断检查是否a[i]小于4000000whilei变量已经是前面的一个索引。每个循环都会有a[i] == 0.

此更改将为您修复代码:

int i=1;
while(a[i]<=4000000){
    i++;
    a[i] = a[i-1] + a[i-2];
}
于 2012-08-17T12:10:11.777 回答
1

您的数组a包含 1,000,000 个元素,从零开始。当你在这里循环它们时:

while(a[i]<=4000000)

您超出了索引容量。第一个超出容量的索引是 1,000,000,因此出现错误。

于 2012-08-17T11:58:43.440 回答
1

在您的 while 循环中,您在检查是否完成之前增加 i 。每当评估该条件时,都会对您要计算的元素进行评估- 但那里还没有任何内容。

这意味着您的 while 循环永远不会终止 - 并最终i变为 1,000,000,此时a[i]无法再评估,导致抛出此异常 - 因为中的最后一个元素aa[999999]

您可以通过多种方式解决此问题;最清晰的方法是从 1 开始,并在分配给之前i将其递增。a[i]

顺便说一句,固定大小的数组通常是一个糟糕的选择,而可变大小的列表ArrayList<E>是一个更好的选择——尽管在这种特殊情况下,由于逻辑错误,最终只会导致 OutOfMemoryException。

于 2012-08-17T12:02:17.303 回答
0

问题就在这里

while(a[i]<=4000000)

在你的情况下,你可以这样做

while(i < a.length && a[i]<=4000000 ){
    a[i] = a[i-1] + a[i-2];
    i++;        
}

你的程序将完美运行..

于 2012-08-17T11:59:58.443 回答
0

添加额外条件 while(i < a.length && a[i]<=4000000)

于 2012-08-17T12:01:55.620 回答
0

问题出在while循环中,查看条件

while(a[i]<=4000000)
于 2012-08-17T12:02:37.153 回答
0

正如其他人指出的那样。你可以这样做:

 int i=2;
for(;i<a.length && a[i]<=4000000;i++)
{
     a[i] = a[i-1] + a[i-2];

}

它检查该值是否小于 400000,并且索引应小于 100000。

于 2012-08-17T12:07:12.770 回答
0
import java.util.Scanner;

public class FibonacciSequence {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] a = new int[40000000];
        Scanner b = new Scanner(System.in);
        System.out.println("Enter Two Numbers From Which You Want To Start Fibonacci Sequence.");
        a[0] = b.nextInt();
        a[1] = b.nextInt();
        for(int i = 2; i <= 10; i++) {
            a[i] = (a[i-2] + a[i-1]);
        }
        System.out.println("The fibonacci Sequence is: ");
        int sum = 0;
        for(int j = 0; j <= 10; j++) {
            System.out.println(a[j] + ", ");
            sum += a[j];
        }
        System.out.println("The Sum of the fibonacci sequence: " + sum);
    }

}
于 2021-08-01T11:40:42.650 回答