2014-12-07 51 views
0

我無法弄清楚如何讓遊戲重新啓動。我想我需要把它放在System.out.println(「好吧,讓我們再玩一次」)。 我需要在那裏重新啓動它?我錯過了什麼?一旦猜到數字,我該如何讓這個數字猜測遊戲重新啓動?

import java.util.Random; 
import java.util.Scanner; 

public class GuessMyNumber { 

    public static void main(String[] args) { 

     Random Bob = new Random(); 
     int NumToGuess = Bob.nextInt(20); 
     int NumOfTries = 0; 
     Scanner Bill = new Scanner(System.in); 
     int guess; 
     boolean win = false; 

     while (win == false) { 

      System.out.println("I'm thinking of a number between 1 and 20, can you guess it?"); 
      guess = Bill.nextInt(); 
      NumOfTries++; 

      if (guess == NumToGuess) { 
       win = true; 
      } 
      else if (guess < NumToGuess) { 
       System.out.println("Your guess is too low"); 
      } 
      else if (guess > NumToGuess) { 
       System.out.println("Your guess is too high"); 
      } 
     } 

     System.out.println("Congratulations!"); 
     System.out.println("The number was " + NumToGuess); 
     System.out.println("It took you " + NumOfTries + " tries"); 

     while (win == true) { 
      String YesOrNo; 
      YesOrNo = Bill.nextLine(); 
      System.out.println("Would you like to play again? " + YesOrNo); 
      YesOrNo = Bill.nextLine(); 

      if (YesOrNo.equalsIgnoreCase("yes")) 
      { 
       System.out.println("Ok, let's play again."); 
      } 
      else 
      { 
       System.out.println("Ok, maybe next time."); 
      } 
     } 
    } 
} 
+0

你缺少你在永恆的循環停留,而不是回到計劃流程的開始。 – EpicPandaForce 2014-12-07 21:44:02

+0

@ crpittman18順便說一句,'''Bob.nextInt(20)''產生一個介於0(含)和20(不含)之間的數字。 – mezzodrinker 2014-12-07 21:51:33

回答

0

你的第二個循環是問題所在。您可能會複製第一個循環。在第二個循環中,您設置了YesOrNo而不是win。所以它會永遠循環。

while (win == true) {   //You test win 
     String YesOrNo; 
     YesOrNo = Bill.nextLine() //You set YesOrNo 
     ... 
    }        //You never set win to anything new so it loops forever 

解決重啓的問題的典型方法是使用一個循環而不是兩個,並讓用戶輸入-1,當他們想戒菸。這樣遊戲一直保持勝利或失敗,直到完成。

順便說一下,我們在java中使用camelCase,所以YesOrNo應該是yesOrNo但這只是一個樣式問題。

0

我會將整個方法體包裝成一個while循環,用於檢查標誌(例如boolean continueGame)是否爲true。我的意思是這樣的:

import java.util.Random; 
import java.util.Scanner; 

public class GuessMyNumber { 
    public static void main(String[] args) { 
     Random Bob = new Random(); 
     Scanner Bill = new Scanner(System.in); 
     int guess; 
     boolean continueGame = true; // the flag (boolean) that allows us to check if the user wants to continue the game or not 

     while(continueGame) { 
      boolean win = false; 
      int NumToGuess = Bob.nextInt(20); // moved into the while loop because they need to be initialized every time you start the game 
      int NumOfTries = 0; 

      while (win == false) { 

       System.out.println("I'm thinking of a number between 1 and 20, can you guess it?"); 
       guess = Bill.nextInt(); 
       NumOfTries++; 

       if (guess == NumToGuess) { 
        win = true; 
       } 
       else if (guess < NumToGuess) { 
        System.out.println("Your guess is too low"); 
       } 
       else if (guess > NumToGuess) { 
        System.out.println("Your guess is too high"); 
       } 
      } 

      System.out.println("Congratulations!"); 
      System.out.println("The number was " + NumToGuess); 
      System.out.println("It took you " + NumOfTries + " tries"); 

      // As CandiedOrange already noted, you need to remove the while loop which was here. 
      String YesOrNo; 
      YesOrNo = Bill.nextLine(); 
      System.out.println("Would you like to play again? " + YesOrNo); 
      YesOrNo = Bill.nextLine(); 

      if (YesOrNo.equalsIgnoreCase("yes")) 
      { 
       System.out.println("Ok, let's play again."); 
       continueGame = true; 
      } 
      else 
      { 
       System.out.println("Ok, maybe next time."); 
       continueGame = false; 
      } 
     } 
    } 
} 

這就是它。如果您對此答案有疑問,請隨時詢問。

+0

我做了你說的所有事情,現在它循環着,但它保持相同的數字,並且在你贏得比賽後繼續比賽時不會切換到新的。 – crpittman18 2014-12-07 22:07:00

+0

@ crpittman18對,對不起。我忘了將'NumToGuess''和'''NumOfTries''放入我創建的循環中。該帖子中的代碼現在應該可以工作。 – mezzodrinker 2014-12-07 22:08:39

+0

非常感謝!對此,我真的非常感激! – crpittman18 2014-12-07 22:11:03

0

您可以使用標籤break語句。 或者你需要重新運行該程序,因爲while循環結束,你就出來了。

2

你可以嘗試這樣的事情太:

public class MainClass { 

public static void main(String[] args) { 
    Random bob = new Random(); 
    Scanner bill = new Scanner(System.in); 

    String yesOrNo = ""; 
    do { 

     int numToGuess = bob.nextInt(20); 
     int numOfTries = 0; 
     int guess = 0; 
     boolean win = false; 

     while (win == false) { 

      System.out 
        .println("I'm thinking of a number between 1 and 20, can you guess it?"); 
      guess = Bill.nextInt(); 
      numOfTries++; 

      if (guess == numToGuess) { 
       win = true; 
      } else if (guess < numToGuess) { 
       System.out.println("Your guess is too low"); 
      } else if (guess > numToGuess) { 
       System.out.println("Your guess is too high"); 
      } 
     } 

     System.out.println("Congratulations!"); 
     System.out.println("The number was " + NumToGuess); 
     System.out.println("It took you " + NumOfTries + " tries"); 

     yesOrNo = bill.nextLine(); 
     System.out.println("Would you like to play again? " + yesOrNo); 
     yesOrNo = bill.nextLine(); 

    } while (yesOrNo.equalsIgnoreCase("yes")); 

    System.out.println("Ok, maybe next time."); 
    System.exit(0); 

} 

}