我的 while 循环进入无限循环,怎么了?
My while loop goes into infinite loop, what's wrong?
我尝试编写 BiggerThanPrime 程序,它允许用户提供输入 p,程序可以找到下一个最接近它的素数 (n),使得 (n-p) 的值最小。
这是我的代码:
static boolean Prime (int n){
boolean NotPrime = true;
boolean result = true;
for (int i=2; i< n; i++){
NotPrime = (n % i != 0);
result = (result && NotPrime);
}
return result;
}
//Using Betrand's Postulate which states that there always exists at least one prime p s.t.a< p <2a
public static void main(String[] args) {
int p = Integer.parseInt(args[0]);
int k = p+1;
while( k > p && k< 2*p){
if( Prime(k) == true){
System.out.println("the next bigger prime than "+ p + " is "+ k);
} else{
k++;
}
}
}
但是while循环进入无限循环。
因此结果是:
the next bigger prime than 20 is 23
the next bigger prime than 20 is 23
.
.
.
(infinitely goes on) :(
我做错了什么?
一旦找到下一个素数,就应该跳出循环:
while( k > p && k< 2*p){
if( Prime(k) == true){
System.out.println("the next bigger prime than "+ p + " is "+ k);
break;
} else{
k++;
}
}
因为您没有在每次迭代时递增 k
试试这个:
while( k > p && k< 2*p){
if( Prime(k) == true){
System.out.println("the next bigger prime than "+ p + " is "+ k);
}
k++;
}
while( k > p && k< 2*p){
if( Prime(k) == true){
System.out.println("the next bigger prime than "+ p + " is "+ k);
} else{
k++;
}
}
一旦 Prime(k) returns 为真,您就不会更改 k 的值,因此循环继续。
一旦你得到更大的素数,你需要打破循环,并且你需要在每次迭代中递增 k,如下所示:
while( k > p && k< 2*p){
if(Prime(k)){
System.out.println("the next bigger prime than "+ p + " is "+ k);
break;
}
k++;
}
另外请注意 Prime(k) returns 是一个布尔值,没有必要再次将它与 true 进行比较。
when ( Prime(k) == true)
it will never increment the value of k. So
it will go to infinite loop.
您可以通过以下任一方式更改 lop
方式 1:
while( k > p && k< 2*p){
if( Prime(k) == true){
System.out.println("the next bigger prime than "+ p + " is "+ k);
}
k++;
}
方式二:
while( k > p && k< 2*p){
if( Prime(k) == true){
System.out.println("the next bigger prime than "+ p + " is "+ k);
Break;
} else{
k++;
}
}
import java.util.*;
public class index_page
{
public static void main(String[] args) {
Scanner keyB = new Scanner(System.in);
String code = keyB.next();
while (!code.equals("01") || code.equals("02") || code.equals("00") || code.equals("03") || code.equals("04") || code.equals("05")){//Checking for erros in code from user
System.out.println("Invalid code!! Try again");
code = keyB.next();
}
}
}
我尝试编写 BiggerThanPrime 程序,它允许用户提供输入 p,程序可以找到下一个最接近它的素数 (n),使得 (n-p) 的值最小。
这是我的代码:
static boolean Prime (int n){
boolean NotPrime = true;
boolean result = true;
for (int i=2; i< n; i++){
NotPrime = (n % i != 0);
result = (result && NotPrime);
}
return result;
}
//Using Betrand's Postulate which states that there always exists at least one prime p s.t.a< p <2a
public static void main(String[] args) {
int p = Integer.parseInt(args[0]);
int k = p+1;
while( k > p && k< 2*p){
if( Prime(k) == true){
System.out.println("the next bigger prime than "+ p + " is "+ k);
} else{
k++;
}
}
}
但是while循环进入无限循环。
因此结果是:
the next bigger prime than 20 is 23
the next bigger prime than 20 is 23
.
.
.
(infinitely goes on) :(
我做错了什么?
一旦找到下一个素数,就应该跳出循环:
while( k > p && k< 2*p){
if( Prime(k) == true){
System.out.println("the next bigger prime than "+ p + " is "+ k);
break;
} else{
k++;
}
}
因为您没有在每次迭代时递增 k
试试这个:
while( k > p && k< 2*p){
if( Prime(k) == true){
System.out.println("the next bigger prime than "+ p + " is "+ k);
}
k++;
}
while( k > p && k< 2*p){
if( Prime(k) == true){
System.out.println("the next bigger prime than "+ p + " is "+ k);
} else{
k++;
}
}
一旦 Prime(k) returns 为真,您就不会更改 k 的值,因此循环继续。
一旦你得到更大的素数,你需要打破循环,并且你需要在每次迭代中递增 k,如下所示:
while( k > p && k< 2*p){
if(Prime(k)){
System.out.println("the next bigger prime than "+ p + " is "+ k);
break;
}
k++;
}
另外请注意 Prime(k) returns 是一个布尔值,没有必要再次将它与 true 进行比较。
when
( Prime(k) == true)
it will never increment the value of k. So it will go to infinite loop.
您可以通过以下任一方式更改 lop
方式 1:
while( k > p && k< 2*p){
if( Prime(k) == true){
System.out.println("the next bigger prime than "+ p + " is "+ k);
}
k++;
}
方式二:
while( k > p && k< 2*p){
if( Prime(k) == true){
System.out.println("the next bigger prime than "+ p + " is "+ k);
Break;
} else{
k++;
}
}
import java.util.*;
public class index_page
{
public static void main(String[] args) {
Scanner keyB = new Scanner(System.in);
String code = keyB.next();
while (!code.equals("01") || code.equals("02") || code.equals("00") || code.equals("03") || code.equals("04") || code.equals("05")){//Checking for erros in code from user
System.out.println("Invalid code!! Try again");
code = keyB.next();
}
}
}