Results 1 to 1 of 1
  1. #1
    celtics is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default Help with Random Number Generator

    Hey guys

    I need to make a program that is a guessing game. The user enters a number from 1 to 20 and my program tries to guess it using its previous guesses as parameters in a random number generator. The problem I'm having is how to generate a random number from the first guess to the number 20 (look at my firstGuessLow method) and how to generate a random number from the lowest possible guess to the highest possible guess (look at my secondGuess method).


    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class GuessingGame {
    
    	Scanner keyboardReader = new Scanner(System.in);
    	Random numberGenerator = new Random();
    	int number, userNumber, highNum, lowNum, minMaxNum;
    	String input;
    
    	
    	public GuessingGame(Scanner input)
    	{
    		keyboardReader = input;
    		number = numberGenerator.nextInt();	
    
    	}
    	public void StartGame()
    	{
    		/* Print an invitation to the user to think
    		*  of a number from 1 to 20
    		*/
    		System.out.println("Type a number from 1 to 20");
    		userNumber = keyboardReader.nextInt();
    		
    		// Give the user the first guess and store it in number
    		System.out.println("I will try to guess your number.");
    		number = numberGenerator.nextInt(20) + 1;
    		
    		// Consume the remaining newline
    		keyboardReader.nextLine();
    		
    		System.out.println("My guess is " + number + ". Is this number high, low, or correct?");
    		input = keyboardReader.nextLine();
    		
    		if (input.equals("high")){
    			firstGuessHigh(number);
    		}
    		else if (input.equals("low")){
    			firstGuessLow(number);
    		}
    		else if (input.equals("correct"))
    		{
    			System.out.println("I win!");
    		}
    	}
    	public void firstGuessHigh (int highestValue)
    	{
    		/* 
    		 * Generate a random number from 1 to highestValue
    		 */
    		highNum = numberGenerator.nextInt(highestValue) - 1;
    		
    		System.out.println("My second guess is " + highNum + ". Is this number high, low, or correct?");
    		input = keyboardReader.nextLine();
    		if (input.equals("high")){
    			secondGuess(1, highNum);
    		}
    		else if (input.equals("low")){
    			secondGuess(highNum, number);
    		}
    		else if (input.equals("correct"))
    		{
    			System.out.println("I win! It only took me two tries!");
    		}
    		
    	}
    	public void firstGuessLow (int lowestPossible)
    	{
    		/*
    		 * Generate a random number from lowestPossible to 20
    		 */
    		
    		lowNum = numberGenerator.nextInt(lowestPossible) + lowestPossible + 1;
    		System.out.println("My second guess is " + lowNum + ". Is this number high, low, or correct?");
    		input = keyboardReader.nextLine();
    		if (input.equals("high")){
    			secondGuess(number, lowNum);
    		}
    		else if (input.equals("low")){
    			secondGuess(lowNum, 20);
    		}
    		else if (input.equals("correct"))
    		{
    			System.out.println("I win! It only took me two tries!");
    		}
    	}
    	public void secondGuess (int minGuess, int maxGuess)
    	{
    		// Generate a random number from minGuess to maxGuess
    		minMaxNum = numberGenerator.nextInt(maxGuess) + minGuess;
    				
    		System.out.println("My final guess is " + minMaxNum + ". Is this number correct?(Please type yes or no)");
    		input = keyboardReader.nextLine();
    		if (input.equals("yes")){
    			System.out.println("I win! It only took me three tries!");
    		}
    		else if (input.equals("no")){
    			System.out.println("You win! Congratulations!");
    		}
    	}
    }
    Last edited by celtics; 03-07-2011 at 09:25 PM.

Similar Threads

  1. Random number generator
    By zerwik in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 01:10 PM
  2. Random Generator
    By Learning Java in forum New To Java
    Replies: 9
    Last Post: 05-31-2010, 09:15 PM
  3. Random number generator
    By Michailangelo in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 07:47 PM
  4. Help with class project, random number generator.
    By Christopher The Great in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 03:12 AM
  5. Random Generator
    By padutch2 in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 07:43 PM

Posting Permissions

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