猿问

java中1到100之间的素数

伙计们,我一直试图弄清楚为什么这段代码在将近 5 个小时内没有显示适当的结果!!!这应该是一项非常简单的任务,但它让我发疯。我必须让项目使用包含的代码而不是另一个类似的代码。我遇到的问题是我的软件底部没有显示素数。根据我的理解,这段代码应该显示 1-100 的质数,并且除了最后一个数字外,所有的数字都是 5 行。非常感谢一些指导。


public class Prime numbers 1-100{

public static void main (String[] args) {


    int number = 100;


    // Assume the number is prime

    boolean isPrime = true; // Is the current number prime?


    // Test if number is prime

    for (int divisor = 2; divisor <= number / 2; divisor++) {

        if (number % divisor == 0) { // If true, number is not prime

            isPrime = false; // Set isPrime to false

            break; // Exit for loop

        }

        // print prime numbers

        if(isPrime)

            System.out.println("Prime numbers between 1 and " + number);

    }

}

}


慕村225694
浏览 203回答 3
3回答

茅侃侃

您的代码应该具有嵌套循环和一个数组,该数组存储要在最后显示的素数。要在最后打印它们,您应该首先打印您的语句,然后使用循环打印包含找到的素数的数组。

桃花长相依

如果你想打印从 1 到 100 的所有质数,那么你必须遍历所有这些数字(第一个循环),并为它们中的每一个迭代所有可能的除数(嵌套循环):int number = 100;boolean isPrime = false;System.out.println("2");System.out.println("3");for (int i = 5; i <= number; i++) {&nbsp; &nbsp; for (int divisor = 2; divisor <= Math.sqrt(i); divisor++) {&nbsp; &nbsp; &nbsp; &nbsp; isPrime = !(i % divisor == 0);&nbsp; &nbsp; &nbsp; &nbsp; if (!isPrime)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; if (isPrime)&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("" + i);}请注意,对于 2 和 3,无法应用条件,因此首先打印它们。我使用了@selbie 的优化提示<= sqrt(number)

精慕HU

我要感谢大家的建议和提示。以及推动我的动力。我能够让程序运行。见下文public class primes {public static void main (String[] args) {&nbsp; &nbsp; final int NUMBER_OF_PRIMES = 26; // Number of primes to display&nbsp; &nbsp; final int NUMBER_OF_PRIMES_PER_LINE = 5; // Display 5 numbers per line&nbsp; &nbsp; int count = 0; // Count the number of prime numbers&nbsp; &nbsp; int number = 1; // A number to be tested for primeness&nbsp; &nbsp; System.out.println("The prime numbers between 1 and 100 are \n");&nbsp; &nbsp; // Repeatedly find prime numbers&nbsp; &nbsp; while (count < NUMBER_OF_PRIMES) {&nbsp; &nbsp; &nbsp; &nbsp; // Assume the number is prime&nbsp; &nbsp; &nbsp; &nbsp; boolean isPrime = true; // Is the current number prime?&nbsp; &nbsp; &nbsp; &nbsp; // Test whether number is prime&nbsp; &nbsp; &nbsp; &nbsp; for (int divisor = 2; divisor <= number / 2; divisor++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (number % divisor == 0) { // If true, number is not prime&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isPrime = false; // Set isPrime to false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break; // Exit the for loop&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Display the prime number and increase the count&nbsp; &nbsp; &nbsp; &nbsp; if (isPrime) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++; // Increase the count&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Display the number and advance to the new line&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(number);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(number + " ");&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //Check if the next number is prime&nbsp; &nbsp; &nbsp; &nbsp; number++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答