我在Java数组中添加项目是错误的吗?

时间:2011-01-12 03:13:19

标签: java arrays

public class ProjectEulerProb2 {

    int firstInt = 1, secondInt = 2, thirdInt = 0, answer = 0;
    int[] array = new int[4000000];
    int[] evenArray = new int[90];

    public static void main(String args[]) {
        ProjectEulerProb2 prob = new ProjectEulerProb2();
        prob.doIt();
        prob = null;

    }

    public void doIt() {

        for (int i = 0; i <= 4000000; i++) {

            if (i == 0) {
                thirdInt = firstInt + secondInt;
            }
            else {
                firstInt = secondInt;
                secondInt = thirdInt;
                thirdInt = firstInt + secondInt;
            }

            array[i] = firstInt;
            array[i + 1] = secondInt;
            array[i + 2] = thirdInt;


            if (thirdInt >= 4000000) {
                break;
            }
        }


        for (int j = 0; j <= 90; j = j + 3) {
            if (j == 0) {
                if (array[j + 1] % 2 == 0) {
                    System.out.println("        " + array[j + 1] % 2 + "        " + array[j + 1]);
                    evenArray[j / 3] = array[j + 1];
                }
                if (array[j + 2] % 2 == 0) {
                    System.out.println("        " + array[j + 2] % 2 + "        " + array[j + 2]);
                    evenArray[j / 3] = array[j + 2];
                }
            }

            if (array[j] % 2 == 0) {
                System.out.println("        " + array[j] % 2 + "      " + array[j]);
                evenArray[j / 3] = array[j];
            }

        }


        for (int u = 0; u < evenArray.length; u++) {
            if (u == 0) {
                answer = evenArray[u];
            }
            else {
                answer = answer + evenArray[u];
            }
        }
        System.out.println(answer);
    }
}

有人可以帮我找到问题吗?每次我打印数组的值时,它都会显示为0而不是指定的值。

编辑:好的,我拿了所有的'System.out.println'  我不需要。

编辑2:好的,所以我重写了代码,不再使用数组了。仍然有兴趣找出我最后一个版本出错的地方。

public class ProjectEulerProb2Other { static int firstInt=1, secondInt=2, thirdInt=0, answer=0;

public static void main(String[] args){

for(int i = 0; i<=4000000;i++){
        if(i==0){
            if(firstInt%2==0){

                answer = answer+firstInt;

            }
            if(secondInt%2==0){

                answer = answer+secondInt;

            }
        thirdInt = firstInt+secondInt;
        }else{
            firstInt = secondInt;
            secondInt = thirdInt;
            thirdInt = firstInt+secondInt;
            if(thirdInt%2==0){

                answer = answer+thirdInt;

            }

        }



    if(thirdInt>=4000000){
        System.out.println(answer);

        break;

    }
}

  }

 }

for(int i = 0; i<=4000000;i++){ if(i==0){ if(firstInt%2==0){ answer = answer+firstInt; } if(secondInt%2==0){ answer = answer+secondInt; } thirdInt = firstInt+secondInt; }else{ firstInt = secondInt; secondInt = thirdInt; thirdInt = firstInt+secondInt; if(thirdInt%2==0){ answer = answer+thirdInt; } } if(thirdInt>=4000000){ System.out.println(answer); break; } } } }

1 个答案:

答案 0 :(得分:2)

向阵列添加元素没有问题。

array[i] = firstInt;

是对的。但是,您的逻辑存在问题。由于这可能是一个功课,我会让你找到它们:))

修改

好的,问题出现在你的第一个版本的循环中:

for (int j = 0; j <= 90; j = j + 3) {
    if (j == 0) { //Bad idea!!
        if (array[j + 1] % 2 == 0) { //Always false. array[0 + 1] == 1
            System.out.println("        " + array[j + 1] % 2 + "        " + array[j + 1]);
            evenArray[j / 3] = array[j + 1];
        }
        if (array[j + 2] % 2 == 0) { //Always true. array[0 + 2] == 2
            System.out.println("        " + array[j + 2] % 2 + "        " + array[j + 2]);
            evenArray[j / 3] = array[j + 2]; 
        }
    }

    if (array[j] % 2 == 0) {
        System.out.println("        " + array[j] % 2 + "      " + array[j]);
        evenArray[j / 3] = array[j];
    }

}

我的修复:

int jCpt = 0; //To add in evenArray in an orderly manner
for (int j = 0; jCpt < 90 && j < 4000000; ++j) { //Changed this
    if (array[j] % 2 == 0) {
        System.out.println("        " + array[j] % 2 + "      " + array[j]);

        evenArray[jCpt++] = array[j]; //We add alement #j from array to evenArray
        /* Equivalent of 
         * evenArray[jCpt] = array[j];
         * jCpt = jCpt + 1; 
         */
    }
}

但那个版本可能更好:

int evenCpt = 0; //To insert the even numbers one after the other
int fibonacciCpt = 0; //To iterate through the fibonacci numbers in array

for (; evenCpt < 90 && fibonacciCpt < 4000000; ++fibonacciCpt) {
    if (array[fibonacciCpt] % 2 == 0)
        evenArray[evenCpt++] = array[fibonacciCpt];
}

恭喜,您解决了问题#2:)