简体   繁体   English

猜数字游戏问题

[英]Number Guessing Game Problems

So, I'm having trouble regarding a guessing game with numbers. 因此,我在考虑猜数字游戏时遇到了麻烦。 I have a random number generated, but when I call my method into my main program to tell me if my guess is too big or too small, it doesnt seem to read the random number. 我生成了一个随机数,但是当我在主程序中调用我的方法以告诉我我的猜测太大还是太小时,它似乎没有读取随机数。

IE Random number generated is 12 IE随机数为12

Guess 3.
"Sorry, too big."
Guess 0
"Sorry, too small."

Any help would be appreciated. 任何帮助,将不胜感激。

Here's a snippet: 这是一个片段:

public static void main(String[] args) {

    Random rand = new Random();
    int rnum = rand.nextInt(49);
    System.out.print(rnum);

    for(int result = 0; result < 5; result++) {
        myMethod(result);
    }
}

public static void myMethod (int rnum) {
    Scanner input = new Scanner (System.in);
    System.out.println(" Guess a number between 0 and 49");
    int guess;
    guess = input.nextInt();

    if (guess > rnum) {
        System.out.print("Too big, sorry. Try again.");
    } else if (guess < rnum) {
        System.out.print("Too small, sorry. Try again.");
    } else if (guess == rnum) {
        System.out.print("You Win!");
    }
}

You are passing result to myMethod in your main method. 您正在将结果传递给主方法中的myMethod。 I suspect that you want to pass rnum to that method instead as that is your psudeo random number. 我怀疑您想将rnum传递给该方法,因为那是您的psudeo随机数。

In your code : 在您的代码中:

for(int result=0;result<5;result++){
    myMethod(result);
}

here you are sending result to myMethod instead of rnum. 在这里,您将结果发送到myMethod而不是rnum。

 Random rand = new Random();
 int rnum = rand.nextInt(49);

when myMethod is called it will send 0,1,2,3,4 according to your code . 调用myMethod时,它将根据您的代码发送0、1、2、3、4。

send 发送

for(int result=0;result<5;result++){
    myMethod(rnum);
}

Instead of 代替

for(int result=0;result<5;result++){
    myMethod(result);
}

Try this 尝试这个

import java.util.Scanner; 导入java.util.Scanner; import java.util.Random; 导入java.util.Random;

public class testt {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Guess a number: ");
    int Guessed = sc.nextInt();
    int Rnumber;
    Random num = new Random();
    for (int i=1; i <= 25; i++)
    {
        Rnumber = num.nextInt(12); // Numbers will be around 12

    }

    if (Guessed >= 3) {
        System.out.println("Sorry, too big");
    }
        else 
            if (Guessed <= 0){
        System.out.println("Sorry, too small");
    }

                else 
                    System.out.println("You win");

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM