2012-04-23 62 views
0

好的,這是猜謎遊戲完整代碼的一部分。找不到猜錯遊戲類錯誤

public static void Game(){    //Second page of game with option of continue playing when guessed close enough to answer. 
    Scanner scan = new Scanner(System.in); 

    GuessingGame testgame=new GuessingGame(); 

    testgame.Generator(); 
    int num = testgame.GetGenNum(); 
    //Produces a random number between 1 and 100 inclusive of 1 and 100 


    System.out.println("Guess the target integer which is between 1 and 100 inclusive"); 

    int guess=scan.nextInt(); 

    int difference=(guess-num); 



    while(guess!=num){ 
     int yesCounter=0; 

     System.out.println("Guess again " +num+" "+difference); 
     guess=scan.nextInt(); 
     difference=(guess-num); 


     ///something wrong here, shouldnt repeat if difference too big 
     if(difference<=5||difference>=-5){ //something wrong with the condition, it says its close enough even tho it isnt.  
     while(yesCounter<1){ 
      System.out.println("Close enough, do you want to keep Guessing? Y/N "); 
      yesCounter++; 


      String play = scan.nextLine(); 
      //play=play1; 
      while(!(play.equalsIgnoreCase("y"))) 
      { 


       if(play.equalsIgnoreCase("n")){ 
        System.exit(1); 
       } 

       else{ 
        if((play.equalsIgnoreCase("y"))){ 
         invalid(); 
         guess=scan.nextInt(); 
        } 
        else{ 
         Game();    ///TAKE note as it might restart the game with new random integer 
        } 
       } 

      } 
     } 

    } 

    } 

輸出爲:

.........................

玩? Y/N

ý

猜測目標整數足夠接近1和100(含)

之間

再次44 6

,你想繼續猜測? Y/N

猜測目標整數是1和100(含)

之間 ..........................

問題是,當用戶猜測一個數字,條件是如果猜測和生成的數字之間的差異是5或更小,告訴用戶它足夠接近,並詢問用戶是否想繼續猜測,但條件wasn沒有實現,但仍然運行,有人可以幫忙嗎?

回答

0
while(!(play.equalsIgnoreCase("y"))) 
     { 


      if(play.equalsIgnoreCase("n")){ 
       System.exit(1); 
      } 

      else{ 
       if((play.equalsIgnoreCase("y"))){ 
        invalid(); 
        guess=scan.nextInt(); 
.... 

這是不對的,if((play.equalsIgnoreCase("y")))永遠是真實的,因爲它循環中,明確地不能進入如果這是真的。這就是你的問題所在,它總是會重新啓動遊戲,因爲它在else分支中調用Game()。總之,這是你要做的:

boolean _true = true; 

while(! _true) { 

    if(_true) { 
     //impossible 
    } 
    else { 
     Game(); //ALWAYS 
    } 
} 

既然你標記的功課你的問題,我不會給予充分的修正,但現在你知道它在哪裏出了問題,你應該能夠找出你需要改變以提前:)

+0

非常感謝!即時看着它現在。 – Liquified 2012-04-23 17:32:24

+0

還有一個問題,while條件之前,有一條線路沒有工作,String play = scan.nextLine();命令根本不起作用,因爲我還沒有發現的原因,並且讓我瘋狂...... – Liquified 2012-04-23 17:42:36

-1

你混合(注:您使用(difference<=5||difference>=-5)的條件總是true)。

if (Math.abs(difference)<=5) { ... } # difference is 5 or less 

if (Math.abs(difference)>=5) { ... } # difference is 5 or more 

RESP:如果您使用Math.abs(...)相反,你可以使用任何的下列

if (difference<=5 && difference>=-5) { ... } # difference is 5 or less 

if (difference<=-5 || difference>=5) { ... } # difference is 5 or more 

更好的可讀性。

+0

謝謝!它看起來非常清晰,當有人向我解釋它哈哈xD – Liquified 2012-04-23 17:28:05

-1

if(difference<=5||difference>=-5)這表示如果差值小於5或大於-5。所有數字都小於5或大於-5,所以這是總是爲真。

我假設你想要的是類似if(Math.abs(difference)<=5)的東西。這會告訴你,如果你的差變量的絕對值小於或等於5

+0

這不會解決它,只是一小部分代碼相同的替代... – MarioDS 2012-04-23 17:16:32

+1

當然可以嗎?他的代碼對所有的數字都是真的,我的代碼不是。或者我只是失去它? :) – Jon7 2012-04-23 17:18:23

+0

nope,該聲明應該工作(除非**我**正在失去它)),但是你應該**使它成爲'&&'而不是'||'。但Jon7認爲這個特定的if語句更好。 – MarioDS 2012-04-23 17:23:37