简体   繁体   English

我的while循环进入无限循环,怎么了?

[英]My while loop goes into infinite loop, what's wrong?

I tried to write the BiggerThanPrime program which allows the user to give an input p and the program can find the next closest prime(n) to it such that the value of (np) is minimum. 我尝试编写BiggerThanPrime程序,该程序允许用户提供输入p,并且该程序可以找到与其最接近的素数(n),从而使(np)的值最小。

Here is my code: 这是我的代码:

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++;
        }
    }
}

But the while loop goes into infinite loop. 但是while循环进入了无限循环。

Thus the result is : 因此结果是:

the next bigger prime than 20 is 23
the next bigger prime than 20 is 23
.
.
. 
(infinitely goes on) :(

What am I doing wrong? 我究竟做错了什么?

You need to break the loop once you get the bigger prime number and also you need to increment k in each iteration as below: 一旦获得更大的质数,就需要中断循环,并且还需要在每次迭代中增加k,如下所示:

while( k > p && k< 2*p){
    if(Prime(k)){
        System.out.println("the next bigger prime than "+ p + " is "+ k);
         break;
     }           
    k++; 
}

And also please note as Prime(k) returns a boolean value, It's not necessary again to compare it to true. 并且还请注意,由于Prime(k)返回一个布尔值,因此无需再次将其与true进行比较。

Once you find the next prime, you should break from the loop: 一旦找到下一个素数,就应该摆脱循环:

    while( k > p && k< 2*p){
        if( Prime(k) == true){
            System.out.println("the next bigger prime than "+ p + " is "+ k);
            break;
        } else{
            k++;
        }               
    } 

because you are not incrementing k at each iteration 因为您没有在每次迭代中增加k

Try this : 尝试这个 :

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++;
            }               
        }  

once Prime(k) returns true, you are not changing the value of k, thus loop continues. 一旦Prime(k)返回true,就不会更改k的值,因此循环继续。

when ( Prime(k) == true) it will never increment the value of k. ( Prime(k) == true) ,它将永远不会增加k的值。 So it will go to infinite loop. 因此它将进入无限循环。

you can change the lop in either of this ways 您可以通过以下两种方式之一来更改

way 1: 方法1:

 while( k > p && k< 2*p){
        if( Prime(k) == true){
            System.out.println("the next bigger prime than "+ p + " is "+ k);
        } 
        k++;
    }

Way 2 : 方式二:

 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(); 
            }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM