2013-10-02 2 views
1

추측 게임 프로그램을 만들려고합니다. 사용자가 전화 번호를 입력하면 전화 번호가 너무 높거나 낮 으면 다시 알려주라는 메시지가 나타납니다. 나는 무한 루프를 만들었고 어떻게 변경해야하는지 알 수 없다. 추측이 잘못되면 프로그램에서 잘못된 값을 확인하고 "잘못된 번호"메시지를 계속 인쇄합니다.무한 사전 테스트 while 루프 (java)

package guessinggame; 
import java.util.Scanner; 
/** 
* 
* @author 
*/ 
public class GuessingGame { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner (System.in); 

     int guesses; //number of users guesses 

     int housePick; //number the user must guess 

     int guess;  //users guess 

     housePick = (int)((Math.random() * 100) +1); 
     //sets housePick to random number from 1 to 100 

     System.out.println("I'm thinking of a number between 1 and 100") ; 
     //print "Im thinking of a nubmer between 1 and 100" 

     System.out.println("Can you guess what it is?"); 
     //print "can you guess what it is" 

     System.out.println 
       ("Enter a number from 1 to 100 (including 1 and 100)"); 
     //prompt user to enter number 

     guess = input.nextInt(); 
     //save entered number as guess 

     while (guess != housePick) //while guess doesnt = housePick... 
     { 
      if (guess > housePick) //and if guess > housePick... 
      { 
       if ((guess - 10) <= housePick) 
        //and if guess is 10 numbers away from housePick... 

       { 
        System.out.println("Close, but too high. Try again."); 
        //print "close but too high, try again" 

       } 
       else    //if guess is not close and guess>housePick... 
       { 
        System.out.println ("Too high, try again."); 
        //then print "Too high, Try again" 
       }       
      } 
      else //If guess<housePick 
      { 
      if ((guess + 10) >= housePick) //AND if guess is close to housePick 
      { 
       System.out.println ("close, but too low.") ; 
       //then print "close, but too low" 

      } 
      else//If guess isnt close to housePick and is less than housePick... 
      { 
       System.out.println ("Too low.");//then print "too low" 
      } 

      } 

     } 



     System.out.println ("You win! It took you " + "guesses."); 
     //If guess = housePick print "Yout win! It took you (# of guesses)" 




    } 
} 

답변

1

사용자 선택을 얻지 않고 while 루프 내에서 추측 변수의 값을 변경하지 마십시오. 추측이 변경되지 않으면 루프는 절대로 변경되지 않으므로 종료되지 않습니다. while (guess != housePick)이며 조건은 false로 유지됩니다.

솔루션 :
명백한를 수행 내부 에서 while 루프를 사용자의 입력을 얻을 수 있도록 입력 스캐너 변수를 사용하여 새 값으로 추측-설정을 다시 사용하십시오.

1

당신은 어떤 수준까지 올랐지 만 일이 잘못되었을 때 루프가 끝나기 전에 사용자로부터 입력을 받아야합니다. 그래서 당신은 동안 루프은 끝났다는 수정 및 업데이트 된 코드 (단지 while 루프 부분을)했을 직전

while (guess != housePick) //while guess doesnt = housePick... 
    { 
     if (guess > housePick) //and if guess > housePick... 
     { 
      if ((guess - 10) <= housePick) 
       //and if guess is 10 numbers away from housePick... 

      { 
       System.out.println("Close, but too high. Try again."); 
       //print "close but too high, try again" 

      } 
      else    //if guess is not close and guess>housePick... 
      { 
       System.out.println ("Too high, try again."); 
       //then print "Too high, Try again" 
      }       
     } 
     else //If guess<housePick 
     { 
     if ((guess + 10) >= housePick) //AND if guess is close to housePick 
     { 
      System.out.println ("close, but too low.") ; 
      //then print "close, but too low" 

     } 
     else//If guess isnt close to housePick and is less than housePick... 
     { 
      System.out.println ("Too low.");//then print "too low" 
     } 

     } 
      /// this is the correction 
      System.out.println 
      ("Enter a number from 1 to 100 again (including 1 and 100)"); 
      //prompt user to enter number 

      guess = input.nextInt(); 
    } 
0

그냥 while 루프 전에 선 아래 추가 다음과 같이 플레이어의 입력을 얻을 수있다 추측이 잘못 될 때마다 묻고 추측이 맞을 때 루프에서 성공적으로 종료되도록 종료됩니다. 다른 사람에 의해 언급 한 바와 같이

while (guess != housePick) //while guess doesnt = housePick... 
    { 
     if (guess > housePick) 
     { 
      \\Your code remains as it is. 
       ... 
       ... 
       ... 
       ... 
     } //if-else loop ends here. 
     guess = input.nextInt(); 
    }//while ends here. 
0

, 당신은 문제가있는 이유는 당신이 단지 확인, 입력 반복되지 않는다는 사실에서 유래한다. input.nextInt()을 while 루프에 넣으면이 문제가 해결됩니다.

또한 프로 시저의 포인트로서 보통 while (항상 한 번 이상 입력을 실행하려는 경우)보다는 do/while 루프를 사용하기에 적절한 위치입니다.