0
我一直在試圖推動複合發電機沿着素數發生器工作,但我似乎無法弄清楚如何使用它。每當我將另一節應用於if (isPrime) { count++;
下的代碼時,我會得到一個else如果沒有統計,則整個程序停止工作。對我的問題有任何建議?我如何製作一個複合發電機,輸出的方式與主發電機一樣?
package primegenerator;
import java.util.Scanner;
public class PrimeGenerator{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
double user;`enter code here`
System.out.print(" Enter any number: ");
user = input.nextDouble();
//Saving user's input for generator
final int NUMBER_OF_PRIMES_PER_LINE = 10; //Display 10 per line
int count = 0;
int number = 2;
System.out.println("Your Prime numbers are: \n");
//Reapeatedly find prime numbers
while (count < user) {
//Assume the number is prime
boolean isPrime = true; // Is the current number prime?
//Test whether 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 the for loop
}
}
//Display the Prime number and increase the count
if (isPrime) {
count++; // Increase the count
if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
//Display the number and advance to the new line
System.out.println(number);
//Displays all primes by 10 a line
}
else
System.out.print(number + " ");
}
//Check if the next number is prime
number++;
}
}
}
我讓它產生所有的素數,當你問它,但我無法形成我需要使用的代碼,以允許程序生成複合材料以及素數,但在不同的列表中。 – 2014-10-06 17:56:32