如果条件为真,则继续while循环

时间:2020-03-31 21:44:21

标签: java

我有一个猜谜游戏程序,其中您有4次尝试猜测0到9之间的数字。我想添加一个选项,该程序在用户超过4次尝试时询问用户是否要再次玩。这是我的代码:

public static void main(String[] args) {
    int nb, x;
    int count = 1;
    Scanner inp = new Scanner(System.in);
    nb = (int) (Math.random() * 10);

    System.out.print("Guess the number between 0 and 9 (4 Guesses allowed):");
    x = inp.nextInt();

    while (x != nb) {
        if (count <= 3) {
            if (x > nb) System.out.println("Lesser");
            else if (x < nb) System.out.println("Bigger");
            System.out.println("Wrong try another number");
            x = inp.nextInt();
            if (x == nb) System.out.println("Found!!");
        } else {
            System.out.print("you exceeded your allowed Guesses");
            break;
        }
        count++;
    }
    inp.close();
}

5 个答案:

答案 0 :(得分:2)

希望您(以及其他所有人)保持健康!

嵌套在while循环中,有一个if-else语句。我在这里建议您补充一下。在else分支内,我将输出所需的消息,然后处理用户输入:如果用户说是,请将您的计数整数重置为零; while循环将重新开始,您将能够继续猜测。如果用户拒绝,我将像您现在那样执行break语句。希望这会有所帮助!

答案 1 :(得分:1)

看起来像个很好的候选人,可以利用单独的方法。将当前循环移动到另一个方法,然后在主方法内进行另一个循环,该循环调用新创建的方法,并在播放器返回后提示播放重播。

部分代码示例:

public static void main(String[] args) {
  Scanner inp = new Scanner(System.in);
  boolean play = true;
  while(play) {
    runGame(inp);
    System.out.println("Play again?");
    play = inp.nextLine().toUpperCase().contains("YES");
  }
  inp.close();
}

public static void runGame(Scanner inp) {
  int count = 1;
  //Move your current loop stuff here
}

答案 2 :(得分:1)

解决方案

在控制流结束时,无论您是猜出正确答案还是超出猜想次数,您都需要提示用户输入。

也许是这样的:“您想再试一次吗?”

您可以使用扫描仪库来执行此操作,可以从here中阅读并实施它。

如果他们输入“是” (请注意大小写)

再次分配nb = (int) (Math.random() * 10);并确认它与以前的值不同。这将导致循环继续运行,从而使游戏继续进行。

注意:重要的是,如果再次出现相同的数字,请您进行处理,以免终止游戏。您可以通过将另一个随机数!=设置为前一个,或者从您的随机数池中排除该前一个数字来做到这一点,从而确保选择是不同的。

此外,出于同样的原因,我建议您给变量起一个更好的名称以提高可读性,并更好地格式化代码。

我希望这会有所帮助。

答案 3 :(得分:1)

稍作调整,即可开始操作。

    public static void main(String[] args) {
int nb, x=-1;
int count = 0;
Scanner inp = new Scanner(System.in);
nb = (int) (Math.random() * 10);


while (x != nb) {
    System.out.print("Guess the number between 0 and 9 (4 Guesses allowed):");
    x = inp.nextInt();
        inp.nextLine();
         count++;
    if (x == nb){ System.out.println("Found!!");break;}
    if (x > nb) System.out.println("Lesser");
        else if (x < nb) System.out.println("Bigger");
        System.out.println("Wrong try another number");

    if (count == 2) {


        System.out.println("you exceeded your allowed Guesses");
        System.out.println("would you like to play again? (input y or n)");
        String newGame = inp.next();
               inp.nextLine();

        if(newGame.compareTo("n")==0)break;
        else count = 0;  

    }

}
inp.close();
}
}

答案 4 :(得分:0)

public static void main(String... args) {
    final int min = 0;
    final int max = 10;
    final int maxGuesses = 4;

    try (Scanner scan = new Scanner(System.in)) {
        while (true) {
            final int expected = min + new Random().nextInt(max + 1 - min);
            boolean found = false;

            System.out.printf("Guess the number between %d and %d (%d Guesses allowed).\n", min, max, maxGuesses);

            for (int i = 1; i <= maxGuesses; i++) {
                System.out.printf("Guess #%d: ", i);
                int number = scan.nextInt();

                if (number == expected) {
                    found = true;
                    break;
                }

                System.out.println(number > expected ? "Lesser" : "Bigger");

                if (i < maxGuesses)
                    System.out.println("Wrong try another number");
            }

            System.out.println(found ? "Found!!" : "you exceeded your allowed Guesses");

            System.out.print("Do you want to continue (Y/N): ");
            char ch = scan.next().charAt(0);

            if (Character.toLowerCase(ch) == 'N')
                break;
        }
    }
}