我是一名初学者程序员,我制作了这个猜数字游戏,该游戏会生成一个随机数(范围取决于所选难度)。我正在努力做到这一点,以便用户可以更改他们的选择,从而重新选择他们的难度。我用了
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)
循环将被忽略。 。因此,我们的玩家继续以相同的数字和相同的难度等级进行游戏。
让我们修改代码,让Player可以正常更改难度级别,并重置数字和句子输出(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();
}
}