如何使用-1输入关闭我的while循环?

时间:2015-10-24 02:10:31

标签: java while-loop

所以我需要制作一个程序,用户必须猜测数字,直到他做对了。并且他需要能够使用-1变量关闭循环,但我无法弄清楚如何使用我目前对java的知识来做这件事。

这是我需要关闭的循环

    random = generator.nextInt(435) + 456;


    System.out.println("Enter digit between 456 and 890 "); 

    randomUser= scan.nextInt(); 

    while  (randomUser > 1 && randomUser < 456 || randomUser > 891  )
    {
        System.out.println("1Input must be between 456 and 890");

        randomUser= scan.nextInt(); 
    }   



    while (randomUser != random || randomUser!=-1  )
    {

        if (randomUser < random)
        {
            System.out.println("Your guess was lower than the random number please try again");
        }

        else
        {
            System.out.println("Your guess was higher than the random number please try again");
        }

        randomUser= scan.nextInt(); 
        while  (randomUser > 1 && randomUser < 456 || randomUser > 891  )

        {
            System.out.println(" 2Input must be between 456 and 890");
            randomUser= scan.nextInt(); 
        }   

    }
    if (randomUser == random)
    {
        System.out.println("CONGRATULATIONS Your answer is correct!!");
    }
    else 
    {
        System.out.println("you exited the program");`

每当我尝试我所拥有的东西时,我都会得到这个答案:

System.out.println("Your guess was lower than the random number please try again");

我知道为什么我得到它因为我的答案低于随机数,但我无法弄清楚当你输入-1时我需要做什么才能停止。

有人可以帮忙吗?

6 个答案:

答案 0 :(得分:1)

更改

(randomUser != random || randomUser!=-1)

(randomUser != random && randomUser!=-1)

您使用的是逻辑OR ||),它总是会评估为true,因为第一个条件(randomUser != random )仍然满足。逻辑OR之后短路第一个评估为真的条件。

相反,

逻辑AND &&)仅在满足所有条件时进行评估,其中包括 randomUser != -1

答案 1 :(得分:0)

if (randomUser == -1)
return;

应该这样做,或者它的一些变化。把它放在while循环的开头。

答案 2 :(得分:0)

更改此条件:

   while (randomUser != random || randomUser!=-1  )

使用&amp;&amp;

   while (randomUser != random && randomUser!=-1  )

答案 3 :(得分:0)

更改

while (randomUser != random || randomUser!=-1  )

while (randomUser != random && randomUser!=-1  )

原因:即使randomUser == -1,你定义随机的方式也确保-1 == random是永远不会的。因此while循环永远不会以逻辑OR退出。相反,你需要逻辑AND。

答案 4 :(得分:0)

更改

(randomUser != random || randomUser!=-1)

(randomUser != random && randomUser!=-1)

如果while中的逻辑为while(logic),则

true会继续运行。

首先,关注你的逻辑:

(randomUser != random || randomUser!=-1)

true || true == true

true || false == true

false || true == true

false || false == false

只有一个案例会让你的逻辑返回false:

(randomUser != random || randomUser!=-1) == (randomUser == random && randomUser==-1)

这样你的while语句永远不会结束。

答案 5 :(得分:0)

正如其他人所提到的,当输入-1时你的循环没有中断的原因是while条件的错误。

我还建议对代码进行一些清理,如下所示:

random = generator.nextInt(435) + 456;

System.out.println("Enter digit between 456 and 890"); 

while (True) {
    randomUser = scan.nextInt();

    if (randomUser == random) {
        System.out.println("CONGRATULATIONS! Your answer is correct!!");
        break;
    } else if (randomUser == -1) {
        System.out.println("you exited the program");
        break;
    } else {
        if (randomUser < 456 || randomUser > 890) {
            System.out.println("Input must be between 456 and 890");
        } else if (randomUser < random) {
            System.out.println("Your guess was lower than the random number.  Please try again");
        } else {
            System.out.println("Your guess was higher than the random number.  Please try again");
        }
    }
}

这应该在您在问题中指定消息时打印出消息,并在从用户获取输入-1时正确停止循环。只要满足任何终止条件,这也会将控制流返回到循环结束。