Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Reverse Number guessing game

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reverse Number guessing game

    I call this a reverse number guessing game because usually the computer would generate a number for you to guess, but I want the computer to guess my number by guessing randomly in decreasing ranges depending on if I say too high or too low.

    The biggest problem I'm having is making the range of the random number be correct. It likes to guess numbers out of the range.

    The computer starts off by giving me a number from 1 - 100. Then i type in "th", "tl", or "co" (too high, too low, correct).

    If I say too low the lowest number in the range should change to the previous guess and same for if i say too high (but it gets set to the previous highest number instead of previous lowest).

    This should decrease the range of the random generator until it gets to the point where the range is so small that the computer can only guess my number or it might just get lucky before that (just like a human).

    Here's the code:
    import java.util.*;	
     
    public class computer_guess 
    {
    	public static void main(String[] args) 
    	{
    		Scanner console;
    		console = new Scanner(System.in);
    		Random aRandom = new Random(); 
     
    		int  
    			prev_low = 1, 
    			prev_high = 100, 
    			guess = aRandom.nextInt(prev_high) + prev_low,
    			guesses = 0;
     
     
    	//	long time = 0; 
     
    		boolean win = false;
     
    		String feedback;
     
     
     
    		while(!win)
    		{
    			System.out.println("My guess is: " + guess);
    			feedback = console.nextLine();
    			//
     
    			if(feedback.equals("tl") || feedback.equals("th") || feedback.equals("co"))
    			{
    				if(feedback.equals("tl"))//too low
    				{
    					prev_low = guess;
    					System.out.println(feedback);
    				}
    				else if(feedback.equals("th"))//too high
    				{
     
    					prev_high = guess;
    					System.out.println(feedback);
    				}
    				else if(feedback.equals("co"))//correct
    				{
    					System.out.print("Yay, I WIN!");
    					win = true;
    					System.out.println(feedback);
    				}
     
    				guess = aRandom.nextInt(prev_high) + prev_low;
    				//++guesses;
    				//feedback = " ";
    				//System.out.print(feedback);
    			}
    		}
    	}
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Reverse Number guessing game

    You're generating a random number in the range of [prev_low, prev_low+prev_high). To generate one in the correct range, pass the difference between the high and low values to nextInt()

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reverse Number guessing game

    wow lol that was easy thanks!

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Reverse Number guessing game

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/42893-reverse-number-guessing-game.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reverse Number guessing game

    Sorry for the trouble, didnt know these forums were connected (are they?). And the lying part I wasnt meaning to lie i just wanted to give a quick answer. (other forum they said i lied which basically i did )
    Last edited by rarman555; April 23rd, 2011 at 08:12 PM.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Reverse Number guessing game

    No, the two forums are run by different groups. However, since we have a common interest (Java programming), there may happen to be people who are members of both forums.

Similar Threads

  1. Reverse Random Number Game
    By koryvandell in forum Java Theory & Questions
    Replies: 2
    Last Post: April 4th, 2011, 01:11 AM
  2. Number Guessing Game
    By JayK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 16th, 2011, 08:46 PM
  3. Guessing Number Game Help
    By Shadow20 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2011, 11:41 AM
  4. Guessing Game begginner question
    By okupa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2010, 07:31 AM
  5. Reverse Number
    By java1 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 28th, 2009, 10:19 AM