2017-02-06 166 views
1

我希望能夠讓用戶開始新一輪或結束遊戲。每次詢問我是否想繼續(n/y)時,我都會回答否,這並不會結束遊戲。我如何擺脫這個循環? java

我又如何計算出回合數?另外私人靜態變量在我沒有做的任務中給出。

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

public class GuessingGame { 

    private static final int MIN_NUMBER = 1; 
    private static final int MAX_NUMBER = 205; 
    private static final int QUIT_VALUE = -1; 
    private static final int LOSE_VALUE = -2; 
    private static final int MAX_GAMES = 4; 
    private static final int MAX_GUESSES = 10; 
    private static final int HINT_THRESHOLD = 5; 
    private static final int BACKDOOR_VALUE = -314; 

    private static final String NOPE_MSG = "nope..."; 
    private static final String NOPE_NOPE_MSG 
      = "you've already guessed that wrong guess..."; 
    private static final String INVALID_INPUT_BEGIN 
      = "*** invalid input -- "; 
    private static final String INVALID_INPUT_LESS_MIN_MSG 
      = INVALID_INPUT_BEGIN + "must be greater than " + (MIN_NUMBER - 1); 
    private static final String INVALID_INPUT_GREATER_MAX_MSG 
      = INVALID_INPUT_BEGIN + "must be less than " + (MAX_NUMBER + 1); 
    private static final String INVALID_INPUT_YN_MSG 
      = INVALID_INPUT_BEGIN + "must be n or y"; 
    private static final String WINNER_MSG 
      = "you're a winner... # of guesses: "; 
    private static final String LOSER_MSG 
      = "too many guesses entered... the number was "; 
    private static final String QUITTER_MSG 
      = "you're a quitter... the number was "; 
    private static final String MAX_GAMES_PLAYED_MSG 
      = "you've played the maximum number (" + MAX_GAMES + ") of games"; 
    private static final String ENTER_GUESS_PROMPT 
      = "enter a guess between " + MIN_NUMBER + " and " + MAX_NUMBER 
      + " (" + QUIT_VALUE + " to quit): "; 
    private static final String PLAY_AGAIN_PROMPT 
      = "Do you want to play again (n or y)? "; 

    private static final String BOLD_BEGIN = "*** "; 
    private static final String BOLD_END = " ***"; 
    private static final String PLAY_MSG = " playing the CSC" + MAX_NUMBER 
      + " guessing game." + BOLD_END; 
    private static final String WELCOME_MSG 
      = BOLD_BEGIN + "Hello! Have fun" + PLAY_MSG; 
    private static final String THANKS_MSG 
      = BOLD_BEGIN + "Thanks for" + PLAY_MSG; 

    public static void main(String[] args) { 

     PlayGame(); 
    } 

    static Random rng = new Random(System.currentTimeMillis()); 
    static Scanner stdin = new Scanner(System.in); 
    static Scanner scan = new Scanner(System.in); 

    static int n = MIN_NUMBER + rng.nextInt(MAX_NUMBER); 
    static int guess; 
    static int guessCounter = 0; 

    static boolean endStart = true; 

    public static void PlayGame() { 

     System.out.println(WELCOME_MSG); 
     while (endStart = true) { 
      do { 

       System.out.println(); 
       System.out.print(ENTER_GUESS_PROMPT); 
       guess = stdin.nextInt(); 
       guessCounter = guessCounter + 1; 

       if (guess < MIN_NUMBER && guess != BACKDOOR_VALUE 
         && guess != QUIT_VALUE) { 
        System.out.println(INVALID_INPUT_LESS_MIN_MSG + "\n"); 
       } 
       if (guess > MAX_NUMBER) { 
        System.out.println(INVALID_INPUT_GREATER_MAX_MSG + "\n"); 
       } 
       if (guess == BACKDOOR_VALUE) { 
        System.out.println(n); 
       } 

       if (guess == QUIT_VALUE) { 
        System.out.println(QUITTER_MSG + n); 
        System.out.println(PLAY_AGAIN_PROMPT); 
        String val = scan.next(); 
        if (val.equalsIgnoreCase("y")) { 
         PlayGame(); 
        } 
        if (val.equalsIgnoreCase("n")) { 
         System.out.println(THANKS_MSG); 
         break; 
        } 
       } 

       if (guessCounter == MAX_GUESSES) { 
        System.out.println(LOSER_MSG + n 
          + PLAY_AGAIN_PROMPT); 
        String val = scan.next(); 
        if (val.equalsIgnoreCase("y")) { 
         PlayGame(); 
        } 
        if (val.equalsIgnoreCase("n")) { 
         System.out.println(THANKS_MSG); 
         break; 
        } 
       } 

       if (guess > MIN_NUMBER || guess < MAX_NUMBER) { 
        if (guess != n || guess == BACKDOOR_VALUE) { 
         System.out.println(NOPE_MSG); 
        } 
        if (guess == n) { 
         System.out.println(WINNER_MSG + guessCounter); 

         System.out.println(PLAY_AGAIN_PROMPT); 
         String val = scan.next(); 
         if (val.equalsIgnoreCase("y")) { 
          PlayGame(); 
         } 
         if (val.equalsIgnoreCase("n")) { 
          System.out.println(THANKS_MSG); 
          break; 
         } 
        } 
       } 
      } while (guess > MIN_NUMBER || guess < MAX_NUMBER); 

     } 
    } 
} 
+2

太多的代碼行和太多的問題。你打破了循環與休息。 – efekctive

+0

@efekctive我知道我可以使用休息,但我想知道如何擺脫特定的循環。 –

+0

然後減少示例代碼。如果你知道如何突破一次,第二次是一樣的 – efekctive

回答

0

,您可以撥打boolean,你可以在你的循環與它檢查例如:

boolean test = true; 
do{ 
    //your code 
    //if you want to break you can change the test variable 
    ... 
    if(val.equalsIgnoreCase("n")){ 
     System.out.println(THANKS_MSG); 
     test = false; 
     break(); 
    } 
    .. 
}while(guess > MIN_NUMBER || guess < MAX_NUMBER || test); 

所以,如果你的條件或檢測是假的,你會從你的循環中斷。

0

while(endStart == true) or while(endStart)會更好看,而同時(endStart = TRUE),絕非endStart變量的比較,再加上你的遞歸無限期每次你打電話瑣事()一次的增益,在玩笑()方法,並它不斷獲得更瘋狂

0

您可以使用帶標籤的中斷,帶標籤的中斷語句用於終止外部循環,應標記循環以使其工作。舉個例子,按照這個,

loop1: 
for(){ 
    ... 
    for(){ 
     .... 
     break loop1; 
    } 
}