循环时不能停止

时间:2015-09-26 03:59:39

标签: java while-loop break

在我创造的这个游戏中我遇到了一个问题。这一切都很有效,除非你因某种原因失败,游戏才会重新启动,而这不是我想要的。我想显示我设置显示的内容,然后打破循环,由于某种原因break;不起作用。 代码:

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

public class GuessingGame1_3 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Random rand = new Random();

        System.out.print("Pick a number to guess between: ");
        int userNumber = input.nextInt();

        int random = rand.nextInt(userNumber);

       if (random < 1) {
            random = random + 1;
       } else if (random > userNumber) {
            random = random - 1;
       }

        while (true) {
            System.out.print('\f');
            System.out.print("Pick the amount of attempts you would like to have (Max of 10 attempts): ");
            int userAttempts = input.nextInt();

          if (userAttempts > 10) {
             System.out.println("To Many Attempts");
             System.out.println("");
          } else if (userAttempts <= 10) {

            System.out.println("Version: 1.3");
            System.out.println("----------------------------------------------------------------------");
            System.out.println("You have " + userAttempts + " attempts to guess the number or else you fail. Goodluck!");
            System.out.println("");
            System.out.println("Type 'begin' to Begin!");
            String start = input.next();
            System.out.print('\f');

            if (start.equals("begin")) {
            for(int i=1; i<userAttempts + 1; i++) {
              System.out.print("Enter a number between 1-" + userNumber + ": ");
              int number = input.nextInt();

              if (number > random) {
                System.out.println("Too Big");
                System.out.println("");
              } else if (number < random) {
                System.out.println("Too Small");
                System.out.println("");
              } else if (number == random) {
                System.out.println("Correct!");
               break;
              }

              if (i == userAttempts) {
               System.out.println("You have failed");
               System.out.println("Number Was: " + random);
               break;
              }
            }
          } else if (!start.equals("begin")) {
            System.out.print('\f');
            System.out.println("Incorrect Command");
            System.out.println("Please Exit Console And Retry");
            break;
          }
        }
      }
    }
}

3 个答案:

答案 0 :(得分:2)

如果您想要break外部循环,可以使用嵌套循环添加标签前缀 1

game: while (true) {

然后你可以

break game;

终止标记为game的语句。

1 另请参阅JLS-14.7. Labeled Statements,其中(部分)语句可能 标签 前缀。

答案 1 :(得分:0)

引入一个变量来检查是否应该继续循环。类似的东西:

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

        System.out.print("Pick a number to guess between: ");
        int userNumber = input.nextInt();

        int random = rand.nextInt(userNumber);

        if (random < 1) {
            random = random + 1;
        } else if (random > userNumber) {
            random = random - 1;
        }

        boolean continueGame = true;
        while (continueGame) {
            System.out.print('\f');
            System.out
                    .print("Pick the amount of attempts you would like to have (Max of 10 attempts): ");
            int userAttempts = input.nextInt();

            if (userAttempts > 10) {
                System.out.println("To Many Attempts");
                System.out.println("");
            } else if (userAttempts <= 10) {

                System.out.println("Version: 1.3");
                System.out
                        .println("----------------------------------------------------------------------");
                System.out
                        .println("You have "
                                + userAttempts
                                + " attempts to guess the number or else you fail. Goodluck!");
                System.out.println("");
                System.out.println("Type 'begin' to Begin!");
                String start = input.next();
                System.out.print('\f');

                if (start.equals("begin")) {
                    for (int i = 1; i < userAttempts + 1; i++) {
                        System.out.print("Enter a number between 1-"
                                + userNumber + ": ");
                        int number = input.nextInt();

                        if (number > random) {
                            System.out.println("Too Big");
                            System.out.println("");
                        } else if (number < random) {
                            System.out.println("Too Small");
                            System.out.println("");
                        } else if (number == random) {
                            System.out.println("Correct!");
                            continueGame = false;
                        }

                        if (i == userAttempts) {
                            System.out.println("You have failed");
                            System.out.println("Number Was: " + random);
                            continueGame = false;
                        }
                    }
                } else { //You may remove if (!start.equals("begin")) check 
                         //as the result is either true or false and you 
                         //have checked the true condition in if
                    System.out.print('\f');
                    System.out.println("Incorrect Command");
                    System.out.println("Please Exit Console And Retry");
                    continueGame = false;
                }
            }
        }
    }

答案 2 :(得分:0)

您需要打破外部循环以及内部循环

看看:(变量failattempt

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

public class GuessingGame1_3 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Random rand = new Random();

        System.out.print("Pick a number to guess between: ");
        int userNumber = input.nextInt();

        int random = rand.nextInt(userNumber);

       if (random < 1) {
            random = random + 1;
       } else if (random > userNumber) {
            random = random - 1;
       }

        boolean failattempt = false;
        while (true) {
            System.out.print('\f');
            System.out.print("Pick the amount of attempts you would like to have (Max of 10 attempts): ");
            int userAttempts = input.nextInt();

          if (userAttempts > 10) {
             System.out.println("To Many Attempts");
             System.out.println("");
          } else if (userAttempts <= 10) {

            System.out.println("Version: 1.3");
            System.out.println("----------------------------------------------------------------------");
            System.out.println("You have " + userAttempts + " attempts to guess the number or else you fail. Goodluck!");
            System.out.println("");
            System.out.println("Type 'begin' to Begin!");
            String start = input.next();
            System.out.print('\f');

            if (start.equals("begin")) {
            for(int i=1; i<userAttempts + 1; i++) {
              System.out.print("Enter a number between 1-" + userNumber + ": ");
              int number = input.nextInt();

              if (number > random) {
                System.out.println("Too Big");
                System.out.println("");
              } else if (number < random) {
                System.out.println("Too Small");
                System.out.println("");
              } else if (number == random) {
                System.out.println("Correct!");
               break;
              }

              if (i == userAttempts) {
               System.out.println("You have failed");
               System.out.println("Number Was: " + random);
               failattempt = true;
               break;
              }
            }
            if (failattempt == true)
                break;
          } else if (!start.equals("begin")) {
            System.out.print('\f');
            System.out.println("Incorrect Command");
            System.out.println("Please Exit Console And Retry");
            break;
          }
        }
      }
    }
}