# Number Guessing Game

• February 16th, 2011, 04:03 PM
JayK
Number Guessing Game
Hey, I'm brand new to Java and I just got an assignment in which I have to create a number guessing game. When you guess wrong, it is supposed to tell you the number is lower or higher depending on how you guess. when you get it right, it is supposed to tell you that you are correct and give you the amount of guesses you have taken. I was able to get up to here done. And hereafter is where I get really confused as to how to go about writing it.

After this, I am supposed to prompt the player by asking if they would like to play again (allowing the user to type a "yes" or "no" regardless of case), and they can keep playing. When they finally answer "no", my program is supposed to say how many games they played and the players average amount of guesses per game.

I have been working and working on this problem for nearly 4 hours now, getting it wrong every time. I know I'm going to have to have a loop inside of a loop, but as of right now I am completely lost and am looking for a push in the right direction. Any help would be great.
• February 16th, 2011, 04:04 PM
JayK
Re: Number Guessing Game
This is my program so far:
Code Java:

```import java.util.Scanner; class GuessNumber { public static void main(String[] args) { Scanner kb = new Scanner(System.in); int number, guess, guessCount;     System.out.println("Guess my number between 1 and 100"); number = (int)(100.0*Math.random()) + 1; guessCount = 0;   while (true) { System.out.println("Enter your guess"); guess = kb.nextInt(); guessCount++; if (guess == number) break; if (guess < number) System.out.println("Too low"); else System.out.println("Too high"); }   System.out.println("Right"); System.out.println("You took " + guessCount + " guesses"); System.out.println("Do you want to play again? Enter Y/N");```
• February 16th, 2011, 06:24 PM
javapenguin
Re: Number Guessing Game
while(true)

is not valid.

if you had a boolean called

playAgain and set it to true to begin with

the statement

while(playAgain == true)

would be valid.

As for guess lower and guess higher, I did find that the Integer class has a compareTo() method that returns 0 if equal, something greater than 0 if greater and something less than 0 if less than.

You could simply do this:
Code java:

```  boolean guessRight = false; int numberOfGuesses = 0; while (guessedRight == false) { int number2 = (int)(100.0*Math.random()) + 1;   Integer temp2 = number2; int number = kb.nextInt();   Integer temp = number;   if (temp.compareTo(temp2) ==0) { System.out.println("You guessed correctly!"); System.out.println(numberOfGuesses); numberOfGuesses++; guessedRight = true; }   else { if (temp.compareTo(temp2) < 0) { System.out.println("You guessed lower than number."); numberOfGuesses++; }   else { System.out.println("You guessed higher than number."); numberOfGuesses++; }   } }```
• February 16th, 2011, 06:30 PM
copeg
Re: Number Guessing Game
Quote:

Originally Posted by javapenguin
while(true)

is not valid.

Based upon what information, because stating the above is wrong. Did you try it? Why would you say it is invalid?
• February 16th, 2011, 07:25 PM
javapenguin
Re: Number Guessing Game
Quote:

Originally Posted by copeg
Based upon what information, because stating the above is wrong. Did you try it? Why would you say it is invalid?

I'll admit I haven't tried it.

However, does

while(true) sound valid to you?

I have seen

while(variable == true)

and while (method())

but never

while (true).

Ok, I did find that's valid (don't know why it is) but as I've no clue what the true refers to, it still makes no sense to me from what I've learned.

I've put

while(true)
{

}

statement

and it never got to statement

I also put

while(false)
{
}
statement

and once again it never got to statement.

Even if the syntax is valid, I don't see any way out other than a break or a System.exit(0);

I suppose if you wanted to be technical, you could:

Code java:

```while(true) {   if (someCondtionIsMet) break; }```

:confused: :confused:
• February 16th, 2011, 08:46 PM
copeg
Re: Number Guessing Game
Quote:

However, does

while(true) sound valid to you?
Yes, it does. Anything is 'valid' if it compiles and behaves the way you want it to.

Quote:

Even if the syntax is valid, I don't see any way out other than a break or a System.exit(0);
And returning from the method, and throwing an Exception (although this would be bad practice).