[英]Why is the changeDifficulty while loop in my java guessing game not repeating?
我是一名初级程序员,我制作了这个猜数字游戏,它会生成一个随机数(范围取决于所选难度)。 我正在努力使用户能够重新选择他们的难度,如果他们想改变他们的选择。 我使用 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 的新布尔值的方法,但是如何比较玩家输入的正确性? 如下。
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.