Results 1 to 5 of 5
  1. #1
    PixelatedIshy is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default 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.InputStreamReader;
    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
    	{
    		InputStreamReader stream = new InputStreamReader(System.in);
    		BufferedReader bf = new BufferedReader(stream);
    		int x = 0;
    		
    		while (x <= 1000)
    		{
    			x++;
    			int yourGuess = Integer.parseInt(bf.readLine());
    		}
    	}
    }
    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. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    PixelatedIshy is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default 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. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default 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 02:41 PM. Reason: changed signature
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    PixelatedIshy is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default 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.InputStreamReader;
    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
    	{
    		InputStreamReader stream = new InputStreamReader(System.in);
    		BufferedReader bf = new BufferedReader(stream);
    		int x = 0;
    		
    		while (x <= 1000)
    		{
    			int yourGuess = Integer.parseInt(bf.readLine());
    			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())
    		{
    			System.out.println("Correct Answer");
    		}
    		
    	}
    	
    	
    }
    Thank You.

Similar Threads

  1. Help?? Guess number game
    By eob13 in forum New To Java
    Replies: 2
    Last Post: 10-28-2013, 02:19 AM
  2. Need help with a Basic > guess a number Game
    By John2find in forum New To Java
    Replies: 8
    Last Post: 07-26-2013, 02:48 PM
  3. Simple number guessing game but something's wrong???
    By JohnPringle83 in forum New To Java
    Replies: 11
    Last Post: 05-29-2011, 08:43 PM
  4. Replies: 3
    Last Post: 12-20-2009, 03:22 PM
  5. guess number game
    By mistah in forum New To Java
    Replies: 10
    Last Post: 11-23-2008, 03:37 AM

Posting Permissions

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