简体   繁体   中英

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.

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.

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:

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.

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

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.

when ( Prime(k) == true) it will never increment the value of k. So it will go to infinite loop.

you can change the lop in either of this ways

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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