Java逻辑错误:isPrime

时间:2015-04-22 21:38:21

标签: java

请问我的代码中可能出现的错误是什么,为什么要打印数字而不是只有少于10,000的素数?

public class Isprime {
    public static void main(String[] args) {
        System.out.println("The  prime  numbers  less  that  10000 are:");
        int number = 2;
        printprime(number);

    }

    public static void printprime(int number) {
        int pperline = 10;
        int count = 0;
        while (number < 10000) {
            if (isprime(number)) {
                count++;
            }
            if (count % pperline == 0) {
                System.out.println();
            } else {
                System.out.print(number + ",");
            }
            number++;
        }
    }

    public static boolean isprime(int number) {
        for (int divisor = 2; divisor <= number / 2; divisor++) {
            if (number % divisor == 0)
                return false;
        }
        return true;
    }
}

4 个答案:

答案 0 :(得分:1)

你正在计算一个数字是否为素数,但是你没有在检查它的情况下进行打印。这意味着你的素数是正确的,但是你为每个数字打印一些东西,而不仅仅是素数。

您需要在if (isprime(number))支票内移动打印报表。此外,您只打印一个主要换行符。您应该从格式逻辑中删除else,这样您就不会跳过每10个素数的打印:

public static void printprime(int number) {
    int pperline = 10;
    int count = 0;
    while (number < 10000) {
        if (isprime(number)) {
            count++;
            if (count % pperline == 0) {
                System.out.println();
            }
            System.out.print(number + ",");
        }
        number++;
    }
}

答案 1 :(得分:0)

如果您正确缩进代码:

public static void printprime(int number) {
    int pperline = 10;
    int count = 0;
    while(number < 10000){
        if(isprime(number)){
            count++;
        }
        if (count % pperline == 0) {
            System.out.println();
        }
        else {
            System.out.print(number+",");
        }
        number++;
    }
}

您可以看到它会打印每个数字,除非您选择打印换行符。

if(isPrime(number)) / if之后移动else的结束括号。此外,无论是否需要打印换行符,您都要打印素数,因此请将数字的打印移到else之外(并删除else)。

public static void printprime(int number) {
    int pperline = 10;
    int count = 0;
    while(number < 10000) {
        if(isprime(number)) {
            count++;
            if (count % pperline == 0) {
                System.out.println();
            }
            System.out.print(number+",");
        }
        number++;
    }
}

答案 2 :(得分:0)

我认为错误来自你的第一个&#34; if&#34;。尝试在括号{}

中添加一些内容

答案 3 :(得分:0)

您可能会发现我的解决方案很有帮助,所以我发布了它:)

public class Isprime {
    public static void main(String[] args) {
        System.out.println("The  prime  numbers  less  that  10000 are:\n");
        int number = 2;
        printprime(number);
    }

    public static void printprime(int number) {
        int pperline = 10;
        int count = 0;
        while (number < 10000) {
            if (isprime(number)) {
                count++;
                System.out.print(number + ", "); // consider printing out if and only if
                                                // the number is actually a prime
                                                // your solution is more complicated and you
                                                // also miss some else if statement
                if (count % pperline == pperline - 1) { // results from above approach, now we are printing endline after
                                                        // every 10 numbers
                    System.out.println();
                }
            }
            number++;
        }
    }

    public static boolean isprime(int number) {
        // note that:
        // 1) one is not a prime
        // 2) you can easily prove that you don't have to check all the divisors up to number / 2
        // you can optimize your code a bit by checking divisors up to the square root of number
        for (int divisor = 2; divisor * divisor <= number; divisor++) {
            if (number % divisor == 0)
                return false;
        }
        return number >= 2; // checks if number was negative (negatives are not prime, 0 and 1 are also not prime)
    }

}