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



我有一个猜谜游戏程序,在这个程序中,你可以尝试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语句。希望这能有所帮助!

只需进行一个小调整,就可以让您继续前进。

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

解决方案

在控制流结束时,如果猜测正确答案超过猜测次数,则需要提示用户输入。

也许是这样的:"你想再试一次吗?">

你可以使用scanner库来完成这项工作,你可以从这里阅读并实现它。

如果他们键入"yes">(小心情况(

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

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

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

我希望这能有所帮助。

看起来是使用单独方法的好候选者。将当前循环移动到另一个方法,然后在主方法内进行另一个循环,调用新创建的方法,并在玩家运行后返回时提示重播。

部分代码示例:

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

最新更新