Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的while循环进入无限循环,什么';怎么了?_Java_While Loop_Primes - Fatal编程技术网

Java 我的while循环进入无限循环,什么';怎么了?

Java 我的while循环进入无限循环,什么';怎么了?,java,while-loop,primes,Java,While Loop,Primes,我试图编写BiggerThanPrime程序,允许用户输入p,程序可以找到下一个最接近的prime(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 = (resul

我试图编写BiggerThanPrime程序,允许用户输入p,程序可以找到下一个最接近的prime(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( k > p && k< 2*p){
        if( Prime(k) == true){
            System.out.println("the next bigger prime than "+ p + " is "+ k);
            break;
        } else{
            k++;
        }               
    } 
while(k>p&&k<2*p){
如果(素数(k)=真){
System.out.println(“比“+p+”大的下一个素数是“+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++;
        }               
    } 
while(k>p&&k<2*p){
如果(素数(k)=真){
System.out.println(“比“+p+”大的下一个素数是“+k”);
打破
}否则{
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){
如果(素数(k)=真){
System.out.println(“比“+p+”大的下一个素数是“+k”);
}           
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){
如果(素数(k)=真){
System.out.println(“比“+p+”大的下一个素数是“+k”);
}           
k++;
}
while(k>p&k<2*p){
如果(素数(k)=真){
System.out.println(“比“+p+”大的下一个素数是“+k”);
}否则{
k++;
}               
}  
一旦Prime(k)返回true,就不会更改k的值,因此循环将继续。

while(k>p&&k<2*p){
如果(素数(k)=真){
System.out.println(“比“+p+”大的下一个素数是“+k”);
}否则{
k++;
}               
}  

一旦素数(k)返回true,就不会更改k的值,因此循环将继续。

一旦获得较大的素数,就需要中断循环,并且还需要在每次迭代中增加k,如下所示:

while( k > p && k< 2*p){
    if(Prime(k)){
        System.out.println("the next bigger prime than "+ p + " is "+ k);
         break;
     }           
    k++; 
}
while(k>p&&k<2*p){
if(素数(k)){
System.out.println(“比“+p+”大的下一个素数是“+k”);
打破
}           
k++;
}

另外请注意,由于素数(k)返回一个布尔值,因此无需再次将其与真值进行比较。

一旦获得较大的素数,您需要中断循环,并且还需要在每次迭代中增加k,如下所示:

while( k > p && k< 2*p){
    if(Prime(k)){
        System.out.println("the next bigger prime than "+ p + " is "+ k);
         break;
     }           
    k++; 
}
while(k>p&&k<2*p){
if(素数(k)){
System.out.println(“比“+p+”大的下一个素数是“+k”);
打破
}           
k++;
}
还请注意,由于Prime(k)返回一个布尔值,所以不必再次将其与true进行比较

(素数(k)=true)
时,它将永远不会增加k的值。所以 它将进入无限循环

你可以用这两种方法中的任何一种来改变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++;
        }
  }
while(k>p&&k<2*p){
如果(素数(k)=真){
System.out.println(“比“+p+”大的下一个素数是“+k”);
} 
k++;
}
方式2:

 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++;
        }
  }
while(k>p&&k<2*p){
如果(素数(k)=真){
System.out.println(“比“+p+”大的下一个素数是“+k”);
打破
}否则{
k++;
}
}
(素数(k)=true)
时,它将永远不会增加k的值。所以 它将进入无限循环

你可以用这两种方法中的任何一种来改变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++;
        }
  }
while(k>p&&k<2*p){
如果(素数(k)=真){
System.out.println(“比“+p+”大的下一个素数是“+k”);
} 
k++;
}
方式2:

 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++;
        }
  }
while(k>p&&k<2*p){
如果(素数(k)=真){
System.out.println(“比“+p+”大的下一个素数是“+k”);
打破
}否则{
k++;
}
}

好吧,它不会改变k或p。。。所以
k>p&&kLooking你的问题,(n-p)在n=2时是最小值。我怀疑这不是你想要回答的问题。从您的代码中,它似乎是“下一个大于p的素数是什么”。你的“最近”可能低于p。最接近19的素数是17;19之后的下一个大素数是23。确保你回答了正确的问题。好吧,它不会改变k或p。。。所以
k>p&&kLooking你的问题,(n-p)在n=2时是最小值。我怀疑这不是你想要回答的问题。从您的代码中,它似乎是“下一个大于p的素数是什么”。你的“最近”可能低于p。最接近19的素数是17;19之后的下一个大素数是23。确保你回答了正确的问题。