Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-28-2005, 05:00 PM   PM User | #1
punx
New Coder

 
Join Date: Aug 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
punx is an unknown quantity at this point
[Java] Help with guessing game.

Code:
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?
		}	
		
	}
}
I am trying to get it to keep letting the user guess while the number is wrong... What am I doing wrong here?
punx is offline   Reply With Quote
Old 09-28-2005, 05:24 PM   PM User | #2
TheShaner
Senior Coder

 
TheShaner's Avatar
 
Join Date: Sep 2005
Location: Orlando, FL
Posts: 1,125
Thanks: 2
Thanked 40 Times in 40 Posts
TheShaner will become famous soon enoughTheShaner will become famous soon enough
You need to restructure big time. You only enter that if statement if the guessed number and random number aren't equal, but then inside of it, you check to see if they're equal? And then you assume that the code will loop in the if statement. You could have set it up to have an inner loop that loops until the guessed number and random number are equal or you could have set it up like the one below. You should really iterate on paper and see where the problem is. It helps a lot. But here is the corrected code anyway.

Code:
while (runAgain == 'y' || runAgain == 'Y')
{

  guessedNumber = reader.readDouble("Please pick a number between 1 and 100");

  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)
  {
    /* 
        Since you got the number, ask to play again. If it is y, 
        then you'll notice the that the while loop will continue,
        and a new randomNumber will be generated.
        If it isn't y, then it'll break the loop before asking for a
        number again. 
    */
    System.out.println("You got it!");
    runAgain = reader.readChar("Play again (y/n)?");
    if runAgain == 'y'
      randomNumber = Math.random() * (100);
  }

}
-Shane

Last edited by TheShaner; 09-28-2005 at 05:29 PM..
TheShaner is offline   Reply With Quote
Old 09-29-2005, 04:40 PM   PM User | #3
punx
New Coder

 
Join Date: Aug 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
punx is an unknown quantity at this point
I've decided to use a while statement to loop, but when I compile and run, it enters an infinate loop, instead of doing what I want to do. Obviously I am doing something wrong. Heres the pseducode:
Code:
if (number is correct) System.out.println("Good job")

else if (number is too high) System.out.println("Too high, guess again")
(then loop and give user chance to guess again)

else if (number is too low) System.out.println("Too low, guess again")
(then loop and give user chance to guess again)
Heres what I have:
Code:
while (runAgain == 'y' || runAgain == 'Y')				
{
guessedNumber = reader.readDouble("Please pick a number between 1 and 100: ");
			
while (guessedNumber != randomNumber)
     {
          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!");
     }	
runAgain = reader.readChar("Play again (y/n)? ");			
}
punx is offline   Reply With Quote
Old 09-29-2005, 11:21 PM   PM User | #4
TheShaner
Senior Coder

 
TheShaner's Avatar
 
Join Date: Sep 2005
Location: Orlando, FL
Posts: 1,125
Thanks: 2
Thanked 40 Times in 40 Posts
TheShaner will become famous soon enoughTheShaner will become famous soon enough
You need this line:

Code:
guessedNumber = reader.readDouble("Please pick a number between 1 and 100: ");
Inside your second while loop. That's why you have the infinite loop. You never ask for the number again, so it keeps processing on the very same number.

-Shane
TheShaner is offline   Reply With Quote
Old 09-30-2005, 04:35 PM   PM User | #5
punx
New Coder

 
Join Date: Aug 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
punx is an unknown quantity at this point
I finally got it. Instead of using Math.random, I used the random Int generator.
Code:
import TerminalIO.*;
import java.util.Random;

public class project6_1GuessingGame
{
	public static void main (String[] args)
	{
		KeyboardReader reader = new KeyboardReader();
		Random random = new Random();
		
		int randomNumber,
		guessedNumber = 0;
		char runAgain = 'y';
		
		randomNumber = random.nextInt(101);									
		System.out.println(randomNumber);
		while (runAgain == 'y' || runAgain == 'Y')
		{
			guessedNumber = reader.readInt("Please pick a number between 1 and 100: ");
			if (guessedNumber < randomNumber)
					System.out.println("Too low!\nGuess again!" +"\n");
				if (guessedNumber > randomNumber)
					System.out.println("Too high\nGuess again!" +"\n");
				if (guessedNumber == randomNumber)
					System.out.println("You got it!" +"\n");
			while (guessedNumber != randomNumber)
			{
				guessedNumber = reader.readInt("Please pick a number between 1 and 100: ");
				if (guessedNumber < randomNumber)
					System.out.println("Too low!\nGuess again!" +"\n");
				if (guessedNumber > randomNumber)
					System.out.println("Too high\nGuess again!" +"\n");
				if (guessedNumber == randomNumber)
					System.out.println("You got it!" +"\n");
			}	
			runAgain = reader.readChar("Play again (y/n)? ");
			if (runAgain == 'n' || runAgain == 'N')
			{
				System.exit(0);
			}
		}
	}
}
Thanks for the help guys
punx is offline   Reply With Quote
Old 10-14-2010, 03:46 AM   PM User | #6
xduh9
New to the CF scene

 
Join Date: Oct 2010
Location: philipines
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
xduh9 is an unknown quantity at this point
how to save in vb?

need help here...
xduh9 is offline   Reply With Quote
Old 10-14-2010, 11:49 AM   PM User | #7
vinyl-junkie
$object->toCD-R(LP);


 
vinyl-junkie's Avatar
 
Join Date: Jun 2003
Posts: 3,053
Thanks: 2
Thanked 22 Times in 22 Posts
vinyl-junkie is on a distinguished road
Quote:
Originally Posted by xduh9 View Post
need help here...
See post #4 in this thread.
__________________
Music Around The World - Collecting tips, trade
and want lists, album reviews, & more
SNAP to it!
vinyl-junkie is offline   Reply With Quote
Old 10-15-2010, 09:24 AM   PM User | #8
ice800
Banned

 
Join Date: Oct 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ice800 is an unknown quantity at this point
i have learnt something different by viewing the post. and thanks TheShaner!
ice800 is offline   Reply With Quote
Old 10-27-2010, 11:12 PM   PM User | #9
Superteo1987
New Coder

 
Join Date: Oct 2010
Location: Mountain View
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Superteo1987 is an unknown quantity at this point
I do have to create a similar program but I got 2 problems. In my case is the user that chose the numer rage for the guessing game...I don't know how to do that. Second I have to keep a counter for all the user try. I need two class a main class with the gui and game class that represents one play of the Game....

Someone can give me some idea how to proceed ?

Thanks
Superteo1987 is offline   Reply With Quote
Old 10-28-2010, 12:31 AM   PM User | #10
Gox
Regular Coder

 
Gox's Avatar
 
Join Date: May 2006
Location: Ontario, Canada
Posts: 392
Thanks: 2
Thanked 20 Times in 20 Posts
Gox will become famous soon enough
Quote:
Originally Posted by Superteo1987 View Post
I do have to create a similar program but I got 2 problems. In my case is the user that chose the numer rage for the guessing game...I don't know how to do that. Second I have to keep a counter for all the user try. I need two class a main class with the gui and game class that represents one play of the Game....

Someone can give me some idea how to proceed ?

Thanks
It is best if you start a new thread. Clearly state the problem(s) you're having, and post your code so that we can help identify where you went wrong.
Gox is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:27 PM.


Advertisement
Log in to turn off these ads.