双重循环,true校验,只要不走你的break,内外循环会一直循环,外层一次,内层一轮,30秒能跑出来算好的了,没有给你死循环就不错了
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数字n:");
int n = sc.nextInt();
System.out.println("大于" + n + "的最小质数为:" + printPrime(n));
}
public static int printPrime(int n) {
while (true) {
n++;
boolean flag = isPrime(n);
if (flag) {
break;
}
}
return n;
}
public static boolean isPrime(int n) {
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}