Java 而Loop赢了';不要终止?

Java 而Loop赢了';不要终止?,java,do-while,Java,Do While,我使用的是while循环,它不会在应该终止时终止。如果它工作正常,那么当randno为==上限或==下限时,它将终止 循环代码: do { do { randno = (int) (Math.round((Math.random()*(4)) + 0.5)-1); direction = getDirection(randno,heading); } while (robot.look(direction)==IRobot.WALL);

我使用的是while循环,它不会在应该终止时终止。如果它工作正常,那么当
randno
==上限
==下限
时,它将终止

循环代码:

do {
    do {
        randno = (int) (Math.round((Math.random()*(4)) + 0.5)-1);
        direction = getDirection(randno,heading);      
    } while (robot.look(direction)==IRobot.WALL);
    System.out.println(randno);
    System.out.println(highbound);
    System.out.println(lowbound);
    System.out.println("---------------");
} while (randno!=lowbound | randno!=highbound);

输出是
32-----
,或者
23-----
,因此循环应该结束。第一个循环正确结束(我嵌入它们以尝试使其工作…)。出什么事了?

randno=低限|兰诺=上限
始终为真,因为
randno
不能同时等于
下限
上限
(假设它们不相等)

因此,循环永远不会终止

如果您希望在
randno
不同于两个边界时终止,请将您的条件更改为:

while (randno==lowbound || randno==highbound)
while (randno!=lowbound && randno!=highbound)
如果希望在
randno
与某个边界相同时终止,请将条件更改为:

while (randno==lowbound || randno==highbound)
while (randno!=lowbound && randno!=highbound)

编辑:根据您的问题,您需要第二个选项。

这是一个
while
循环,而不是
until
循环,因此它必须不同于下限和上限才能继续循环,而不是or。嘿,非常感谢!我知道这可能是个愚蠢的问题,但我已经盯着它看了好几个小时了。。。