から割り当てを「0」とのループを終了しようとすると、どのようなユーザ入力から素数を取得しようと、私は0ユーザー
/*
Natasha Shorrock
Assignment A4
10/20/16
*/
package a4;
import java.util.Scanner;
public class MainApp {
static boolean isPrimeOptimized(int candidate) {
if (candidate < 2) {
return false;
}//if
if (candidate % 2 == 0) {
//divisble by 2 with no remainder
//is not prime
return false;
}//if
int divisor = 3;
while (divisor < candidate/2) {
if (candidate % divisor == 0) {
return false;// this is not prime
}//if
divisor = divisor + 2;
}//while
return true;
}//isPrime
public static void main(String[] args) {
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 numbers or zero to exit: ");
//ask the user for low and high
//range of [prime numbers to generate
int low = sc.nextInt();
int high = sc.nextInt();
int candidate = low;
int count = 0;
while (candidate <= high) {
if (isPrimeOptimized(candidate)) {
//if prime print
count = count + 1;
System.out.println();
System.out.println(count + " " + candidate);
}//if
candidate = candidate + 1;
}//while
}//while (true)
}//class
}//classMainApp
彼女は1つの番号しか求めていないので、「0」を入力した場合は「low」だけが入力され、「high」を待機します。したがって、2つのスキャンの間にあるべきである場合は、「低」のみを確認する必要があります –