Hi,
I am new to java.Please help me in solving this problem.
I have written a small program proably took the help from the book Head first
java.
Its giving me compilation error when i am trying to access variable GNum ie at lines Player1Num =Player1.GNum;,Player2Num =Player2.GNum; and Player3Num =Player3.GNum;.
Error coming is "Cannot find sysmbol variable GNum".How do I access the variable of a method defined in a class from another class.
This is the same way this code is written in head first java.
If any one has the answer please reply.
class GuessGame
{
Player Player1 = new Player();
Player Player2 = new Player();
Player Player3 = new Player();
int Player1Num=0;
int Player2Num=0;
int Player3Num=0;
boolean P1IsRight =false;
boolean P2IsRight =false;
boolean P3IsRight =false;
public void StartGame()
{
int targetNumber = (int) (Math.random() * 10);
System.out.println("Let s start the game of gussing the numbers");
while(true)
{
System.out.println("Its the turn of Player1. Please guess a number between 0 and 9");
Player1.Guess();
Player1Num =Player1.GNum;
System.out.println("Its the turn of Player2. Please guess a number between 0 and 9");
Player2.Guess();
Player2Num =Player2.GNum;
System.out.println("Its the turn of Player3. Please guess a number between 0 and 9");
Player3.Guess();
Player3Num =Player3.GNum;
if(Player1Num == targetNumber)
{
P1IsRight = true;
}
if(Player2Num == targetNumber)
{
P2IsRight = true;
}
if(Player3Num == targetNumber)
{
P3IsRight = true;
}
if (P1IsRight || P2IsRight || P3IsRight)
{
System.out.println("We have winner");
System.out.println("Is Player1 a winner?" + P1IsRight);
System.out.println("Is Player2 a winner?" + P2IsRight);
System.out.println("Is Player3 a winner?" + P3IsRight);
break;
}
else
{
System.out.println("Lets play the game again as we dont have any winner");
}
}
}
}
class Player
{
int GuessNum;
public void Guess()
{
int GNum = (int) (Math.random() * 10);
System.out.println("I am guessing the number as" +GNum);
}
}
public class GameLauncher
{
public static void main(
String[] args)
{
GuessGame Game = new GuessGame();
Game.StartGame();
}
}