wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes simple simple jave game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple simple jave game" Watch "simple simple jave game" New topic
Author

simple simple jave game

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120



i am trying to write a program that is like a simple game. the program picks a number at random, you try to guess this number, and with each guess the program tells you if you are too low or too high. it also counts the number of guesses that it take you before you guess correctly. im not sure where i messed up on this program but if anyone knows thank you in advance for your help.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
one other thing, i dont know why, but i am getting an error of illegal start of type on my while statement. i copied it directly from a previous program that worked, i dont understand why it isnt working.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
while (userGuess > secretNumber) && (userGuess < secretNumber){

hereis your problem. It is impossible for both conditions to be true. Therefore the code in your while loop is never executed.

p.s. it looks like you are missing a closing parenthesis on your while loop also.

p.s.s. you also have to surround your entire while condition by another pair of brackets,


nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i fixed the problem with my while statement, now my program will compile, but my output is still just a looped "enter your guess" after i enter my first guess.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Think about what you're doing: you're trying to loop until the user gets the number correct. There's a single loop; you have two. You only allow the user to enter one number.

It's also not clear to me why you have both a randomNumber and a secretNumber.

Please also consider putting closing brackets on a new line to make your program's structure easier to see.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I would also like to point out that you probably shouldn't create a new Random object every time you need a new random number.

All you need to do is create one instance of Random at the very beginning of your class, then use that object's nextInt() method when you need another random number.
 
 
subject: simple simple jave game