2013-10-03 2 views
0

내 자바 추측 게임 프로그램에서 내가 뭘 잘못하고 있는지 파악하지 못했습니다. 컴퓨터는 1에서 100 사이의 숫자를 선택하고 사용자는 그것을 추측하도록 요청됩니다. 사용자가 너무 낮거나 너무 높게 물어보고 올바른 때까지 다시 추측하도록 요청합니다. 제 문제는 숫자가 맞을 때 항상 너무 낮게 말할 것입니다. 그러나 같은 번호를 다시 입력하면 올바르다 고 말할 것입니다. 당신은 위의 조건 틀렸어자바 추측 게임이 실행되지만 올바르게 실행되지 않습니다.

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 

     guesses = 0; 

     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 

     System.out.println("test " +housePick); 
     //Test: tells user the correct answer 
     do 
     { 
      guess = input.nextInt(); 

      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" 

        guesses = guesses+ 1 ; 



       } 

       else    //if guess is not close and guess>housePick... 
       { 
        System.out.println ("Too high, try again."); 
        //then print "Too high, Try again" 

        guesses = guesses+ 1; 


       }       
      } 
      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" 

       guesses = guesses + 1; 

      } 
      else//If guess isnt close to housePick and is less than housePick... 
      { 
       guesses = guesses+ 1; 

       System.out.println ("Too low.");//then print "too low" 
      } 

      } 

     }while (guess != housePick); //while guess doesnt = housePick... 

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

    guesses = guesses + 1; 

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




    } 
} 
+0

당신은 그것을 디버깅하는 무슨 짓을 그것을 삭제를 ??? –

답변

2
else //If guess<housePick 

, 그것은 guess <= housePick에 해당합니다. 그것은 housePick == guess 그래서, 당신은 단순히 당신 승리 말할 수 guess = input.nextInt()을하는 지점이없는 때 실행되는 코드의 블록에 따라, 또한

else if(guess < housePick) 

해야합니다.

}while (guess != housePick); //while guess doesnt = housePick... 

    // At this point, guess == housePick, why ask for input again???? 

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

    //guesses = guesses + 1; 
+0

와우. Cant는 그걸 알아낼 수 없다고 생각합니다. – user2809114

+2

@ user2809114 이런 일이 일어납니다. 해피 코딩 :) – TheKojuEffect

0

는 볼 수

Housepick = 87

추측 = 87

if ((guess + 10) >= housePick) //AND if guess is close to housePick 
{ 
     System.out.println ("close, but too low.") ; 
     //then print "close, but too low" 

     guesses = guesses + 1; 

} 

흠 87 + 10 87

초과 한 후는 DO/잠시 휴식 반복하고 다시 묻습니다.

1

는 또한 잠시 후 추가 input.nextInt()이 있습니다

}while (guess != housePick); //while guess doesnt = housePick... 

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

관련 문제