# Thread: Simple think of a number and guess game.

1. Member
Join Date
Dec 2013
Posts
5
Rep Power
0

## Simple think of a number and guess game.

Hey I'm new to Java and am building a small think of a number game. It involves a number generator to think of a number, a BufferedReader object for user input and then I would like to put into the program a too high, too low or correct system using a if statement. Here is my code, there are 2 separate classes.

Java Code:
```import java.io.BufferedReader;
import java.io.IOException;
import java.util.Random;

public class Main
{
public static void main(String[] args) throws IOException
{
RandomNumber randObject = new RandomNumber();
randObject.machineNumber();

Main input = new Main();
input.userInput();
}

public static void userInput() throws IOException
{
int x = 0;

while (x <= 1000)
{
x++;
}
}
}```
And the second class.

Java Code:
```import java.util.Random;

public class RandomNumber
{
public static void machineNumber()
{

Random theNum = new Random();
int number;
number = 1 + theNum.nextInt(100);

}

}```
I want to use an if statement, maybe in another class or within the RandomNumber class, but as a separate method, I want the if statement to go something like...

Java Code:
```if (yourGuess > number)
{
System.out.println("To high!");
}
etc```
But my problem is that if I put the if statement as a separate method inside the RandomNumber class then when using the yourGuess variable there is an error as that variable is within the main class. But if I then decide to put the if statement within the Main class then, when using the number variable in the if statement I get an error as the value of number is within the RandomNumber class. Also if you suggest that I should move everything into my Main class, I don't want to do that that as I want to build a program that has pieces too it, and not clumped all inside the main.

Can you help me out with a solution or direction of where to go in. Thank You.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,747
Rep Power
10

## Re: Simple think of a number and guess game.

There are lots of ways to do it. You could just return the random number from the method where you create it. Or simply store it in the class and then use another method to reference it via a get method.

Regards,
Jim

3. Member
Join Date
Dec 2013
Posts
5
Rep Power
0

## Re: Simple think of a number and guess game.

Do you mean just like this....

Java Code:
```import java.util.Random;

public class RandomNumber
{
public static int machineNumber()
{

Random theNum = new Random();
int number;
number = 1 + theNum.nextInt(100);
return number;

}

}```
But that still wont let me use the number variable inside of a if statement within the Main class. Or will it?

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,747
Rep Power
10

## Re: Simple think of a number and guess game.

If you want, you could do something like this.

Java Code:
```public class RandomNumber {
private int number;
public void machineNumber() {
Random theNum = new Random();
number = 1 + theNum.nextInt(100);
}
public int getNumber() {
return number;
}
}```
Then later.

Java Code:
```RandomNumber rn = new RandomNumber();
rn.machineNumber();

if (numb < rn.getNumber()) {
// do something
}```
Of course, this is all contrived to let you do your own methods and classes. You could also make parts static like you had or even generate the number in the class constructor. Normally you wouldn't keep making a method call to get a number which is not going to change during its use.

Regards,
Jim
Last edited by jim829; 12-19-2013 at 03:41 PM. Reason: changed signature

5. Member
Join Date
Dec 2013
Posts
5
Rep Power
0

## Re: Simple think of a number and guess game.

I kind of tried that, but it didn't work. I need a way of using just the value of a number that was produced in one method in another method. The error I occurred was in the second class RandomNumber, within the if statement when I used number = mainObject.getNumberGuess(). Can you help please.

Here's the code....

1st Class
Java Code:
```import java.io.BufferedReader;
import java.io.IOException;
import java.util.Random;

public class Main
{
public static void main(String[] args) throws IOException
{
RandomNumber randObject = new RandomNumber();
randObject.machineNumber();

Main input = new Main();
input.userInput();
}

private static int yourGuess;
public static int userInput() throws IOException
{
int x = 0;

while (x <= 1000)
{
x++;
}
return yourGuess;
}

public int getNumberGuess()
{
return yourGuess;
}
}```
2nd Class

Java Code:
```import java.util.Random;

public class RandomNumber
{

private int number;
public static void machineNumber()
{
RandomNumber randomObject = new RandomNumber();
Main mainObject = new Main();

Random theNum = new Random();
int number;
number = 1 + theNum.nextInt(100);

if (number = mainObject.getNumberGuess())
{
}

}

}```
Thank You.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•