简体   繁体   English

我如何突破这个循环? java的

[英]How do I break out of this loop? java

I want to be able to get the user to either start a new round or end the game. 我希望能够让用户开始新一轮或结束游戏。 Every time I answer no when asked if I want to continue(n/y) it does not end the game. 每次当我被问到是否要继续(n / y)时我都不回答它并不会结束游戏。

Also how do I count the number of rounds played? 另外我如何计算比赛次数? Also the private static variables are given in the assignment I did not make them. 私有静态变量也是在我没有做出的赋值中给出的。

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);

        }
    }
}

while(endStart == true) or while( endStart )看起来会更好,而while(endStart = true),绝不是endStart变量的比较,再加上你是不可避免的回归每当你在PlayGame中调用PlayGame()一次获得时( )方法,它一直变得越来越疯狂

You can make a boolean and you can check with it in your loop for example : 您可以创建一个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);

So if your condition or test is false you will break from your loop. 因此,如果您的条件或测试是错误的,您将从循环中断开。

You Can use a break with label, Labeled break statement is used to terminate the outer loop, the loop should be labeled for it to work. 你可以使用带标签的break,Labeled break语句用于终止外部循环,循环应标记为可以工作。 For an example follow this, 举个例子来说,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM