Skip to content
Advertisement

Can I use a randomly generated number as a parameter to a method in Java?

So I’m very new to Java so this question might have a pretty simple answer. I’m trying to create a Magic8 ball program and I want the fortune to depend on the randomly generated number, but it’s treated as a method and I’m not sure that a method can have another method as a parameter.

public int generator(){
    int num = rnd.nextInt(2) + 1;
return num;
}

That’s my method to generate the random number, is there a way to use the product of this in another method?

Advertisement

Answer

while defining the method you can do normal defining process such as

public int doStuffWithRandomNumber(int i){
//your code;
}

At the time of calling method you can use generator() method as parameter

doStuffWithRandomNumber(generator());

it will work properly no issue

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement