我有一个检查素数的java代码,然后显示它们。但是,在某些情况下没有素数(例如 14 - 17 之间)。在这些情况下,我希望显示一条消息。例如"No primes found"
. 我不知道如何将其添加到我的代码中。
这是我的全班:
import java.io.*;
public class Prime {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
public static int lowerBound;
public static int higherBound;
public void getInput() throws IOException {
System.out.println("Please enter the lower and upper bound");
String line1 = input.readLine();
String line2 = input.readLine();
int lowInput = Integer.parseInt(line1);
int highInput = Integer.parseInt(line2);
lowerBound = lowInput;
higherBound = highInput;
}
public void validatedata() throws IOException {
do {
getInput();
if (lowerBound < 2) {
System.out.println("Finish");
break;
} else if (higherBound < lowerBound) {
System.out
.println("The upper bound should be at least as big as the lower bound.");
}
} while (higherBound < lowerBound);
}
public void prime_calculation() throws IOException {
while ((lowerBound >= 2) && (higherBound > lowerBound)) {
int k;
for (k = lowerBound; k < higherBound; k++) {
boolean primecheck = true;
for (int j = 2; j < k; j++) {
if (k % j == 0) {
primecheck = false;
}
}
if (primecheck)
System.out.println(k);
}
validatedata();
}
}
}
这是我的主要 void 方法:
import java.io.IOException;
public class PrimeUser extends Prime {
public static void main(String argv[]) throws IOException {
Prime isprime = new Prime();
isprime.validatedata();
isprime.prime_calculation();
}
}