2017-10-12 5 views
-1

아래 코드는 Java에서 2 개의 난수를 생성해야하는 게임에서 가져온 것으로 사용자에게 그 중 하나가 표시됩니다. 그런 다음 두 번째 숫자가 첫 번째 숫자보다 높거나 낮은 지 추측해야합니다. while 루프를 작성하여 입력에 오류가 있는지 확인하고 조건이 참이 아닐지라도 루프가 실행됩니다. H를 입력하면 자신의 추측이 다음 번호가 더 높은 것을 의미하고, L은 그 낮은Java while 루프가 입력 검사와 함께 작동하지 않습니다.

// Input 
System.out.println ("Do you think the second number is higher or lower (enter 'H' or 'L' respectively): "); 
char userGuess = Character.toUpperCase(input.next().charAt(0)); 

// Input error check (and immediate output of second number) 
while (userGuess != 'H' || userGuess != 'L') { 
    System.out.println ("Sorry, I didn't understand that. Please try again: "); 
    userGuess = Character.toUpperCase(input.next().charAt(0));  
} 
System.out.println ("The second number was " + secondRandomNumber); 
+0

@ user2023608 감사합니다! – developer067

답변

2

사용 & &이 || 믿지 아니하는 것을 의미한다. 물론 H가 아니거나 L이되지 않을 것입니다.

while (userGuess != 'H' && userGuess != 'L') { 
    System.out.println ("Sorry, I didn't understand that. Please try again: "); 
    userGuess = Character.toUpperCase(input.next().charAt(0));  
}