2014-10-28 4 views
-3

while 루프를 사용하고 있어야 할 때 종료되지 않습니다. 올바르게 작동하면 randno== highbound 또는 == lowbound 일 때 종료됩니다. 루프While 루프가 종료되지 않습니까?

번호 :

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); 

출력 어느 3 3 2 ------ 또는 2 3 2 ------이므로 루프가 종료한다. 첫 번째 루프가 제대로 끝납니다 (나는 그것을 삽입하여 작동 시키려고합니다 ...). 무슨 일 이니? randno이 (가 동일하지 가정) lowboundhighbound 모두 동일 할 수 없기 때문에

+0

'while' 루프가 아니며'until' 루프가 아니므로 * 또는 *가 아닌 루프를 계속하려면 lowbound * 및 * highbound와 달라야합니다. –

답변

5

randno!=lowbound | randno!=highbound은 항상 사실이다.

따라서 루프가 종료되지 않습니다. 당신이 randno이 조건을 변경, 경계의 것과 동일 할 때 종료하려면

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

: 당신이 randno이 두 경계가 모두 다른 경우 종료하고자하는 경우

은, 당신의 조건을 변경 :

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

편집 : 질문에 따라 두 번째 옵션을 원합니다.

+0

안녕하세요, 정말 고마워요! 나는 아마 어리석은 질문 이었다는 것을 알고있다 그러나 나는 문자로 그것을 몇 시간 봤다 ... – LozzaManiac

관련 문제