繁体   English   中英

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

[英]continue a while loop if a condition is true

我有一个猜谜游戏程序,您有 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();
}

希望您(和其他所有人)都保持健康!

嵌套在您的 while 循环中,您有一个 if-else 语句。 这是我建议您添加的地方。 在 else 分支中,我会输出您想要的消息,然后处理用户输入:如果用户说是,则将您的计数整数重置为零; while 循环将重新启动,您将能够继续猜测。 如果用户说不,我会像你现在一样执行 break 语句。 希望这可以帮助!

看起来是一个很好的候选者,可以使用单独的方法。 将当前循环移动到另一个方法,然后在 main 方法中创建另一个循环,该循环调用新创建的方法并在播放器运行后返回时提示重播。

部分代码示例:

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
}

解决方案

在控制流结束时,无论是您猜到了正确答案还是超过了猜测次数,您都需要提示用户进行输入。

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

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

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

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

注意:重要的是,如果再次出现相同的数字,请处理它以免终止游戏。 您可以通过获取另一个随机数 != 到前一个,或从您的随机数池中排除该前一个数字来确保选择不同。

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

我希望这有帮助。

只需稍作调整,这将使您继续前进。

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

暂无
暂无

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

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