如何使用中断返回到最内部循环

时间:2017-03-18 11:19:53

标签: java

我不确定问题是什么,但是我的嵌套while循环中的中断让我回到我的外循环,当我想要回到我内心的循环时,我已经尝试过了使用标签,但没有工作。

package e;

import java.util.Scanner;

public class HangmanBeta{
private static final boolean testingMode = true;
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    keyboard.useDelimiter("\\n"); // ---------- this is what tells scanner to use new line char as delimiter

    while (true) {
        System.out.println("Enter your difficulty: Easy (e), Intermediate (i), or Hard (h)");
        String diff = keyboard.next();
        int amountOfSpaces = 0;
        String response = "";
        String guess = "";
        String newGuess = "";
        String newWord = "loops";//RandomWord.newWord();

        int guesses = 0;
        for (int i = 0; i < newWord.length(); i++) {
            guess = newWord.replaceAll("[^#]", "-");
        }
        if ((diff.equalsIgnoreCase("e")) || (diff.equalsIgnoreCase("i")) || (diff.equalsIgnoreCase("h"))) {
            if (diff.equalsIgnoreCase("e"))
            {
                guesses = 15;
            }
            if(diff.equalsIgnoreCase("i"))
            {
                guesses = 12;
            }
            if(diff.equalsIgnoreCase("h"))
            {
                guesses = 15;
            }
            if (testingMode == true)
            {
                System.out.println("The secret word is:" + " " + newWord);
            }
            System.out.println("The word is:" + " " + guess);
            label1:
                while(!newWord.equalsIgnoreCase(guess))
                {
                    {
                        System.out.println("Please enter the letter you want to guess");
                        String letterInput = keyboard.next();
                        if(letterInput.equalsIgnoreCase("solve"))
                        {
                            System.out.println("Please solve the answer:");
                            String userSolve = keyboard.next();
                            if (!userSolve.equalsIgnoreCase(newWord))
                            {
                                System.out.println("That is not the secret word");
                                guesses = guesses - 1;
                                System.out.println("Guesses remaining: " + guesses);
                            }
                            else
                            {
                                System.out.println("You win!");
                                System.out.println("You have guessed the word! Congratulations");
                                System.out.println("Would you like to play again? Yes(y) or No (n)");
                                response = keyboard.next();
                                if (response.equalsIgnoreCase("n"))
                                {
                                    System.exit(0);
                                }

                            }
                        }
                        System.out.println("Please enter the spaces you want to check (seperated by spaces)");
                        String spaces = keyboard.next();
                        amountOfSpaces = spaces.trim().length();
                        if (diff.equalsIgnoreCase("e"))
                        {
                            if(amountOfSpaces == 4)
                            {
                                continue;
                            }
                            else
                            {
                                System.out.println("Your input is not valid, try again");
                                break label1;
                            }
                        }
                        if (diff.equalsIgnoreCase("i"))
                        {
                            if(amountOfSpaces != 3)
                            {
                                System.out.println("Your input is not valid, try again");   
                                break;
                            }
                        }

                        if (diff.equalsIgnoreCase("h"))
                        {
                            if(amountOfSpaces != 2)
                            {
                                System.out.println("Your input is not valid, try again");
                                break;
                            }
                        }

                        for (String s : spaces.split("\\s"))
                        {
                            int x = Integer.valueOf(s);

                            if (Character.toLowerCase(newWord.charAt(x)) == letterInput.charAt(0)) 
                            {
                                //System.out.println("Guess is correct for position " + x);
                                guess = guess.substring(0, x) + letterInput + guess.substring(x + 1, guess.length());

                            }
                            else
                            {
                                guesses= guesses - 1;
                                System.out.println("Your letter was not found in spaces you provided");
                                System.out.println("Guesses Remaining: " + guesses);
                            }

                            if (guesses == 0)
                            {
                                System.out.println("You have failed to guess the word....:(");
                                System.out.print("Would you like to play again? Yes(y) or No(n)");
                                response = keyboard.next();
                                if (response.equalsIgnoreCase("n"))
                                {
                                    System.exit(0);
                                }
                            }
                            System.out.println("Your Guess is in the word");
                        }System.out.println(guess);
                    }
                }   }
    }
}

}

0 个答案:

没有答案