5 Replies - 568 Views - Last Post: 13 April 2012 - 01:23 PM Rate Topic: -----

#1 mrme2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-April 12

Write a Java program to play a guessing game with a user

Posted 13 April 2012 - 10:17 AM

I need the game to offer to finish the game or continue playing if the difference between their guess and the target if 5 or less, . If they elect to continue playing this offer is not repeated for the rest of the game no matter what numbers they guess. Also i want the program to allow the user to decide to play another game or terminate the program once a game has finished. This is the code i have so far.

import TerminalIO.*;

public class GuessingGame
{
	public static void main (String[] args)
	{
		KeyboardReader reader = new KeyboardReader();
		
		double randomNumber,													//The number to be guessed
		guessedNumber;															//The number guessed by the player.
		char runAgain = 'y';													//Run again option
		
		randomNumber = Math.random() * (100);									//Determines the number
		
		while (runAgain == 'y' || runAgain == 'Y')								//Play again?
		{
			guessedNumber = reader.readDouble("Please pick a number between 1 and 100");														//
			
			if (guessedNumber != randomNumber)									//Loop untill the user guesses the correct number
			{																	//
				if (guessedNumber < randomNumber)								//
					System.out.println("Too low!\nGuess again!");				//
				else if (guessedNumber > randomNumber)							//												//
					System.out.println("Too high\nGuess again!");				//
				else if (guessedNumber == randomNumber)							//
					System.out.println("You got it!");
					
			guessedNumber = reader.readDouble("Please pick a number between 1 and 100");
			}
				
			runAgain = reader.readChar("Play again (y/n)?");					//Play again?
		}	
		
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Write a Java program to play a guessing game with a user

#2 IamTw_  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 238
  • Joined: 25-February 11

Re: Write a Java program to play a guessing game with a user

Posted 13 April 2012 - 12:43 PM

Hey

What is the KeyboardReader class, is that your own class? I'd use a scanner if I was to do this. This reads user input
Scanner scanner = new Scanner(System.in);


Remember to write the line of code that uses the scanner instance ofcourse.

For the number to guess, I'd use an int instead of double, it is more realistic, and the user would not have to worry that the number to guess isnt 3, 3.4 or 3.9 etc.

You are on the right track, but your code is a bit messy. Here is an example of the Random class usage
Random random = new Random();
int nrToGuess = random.nextInt(100);


The above code will generate a random number between 0 and 99.
Now the guessing
boolean found = false;
while(found is not true){
     userInput = scanner.nextInt();
       if(userInput is equal to nrToGuess){
            we guessed the right number
            break;
           }
       }


Regarding giving the choice to try again.
You can also use scanner with Strings. And another thing... Instead of checking for big and small letters (From user),
you can just make them to lower or uppercase letters before checking them for the value.
So something like this
String yes = scanner.next() // Or maybe scanner.nextLine(), I dont remember the difference
String letterConvertedToLowerCase = yes.toLowCase();


Now you can check for if it was "yes or no", with out worrying about "YES or NO"
Now the rest I think you can figure out, you are pretty much did it, just check for the input, and make the choice to go on with the guessing game. If the user choses "no" and wants to stop, you can just use a break statement like I did above.
For the target, to find out if it is 5 or less, introduce a counter variable to keep track of the nr of attempts.
Hope this help, otherwise you are welcome to ask again
Was This Post Helpful? 1
  • +
  • -

#3 Air Legend  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 101
  • Joined: 28-November 11

Re: Write a Java program to play a guessing game with a user

Posted 13 April 2012 - 12:47 PM

boolean found = false;
	while(found is not true){
	     userInput = scanner.nextInt();
	       if(userInput is equal to nrToGuess){
	            we guessed the right number
	            break;
	           }
	       }



why not write like this instead? Makes more logical sense:

	while(true){
	     userInput = scanner.nextInt();
	       if(userInput is equal to nrToGuess){
	            we guessed the right number
	            break;
	           }
	       }


Was This Post Helpful? 0
  • +
  • -

#4 IamTw_  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 238
  • Joined: 25-February 11

Re: Write a Java program to play a guessing game with a user

Posted 13 April 2012 - 12:58 PM

View PostAir Legend, on 13 April 2012 - 12:47 PM, said:

boolean found = false;
	while(found is not true){
	     userInput = scanner.nextInt();
	       if(userInput is equal to nrToGuess){
	            we guessed the right number
	            break;
	           }
	       }



why not write like this instead? Makes more logical sense:

	while(true){
	     userInput = scanner.nextInt();
	       if(userInput is equal to nrToGuess){
	            we guessed the right number
	            break;
	           }
	       }


Yeah, that's another way to do it, though a minor difference. I like to instanciate the boolean primitive type, that was how it helped me get the hang of it when I was a beginner :)
Was This Post Helpful? 0
  • +
  • -

#5 Air Legend  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 101
  • Joined: 28-November 11

Re: Write a Java program to play a guessing game with a user

Posted 13 April 2012 - 01:00 PM

View PostIamTw_, on 13 April 2012 - 12:58 PM, said:

View PostAir Legend, on 13 April 2012 - 12:47 PM, said:

boolean found = false;
	while(found is not true){
	     userInput = scanner.nextInt();
	       if(userInput is equal to nrToGuess){
	            we guessed the right number
	            break;
	           }
	       }



why not write like this instead? Makes more logical sense:

	while(true){
	     userInput = scanner.nextInt();
	       if(userInput is equal to nrToGuess){
	            we guessed the right number
	            break;
	           }
	       }


Yeah, that's another way to do it, though a minor difference. I like to instanciate the boolean primitive type, that was how it helped me get the hang of it when I was a beginner :)


ah, I see. Just uses a little more memory so just trying to be as efficient as possible ;)
Was This Post Helpful? 1
  • +
  • -

#6 IamTw_  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 238
  • Joined: 25-February 11

Re: Write a Java program to play a guessing game with a user

Posted 13 April 2012 - 01:23 PM

Actually, I never thought of it using more memory. That's a good point and reason to change it for me :-)
Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1