Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向Java数组添加项是否错误? 公共类项目EUlerProb2{ int firstInt=1,secondInt=2,thirdInt=0,answer=0; int[]数组=新int[4000000]; int[]evenArray=新int[90]; 公共静态void main(字符串参数[]){ ProjectEulerProb2 prob=新项目EulerProb2(); 问题doIt(); prob=null; } 公共无效doIt(){ 对于(int i=0;i=4000000){ 打破 } } 对于(int j=0;j_Java_Arrays - Fatal编程技术网

向Java数组添加项是否错误? 公共类项目EUlerProb2{ int firstInt=1,secondInt=2,thirdInt=0,answer=0; int[]数组=新int[4000000]; int[]evenArray=新int[90]; 公共静态void main(字符串参数[]){ ProjectEulerProb2 prob=新项目EulerProb2(); 问题doIt(); prob=null; } 公共无效doIt(){ 对于(int i=0;i=4000000){ 打破 } } 对于(int j=0;j

向Java数组添加项是否错误? 公共类项目EUlerProb2{ int firstInt=1,secondInt=2,thirdInt=0,answer=0; int[]数组=新int[4000000]; int[]evenArray=新int[90]; 公共静态void main(字符串参数[]){ ProjectEulerProb2 prob=新项目EulerProb2(); 问题doIt(); prob=null; } 公共无效doIt(){ 对于(int i=0;i=4000000){ 打破 } } 对于(int j=0;j,java,arrays,Java,Arrays,,如何向数组中添加元素没有问题 for(int i = 0; i<=4000000;i++){ if(i==0){ if(firstInt%2==0){ answer = answer+firstInt; } if(secondInt%2==0){ answer = answer+secondInt; }

,如何向数组中添加元素没有问题

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;

    }
}

  }

 }
是正确的。但是,您的逻辑有问题。因为这可能是一个家庭作业,我会让您找到它们:)

编辑

好的,那么问题是在您的第一个版本中的这个循环中:

array[i] = firstInt;

用于(int j=0;j请你整理一下格式,让块排列得更好。你到处都有println,你在说什么?你能整理一下代码吗?就拿你正在处理的例子。值得一提的是,你根本不需要数组来解决这个问题。按顺序计算斐波那契数,然后每次你得到一个偶数,把它加到结果中。不需要记住前面的所有数字,只需要记住前两个,这样你就可以计算下一个。除非老师把它作为家庭作业来布置,否则它不是家庭作业。不过,它通常意味着要独立解决。是的,这不是家庭作业=P。我只是想做一个项目lem.@Shaked_Earth只有14岁。除非他跳过几个年级,否则他在学校里不会被分配这种问题。坚持下去,这对你这个年龄来说太神奇了!我真的不知道我在以前的版本中有什么不合逻辑的地方。好了。如果我的变化不清楚,请随时提问!
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];
}