我正在编写一个RNG猜谜游戏作为我的CS入门课程,并且进展顺利。唯一的问题是,在我测试代码的时候,有时候我在简单模式下输入一个整数,比如5,它说太高了,所以我每次降低一个中间值到1,这应该是最小的边界,它仍然说太高了。是否有人愿意找到我在代码中遗漏的错误,并向我解释为什么这是一个错误,以及更好的方法来处理它,以便我知道将来的参考?谢谢!
公共类Game {
public static Random randAIThor = new Random();
public static BufferedReader buffLove = new BufferedReader(new InputStreamReader(System.in));
public static int counter = 5;
public static void run() throws IOException {
difficultyMenu();
}
public static void difficultyMenu() throws IOException {
System.out.println("Please select your difficulty:");
System.out.println("Press 1 for Easy Mode.");
System.out.println("Press 2 for Medium Mode.");
System.out.println("Press 3 for Hard Mode.");
System.out.println("Press 4 to Exit.");
BufferedReader buffLove = new BufferedReader(new InputStreamReader(System.in));
String input = buffLove.readLine();
int menu = Integer.parseInt(input);
if (counter == 0) {
counter = 5;
}
switch (menu) {
case 1:
System.out.println("Easy");
gameModeEasy();
break;
case 2:
System.out.println("Medium");
gameModeMedium();
break;
case 3:
System.out.println("Hard");
gameModeHard();
break;
case 4:
System.out.println("Exit");
System.out.println("That'll do pig, that'll do.");
System.exit(menu);
break;
default:
difficultyMenu();
break;
}
}
public static void gameModeEasy() throws IOException {
int e = randAIThor.nextInt(10) + 1;
while (counter > 0) {
counter--;
System.out.println("Guess a number!");
String input = buffLove.readLine();
int userNum = Integer.parseInt(input);
if (e > userNum) {
System.out.println("Your guess is too high, please try again.");
} else if (e < userNum) {
System.out.println("Your guess is too low, please try again.");
} else if (userNum == e) {
System.out.println("Whoa! You must be psychic! Good guess dude! (You Win)");
System.out.println("Want to play again?");
difficultyMenu();
}
System.out.println("You have " + counter + " attempts left.");
if (counter == 0) {
System.out.println("GAME OVER");
System.out.println("Want to try again?");
difficultyMenu();
}
}
}
public static void gameModeMedium() throws IOException {
int m = randAIThor.nextInt(50) + 1;
while (counter > 0) {
counter--;
System.out.println("Guess a number!");
String input = buffLove.readLine();
int userNum = Integer.parseInt(input);
if (m > userNum) {
System.out.println("Your guess is too high, please try again.");
} else if (m < userNum) {
System.out.println("Your guess is too low, please try again.");
} else if (userNum == m) {
System.out.println("Whoa! You must be psychic! Good guess dude! (You Win)");
System.out.println("Want to play again?");
difficultyMenu();
}
System.out.println("You have " + counter + " attempts left.");
if (counter == 0) {
System.out.println("GAME OVER");
System.out.println("Want to try again?");
difficultyMenu();
}
}
}
public static void gameModeHard() throws IOException {
int h = randAIThor.nextInt(100) + 1;
while (counter > 0) {
counter--;
System.out.println("Guess a number!");
String input = buffLove.readLine();
int userNum = Integer.parseInt(input);
if (h > userNum) {
System.out.println("Your guess is too high, please try again.");
} else if (h < userNum) {
System.out.println("Your guess is too low, please try again.");
} else if (userNum == h) {
System.out.println("Whoa! You must be psychic! Good guess dude! (You Win)");
System.out.println("Want to play again?");
difficultyMenu();
}
System.out.println("You have " + counter + " attempts left.");
if (counter == 0) {
System.out.println("GAME OVER");
System.out.println("Want to try again?");
difficultyMenu();
}
}
}
}
您的if-condition错误。你告诉用户,这个数字太高了,而实际上它太低了:
if(e < userNum) {
System.out.println("Your guess is too high, please try again.");
}