我是一名初学者程序员,我制作了这个猜数字游戏,它会生成一个随机数(范围取决于所选的难度)。我试图做到这一点,以便用户在想要更改选择时能够重新选择他们的难度。我用的是 while (changeDifficulty == true) 尝试执行此操作,但是在运行代码时,无论用户输入如何,循环都不会重复。有什么建议么?谢谢。
//Create a random number for the user to guess
int theNumber = 0;
Boolean changeDifficulty = true;
do {
while (changeDifficulty == true) {
System.out.println("Welcome to the number guessing game! \nSelect your difficulty (easy, medium, or hard.)");
String difficulty = "";
difficulty = scan.nextLine();
if (difficulty.equalsIgnoreCase("easy")) {
theNumber = (int)(Math.random() * 100 + 1);
} else if (difficulty.equalsIgnoreCase("medium")) {
theNumber = (int)(Math.random() * 1000 + 1);
} else if (difficulty.equalsIgnoreCase("hard")) {
theNumber = (int)(Math.random() * 10000 + 1);
}
String correctDifficulty = "";
if (difficulty.equalsIgnoreCase("easy")) {
System.out.println("You have selected " + difficulty + " meaning you must guess a number from 1-100"
+ " is this okay? (y/n)");
correctDifficulty = scan.nextLine();
if (correctDifficulty.equalsIgnoreCase("n")) {
changeDifficulty = false;
}
} else if (difficulty.equalsIgnoreCase("medium")) {
System.out.println("You have selected " + difficulty + " meaning you must guess a number from 1-1000"
+ " is this okay? (y/n)");
correctDifficulty = scan.nextLine();
if (correctDifficulty.equalsIgnoreCase("n")) {
changeDifficulty = false;
}
} else if (difficulty.equalsIgnoreCase("hard")) {
System.out.println("You have selected " + difficulty + " meaning you must guess a number from 1-10000"
+ " is this okay? (y/n)");
correctDifficulty = scan.nextLine();
if (correctDifficulty.equalsIgnoreCase("n")) {
changeDifficulty = false;
}
}
}
System.out.println(theNumber);
int guess = 0;
int numberOfTries = 0;
while (guess != theNumber) {
System.out.println("Guess a number between 1 and 100:");
guess = scan.nextInt();
numberOfTries = numberOfTries + 1;
if (guess < theNumber) {
System.out.println(guess + " is too low. Try again.");
} else if (guess > theNumber) {
System.out.println(guess + " is too high. Try again.");
} else {
System.out.println(guess + " is correct. You win! You took " + numberOfTries + " tries.");
}
}//end of while loop for guessing
System.out.println("Would you like to play again (y/n)?");
playAgain = scan.next();
} while (playAgain.equalsIgnoreCase("y"));
System.out.println("Thanks for playing! Goodbye!");
scan.close();
}
最佳答案
欢迎来到 Java 世界!
代码本身没有错误,但它没有按照我们想要的方式工作。让我们找出问题所在。
while (changeDifficulty == true) {
System.out.println("Welcome to the number guessing game! \nSelect your difficulty (easy, medium, or hard.)");
String difficulty = "";
difficulty = scan.nextLine();
if (difficulty.equalsIgnoreCase("easy")) {
theNumber = (int)(Math.random() * 100 + 1);
} else if (difficulty.equalsIgnoreCase("medium")) {
theNumber = (int)(Math.random() * 1000 + 1);
} else if (difficulty.equalsIgnoreCase("hard")) {
theNumber = (int)(Math.random() * 10000 + 1);
}
String correctDifficulty = "";
if (difficulty.equalsIgnoreCase("easy")) {
System.out.println("You have selected " + difficulty + " meaning you must guess a number from 1-100"
+ " is this okay? (y/n)");
correctDifficulty = scan.nextLine();
if (correctDifficulty.equalsIgnoreCase("n")) {
changeDifficulty = false;
}
} else if (difficulty.equalsIgnoreCase("medium")) {
System.out.println("You have selected " + difficulty + " meaning you must guess a number from 1-1000"
+ " is this okay? (y/n)");
correctDifficulty = scan.nextLine();
if (correctDifficulty.equalsIgnoreCase("n")) {
changeDifficulty = false;
}
} else if (difficulty.equalsIgnoreCase("hard")) {
System.out.println("You have selected " + difficulty + " meaning you must guess a number from 1-10000"
+ " is this okay? (y/n)");
correctDifficulty = scan.nextLine();
if (correctDifficulty.equalsIgnoreCase("n")) {
changeDifficulty = false;
}
}
}
我发现了其中可疑的部分,所以让我们一一分析一下。
如果玩家在您提出的代码进入“替换难度”步骤时按“n”[替换难度],则 changeDiffinity
将固定为 false,并且因此,每个游戏的 while(changeDifficulty ==true)
循环将被忽略。
因此,我们的玩家继续以相同的数字和相同的难度进行游戏。
让我们修改代码,让玩家可以正常改变难度级别,并重置数字和句子输出(1和100),以便难度级别改变后!
这是放入一个名为changeDifficient的新 boolean 值的一种方法,但是如何比较玩家输入的正确的Difficiency呢?如下。
while(changeDifficulty == true) -> do{ }while(correctDifficulty.equalsIgnoreCase("n"));
像这样
String correctDifficulty = "";
String numberRange ="";
...
...
do {
System.out.println("Welcome to the number guessing game! \n"
+ "Select your difficulty (easy, medium, or hard.)");
difficulty = scan.nextLine();
if (difficulty.equalsIgnoreCase("easy")) {
theNumber = (int)(Math.random() * 100 + 1);
numberRange = "1 to 100";
} else if (difficulty.equalsIgnoreCase("medium")) {
theNumber = (int)(Math.random() * 1000 + 1);
numberRange = "1 to 1000";
} else if (difficulty.equalsIgnoreCase("hard")) {
theNumber = (int)(Math.random() * 10000 + 1);
numberRange = "1 to 10000";
}
//Change Difficulty
System.out.println("You have selected " + difficulty +
" and it means you must guess a number from "+ numberRange //It depends on the level of difficulty.
+ " is this okay? (y/n)");
correctDifficulty = scan.nextLine();
}while(correctDifficulty.equalsIgnoreCase("n"));
希望对您有一点帮助,如果有什么难以理解的地方,请留言!希望您有一个平静的一天!
<小时/> <小时/>这是整个代码的改编。不过,我强烈建议您在看到此代码之前尝试自己的方式。
因为这也不是完美的方式。
import java.util.Scanner;
public class StackOver
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//Create a random number for the user to guess
String playAgain = "";
int theNumber = 0;
String difficulty = "";
String correctDifficulty = "";
String numberRange ="";
do {
do {
System.out.println("Welcome to the number guessing game! \n"
+ "Select your difficulty (easy, medium, or hard.)");
difficulty = scan.nextLine();
if (difficulty.equalsIgnoreCase("easy")) {
theNumber = (int)(Math.random() * 100 + 1);
numberRange = "1 to 100";
} else if (difficulty.equalsIgnoreCase("medium")) {
theNumber = (int)(Math.random() * 1000 + 1);
numberRange = "1 to 1000";
} else if (difficulty.equalsIgnoreCase("hard")) {
theNumber = (int)(Math.random() * 10000 + 1);
numberRange = "1 to 10000";
}
//Change Difficulty
System.out.println("You have selected " + difficulty +
" and it means you must guess a number from "+ numberRange //It depends on the level of difficulty.
+ " is this okay? (y/n)");
correctDifficulty = scan.nextLine();
}while(correctDifficulty.equalsIgnoreCase("n"));
System.out.println(theNumber); //oㅁo!
int guess = 0;
int numberOfTries = 0;
while (guess != theNumber) {
System.out.println("Guess a number between " + numberRange);
guess = scan.nextInt();
numberOfTries = numberOfTries + 1;
//numberOfTries += 1;
//numberOfTries ++;
//also make sense and shorter!
if (guess < theNumber) {
System.out.println(guess + " is too low. Try again.");
} else if (guess > theNumber) {
System.out.println(guess + " is too high. Try again.");
} else {
System.out.println(guess + " is correct. You win! You took " + numberOfTries + " tries.");
}
}//end of while loop for guessing
System.out.println("Would you like to play again (y/n)?");
playAgain = scan.next();
} while (playAgain.equalsIgnoreCase("y"));
System.out.println("Thanks for playing! Goodbye!");
scan.close();
}
}
关于java - 为什么我的java猜谜游戏中的变化难度while循环没有重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60824016/