I'm coding a RNG Guessing Game for my intro to CS class, and it's going well. The only issue I'm having now before it's done is that there are times when I test the code, and I enter an integer like 5 on easy mode, and it says too high, so I lower one inter at a time to 1, which should be the lowest bound, and it still says it's too high. Would someone be willing to find the error I'm missing in the code and explain to me why it's an error, and a better way to go about it so I know for future reference? Thanks!
public class 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();
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire