即使我第二次输入相同的值,do while循环也只会发生一次。为什么?

时间:2020-02-26 16:13:40

标签: java while-loop java.util.scanner

Scanner sc1 = new Scanner(System.in);
        System.out.print("Enter prime p value up to 32 bits: ");
        BigInteger p = sc1.nextBigInteger();
        while(p.isProbablePrime(1) == false && p.compareTo(BigInteger.valueOf(2147483647)) == 1);
        {
            System.out.print("Sorry your p value must be a prime and up to 32bits! Please enter again: ");
            p = sc1.nextBigInteger();
        }

因此,我输入的值大于2147483647,我的while第一次工作,但第二次没有运行。

Enter prime p value up to 32 bits: 2147483659
Sorry your p value must be a prime and up to 32bits! Please enter again: 2147483659
Enter prime q value up to 32 bits:

不满足我的条件时,它会跳到我的q值。

3 个答案:

答案 0 :(得分:1)

尝试改用以下代码(例如,删除do / while):

Scanner sc1 = new Scanner(System.in);
System.out.print("Enter prime p value up to 32 bits: ");
BigInteger p = sc1.nextBigInteger();
while(!p.isProbablePrime(1) || p.compareTo(BigInteger.valueOf(2147483647)) == 1) {
       System.out.print("Sorry your p value must be a prime and up to 32bits! Please enter again: ");
       p = sc1.nextBigInteger();
}

答案 1 :(得分:0)

无论如何,DO循环将始终执行一次。 因此,您的子对象总是评估为false(因此循环停止)。 尝试这样设置

<?php
namespace dealspace_websocket;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        //$this->clients = new \SplObjectStorage;
        $this->clients = [];
    }

    public function onOpen(ConnectionInterface $conn) {
        $cookiesArray = $conn->httpRequest->getHeader('Cookie');

    }
}

那是

public final long c = 2147483647;

while(!p.isProbablePrime(1) || p.compareTo(BigInteger.valueOf(c)) == 1);

答案 2 :(得分:0)

可以使用吗?

public static void main(String[] args) {
    Scanner sc1 = new Scanner(System.in);
    System.out.print("Enter prime p value up to 32 bits: ");
    BigInteger p = sc1.nextBigInteger();
    while (!p.isProbablePrime(1) || p.compareTo(BigInteger.valueOf(2147483647)) > 0)) {
        System.out.print("Sorry your p value must be a prime and up to 32bits! Please enter again: ");
        sc1 = new Scanner(System.in);
        p = sc1.nextBigInteger();
    }
}