Lyfe Lyfe - 3 years ago 156
Java Question

Random generator always amounting to 0 (Java)

I am working through Head First Java, and my Random generator is amounting to 0. Here are my classes:
This is my class with the main method.

public class GameLauncher {
public static void main(String[] args) {
GuessGame game = new GuessGame();

This is my player object class:

import java.util.Random;

public class Player {
int number = 0; //Where the guess goes

public void guess() {
//random1 is in GuessGame
Random random2 = new Random();
int number = random2.nextInt(10);
System.out.println("I'm guessing " + number);

Finally, this is the class where most of the code is happening.

import java.util.Random;

public class GuessGame {
//Guessgame has three instance variables for the three Player objects
Player p1;
Player p2;
Player p3;

public void startGame() {
//Create three Player objects and assign them to the three Player instance variables
p1 = new Player();
p2 = new Player();
p3 = new Player();
//Declare three variables to hold the three guesses the players make
int guessp1 = 0;
int guessp2 = 0;
int guessp3 = 0;
//Declare three variables to hold a true or false based on the player's answer
boolean p1isRight = false;
boolean p2isRight = false;
boolean p3isRight = false;
//Make a "target" number that the players have to guess
Random random = new Random();
//Generate a number between 0 and 9
int targetNumber = random.nextInt(10);
System.out.println("I'm thinking of a number between 0 and 9...");

while (true) {
System.out.println("Number to guess is " + targetNumber);
//Call each player's guess() method
Get each player's guess (the result of their guess() method
running) by accessing the number variable of each player
guessp1 = p1.number;
guessp2 = p2.number;
guessp3 = p3.number;
System.out.println("Player one guessed " + guessp1);
System.out.println("Player two guessed " + guessp2);
System.out.println("Player three guessed " + guessp3);
Check each player's guess to see if it matches the target number. If a player is right, then set that player's variable to be true (remember, we set it false by default)
if (guessp1 == targetNumber) {
p1isRight = true;
if (guessp2 == targetNumber) {
p2isRight = true;
if (guessp3 == targetNumber) {
p3isRight = true;
//If player one OR player two OR player three is right... (the || operator means OR)
if (p1isRight || p2isRight || p3isRight) {
System.out.println("We have a winner!");
System.out.println("Player one got it right? " + p1isRight);
System.out.println("Player two got it right? " + p2isRight);
System.out.println("Player three got it right? " + p3isRight);
System.out.println("Game is over.");
break; //Game over, so break out of the loop
else {
//We must keep going because nobody got it right!

System.out.println("Players will have to try again.");
} //end if/else
} //end loop
} //end method
} //end class

I am new to these forums, so if I did something wrong please let me know :)
Does anyone know why this isn't working?

Answer Source

You are storing random number in local variable and you think you set it in instance variable

at line

int number = random2.nextInt(10);

change it to

this.number = random2.nextInt(10);

that atleast solves stated problem.

Also See

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download