I'm trying to have the main method call the newGame method but it's giving me an error.
error: cannot find symbol
newGame(answer);
symbol: variable answer
location: class GuessingGame
import java.util.Random;
public class GuessingGame {
public static newGame(int answer){
Random rand = new Random(int answer);
answer = rand.nextInt(51);
}
public static void main (String [] args ){
newGame(answer);
}
}
Your posted code is missing a few things, and doesn't do much. I assume you want to return the new random value from newGame
(and thus it should return
an int
). Also, it's better to pass the Random
to your method (because creating a new Random
involves seeding it, and if you do it quickly in a loop you can pick the same seed). So, that might look like
public static int newGame(Random rand) {
return rand.nextInt(51);
}
Then you need to save the answer
in main
. And construct the Random
. Like,
public static void main(String[] args) {
Random rand = new Random();
int answer = newGame(rand);
}