简体   繁体   English

为什么我的质数java程序不打印出质数?

[英]Why does my prime number java program not print out prime numbers?

static void primeNumbers(int n)
    {
         System.out.println("All the prime numbers up to "+n+" are -->\n");

        boolean isPrime = true; 
        for(int i = 2; i < n; i++)
        {
            for (int j = 1; j < i; j++)
            {
                if(i % j == 1)
                {
                    isPrime = true; 
                    System.out.print(j);
                }               

                if(i % j == 0)
                {
                    isPrime = false; 
                    System.out.print(" ");
                }
            }
        }

    }

In this code I am trying to list all the prime numbers up to 'n' which is the number the user inputs.在这段代码中,我试图列出直到“n”的所有质数,这是用户输入的数字。 I am confused as to how to fix this to produce all of the prime numbers up to n.我很困惑如何解决这个问题以产生直到 n 的所有素数。

You want to test if i is dividable by any number in the range of [2..i-1] and if it is, then it is not a prime number.您想测试i是否可被 [2..i-1] 范围内的任何数字整除,如果是,则它不是素数。 Your j -loop starts at 1, that is the first error.您的j循环从 1 开始,这是第一个错误。

Knowing when a number is dividable by another is tested like this: if (i % j == 0) { (if the division remainder is zero) where in your code you test for equality with 1 instead.知道一个数字何时可被另一个数字整除的测试如下: if (i % j == 0) { (如果除法余数为零)在您的代码中,您测试是否与 1 相等。

Third thing is that you make your decision in the first iteration (when j is 1) and always print something in your inner loop.第三件事是您在第一次迭代中做出决定(当 j 为 1 时)并始终在内部循环中打印一些内容。 You need to take the logic out of the inner loop.您需要将逻辑从内部循环中取出。 Only if i is not dividable by any of the j s then it is prime.只有当 i 不能被任何j整除时,它才是素数。

Here is a modified version of your code:这是您的代码的修改版本:

static void primeNumbers(int n) {
    System.out.println("All the prime numbers up to "+n+" are -->\n");

    for (int i = 2; i < n; i++) {
        boolean isPrime = true;
        for (int j = 2; j < i; j++) {
            if(i % j != 0) {
                isPrime = false;
            }
        }
        if (isPrime) {
            System.out.print(i, " ");
        }
    }
}
if (i % j == 0) {
    isPrime = false;
    System.out.print(" ");
}

This if statement is always true for any i because j starts at 1. As a result, you print a space for every number < n and >= 2这个 if 语句对于任何i始终为真,因为j从 1 开始。因此,您为每个数字 < n和 >= 2打印一个空格

Also, isPrime may be an unnecessary variable.此外, isPrime可能是一个不必要的变量。

Your algorithm, although slow, seems to be correct on most parts except for the fact that you are not handling the boundary conditions well.您的算法虽然很慢,但似乎在大多数部分都是正确的,除了您没有很好地处理边界条件这一事实。 Eg 2 is a prime number, you are running iterations only till n-1, you are not reseting the isPrime flag for every number, and a few small things.例如 2 是一个质数,你只运行迭代到 n-1,你没有为每个数字重置 isPrime 标志,还有一些小事情。 Here's a revised working version.这是一个修订后的工作版本。 Try to compare it with yours and understand where you didn't get it right.尝试将其与您的进行比较,并了解您哪里做对了。

    int n = 10;
    System.out.println("All the prime numbers up to "+n+" are -->\n");

    boolean isPrime = true; 
    for(int i = 2; i <= n; i++)
    {
        for (int j = 2; j < i; j++)
        {   
            if(i % j == 0)
            {
                isPrime = false; 
                break;
            }
        }
        if(isPrime) {
            System.out.println(i);
        }
        isPrime = true;
    }

Don't re-set isPrime to true in your second for loop, as soon as the number is not prime once, we know it is not prime forever.不要在第二个 for 循环中将isPrime重新设置为 true,一旦该数字不是素数,我们就知道它永远不是素数。 Just set it to true once every iteration of the outer loop, and do your final "Is prime" check outside.只需在外循环的每次迭代中将其设置为 true 一次,然后在外面进行最后的“是素数”检查。

static void isPrime(int n)
    {
         System.out.println("All the prime numbers up to "+n+" are -->\n");

        for(int i = 2; i <= n; i++)
        {
            boolean isPrime = true;
            for (int j = 2; j < i; j++)
            {

                if(i % j == 0)
                {
                    isPrime = false;
                    System.out.println(i + " is not prime because " + i + " is divisible by " + j );
                    break;
                }
            }
            if (isPrime){
              System.out.println(i + " is prime.");
            }
        }

    }

I've added a couple prints to this code so that you can see why each part of the logic is doing what it does -- compare it to your code so that you can understand why yours doesn't actually search for primes.我在此代码中添加了一些打印件,以便您可以了解为什么逻辑的每个部分都在做它所做的事情 - 将它与您的代码进行比较,以便您了解为什么您的代码实际上并不搜索素数。

I think is a logical problem in your script.我认为这是您脚本中的逻辑问题。 You should count with a loop all the number from 2 to n-1 which are = to 0 with a test你应该用循环计算从 2 到 n-1 的所有数字,这些数字是= 0 并通过测试

n%i = 0

If the counter = 0 then no divider then prime number如果计数器 = 0,则没有除法器,则为素数

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

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