简体   繁体   English

我找不到返回素数数组的java方法中的错误。

[英]I can't find the bug in my java method that returns an array of prime numbers.

It only uses the prime numbers to check if the other numbers are also prime 它仅使用质数检查其他数字是否也是质数

static public int[] primeGen(int a){

    int[] series={2};

    if (a==1 || a==2 || a<=0){
        return series;
    }   

this is where the errors occur 这是发生错误的地方

    else{

        boolean Prime = false;

        for (int i = 3; i<=a; i++){

            boolean[] state = {};

            for (int j = 0; !(state[state.length-1]) && (j<series.length); j++){
                state = Arrays.copyOf(state, state.length +1);
                state[state.length -1] = i % series[j] ==0;
            }

            for (int k = 0; (Prime) && (k<state.length); k++){
                Prime = !(state[k]);
            }

            if (Prime){
                series = Arrays.copyOf(series, series.length +1);
                series[series.length -1] = i;
            }
        }
        return series;
    }
} 

Sorry if I just made a rookie mistake, cause I've been learning Java for 3 days now 抱歉,如果我刚遇到菜鸟错误,因为我已经学习Java 3天了

您的state数组被初始化为空数组,所以!(state[state.length-1])试图访问该数组的无效索引(-1)。

This line 这条线

for (int k = 0; (Prime) && (k<state.length); k++){

means that the loop will only be executed if Prime is true. 表示仅当Prime为true时才执行循环。 However you have initialized Prime to false and only set it true inside the loop. 但是,您已将Prime初始化为false,并且仅在循环内将其设置为true。

It looks like you are using Arrays.copyOf to increase the size of your output array. 看起来您正在使用Arrays.copyOf来增加输出数组的大小。 You can just use Vector.add() instead and the JVM will take care of any resizing needed. 您可以只使用Vector.add()来代替,JVM将负责任何所需的调整大小。

Now it's working fine! 现在一切正常!

My mistake was that I didn't know that the for loop doesn't start at all if the statement in the middle for(int j =0; (Prime)&&(j<) ; j++){} is't true. 我的错误是,如果中间的for(int j = 0; (Prime)&&(j <) ; j ++){}是不正确的,我根本不知道for循环根本不会开始。 What I thought was that this would be checked only at the end of each repetition. 我想这是只有在每次重复结束时才进行检查。 Anyway, I wouldn't have realized this without u guys pointing out that the statements don't add up, so thanks a lot! 无论如何,如果没有你们指出这些语句不会加起来,我就不会意识到这一点,非常感谢!

        boolean Prime = false;

        for (int i = 3; i<=a; i++){

            boolean[] state = {};

            for (int j = 0; (j<series.length); j++){
                state = Arrays.copyOf(state, state.length +1);
                state[state.length -1] = i % series[j] == 0;
       //This is where I added my statement that breaks the loop
                if (!(state[state.length-1]==false&&j<series.length)){break;}
            }

            for (int k = 0; (k<state.length); k++){
                Prime = !(state[k]);
       //As well as here
                if (!(Prime==true&&k<state.length)){break;}
            }

            if (Prime == true){
                series = Arrays.copyOf(series, series.length +1);
                series[series.length -1] = i;
            }
        }
        return series;
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM