Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I';我在做一个RNG猜谜游戏,而生成器一直保持1-10的包含集界限_Java_Random - Fatal编程技术网

Java I';我在做一个RNG猜谜游戏,而生成器一直保持1-10的包含集界限

Java I';我在做一个RNG猜谜游戏,而生成器一直保持1-10的包含集界限,java,random,Java,Random,我正在为我的CS课程介绍编写一个RNG猜测游戏,进展顺利。在它完成之前,我现在遇到的唯一问题是,有时我测试代码,在easy模式下输入一个像5这样的整数,它表示太高,所以我一次将一个inter降低到1,这应该是最低界限,它仍然表示太高。是否有人愿意找到我在代码中遗漏的错误,并向我解释为什么这是一个错误,以及更好的方法来处理它,以便我知道以备将来参考?谢谢 公开课游戏{ public static Random randAIThor = new Random(); public static Buf

我正在为我的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();
        }
    }
}
public static Random randAIThor=new Random();
publicstaticbufferedreader buffLove=newbufferedreader(newinputstreamreader(System.in));
公共静态整数计数器=5;
public static void run()引发IOException{
困难菜单();
}
公共静态无效困难菜单()引发IOException{
System.out.println(“请选择您的难度:”);
System.out.println(“按1表示简易模式”);
System.out.println(“按2表示中等模式”);
System.out.println(“按3键表示硬模式”);
System.out.println(“按4退出”);
BufferedReader buffLove=新的BufferedReader(新的InputStreamReader(System.in));
字符串输入=buffLove.readLine();
int菜单=Integer.parseInt(输入);
如果(计数器==0){
计数器=5;
}
开关(菜单){
案例1:
System.out.println(“容易”);
gameModeEasy();
打破
案例2:
系统输出打印项次(“中”);
gamemodemedia();
打破
案例3:
System.out.println(“硬”);
gameModeHard();
打破
案例4:
System.out.println(“退出”);
System.out.println(“那就行了,猪,那就行了”);
系统退出(菜单);
打破
违约:
困难菜单();
打破
}
}
public static void gameModeEasy()引发IOException{
int e=randAIThor.nextInt(10)+1;
而(计数器>0){
计数器--;
System.out.println(“猜一个数字!”);
字符串输入=buffLove.readLine();
int userNum=Integer.parseInt(输入);
如果(e>userNum){
System.out.println(“您的猜测太高,请再试一次。”);
}else if(e0){
计数器--;
System.out.println(“猜一个数字!”);
字符串输入=buffLove.readLine();
int userNum=Integer.parseInt(输入);
如果(m>userNum){
System.out.println(“您的猜测太高,请再试一次。”);
}else if(m0){
计数器--;
System.out.println(“猜一个数字!”);
字符串输入=buffLove.readLine();
int userNum=Integer.parseInt(输入);
if(h>userNum){
System.out.println(“您的猜测太高,请再试一次。”);
}else if(h

}

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

if(e < userNum) {
    System.out.println("Your guess is too high, please try again.");
}
if(e
if(e>userNum)
换句话说
if(随机数比用户选择的要大)
您告诉用户他猜得太高了。非常感谢。我甚至没意识到我把符号弄混了。。完全令人尴尬的lol。。谢谢你,伙计,帮了我大忙。