有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我正在开发一个RNG猜测游戏,生成器一直保留110的包含集界限

我正在为我的CS课程介绍编写一个RNG猜测游戏,进展顺利。在它完成之前,我现在唯一的问题是,有时我测试代码,在easy模式下输入一个像5这样的整数,它表示太高,所以我一次降低一个inter到1,这应该是最低界限,但它仍然表示太高。是否有人愿意找到我在代码中遗漏的错误,并向我解释为什么这是一个错误,以及更好的方法来处理它,以便我知道以备将来参考?谢谢

公开课游戏{

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();
        }
    }
}

}


共 (1) 个答案

  1. # 1 楼答案

    如果你的情况不对。你告诉用户,这个数字太高了,而实际上它太低了:

    if(e < userNum) {
        System.out.println("Your guess is too high, please try again.");
    }