aspose file tools*
The moose likes Beginning Java and the fly likes guessing game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "guessing game" Watch "guessing game" New topic
Author

guessing game

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120


i am trying to make a program that will create a secret number, once it has been randomly selected the program runs a loop until guessing random numbers until it finds the secret number. but, my program simply creates a random number to try to guess the secret number only once and continually loops that one number down the screen. im not sure what is wrong, but if someone knows, please help.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i looked through the apis and found a better way to create my random number, but it still doesnt work properly.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41509
    
  53
There's no statement that would create a new random number in the loop, that's why it loops endlessly.

I'm not sure why you're checking for "<"; from your description it sounds as if you should be checking for "!=" - in which case the the second while statement is superfluous, because both numbers are guaranteed to be equal at that point.


Ping & DNS - my free Android networking tools app
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
so would this work any better? im using blue j and when i compile i dont get any errors, but it wont run.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41509
    
  53
The secret number should only be calculated once, shouldn't it? And that should happen before the loop starts.

As it is, both numbers are zero initially, so the loop is never executed because it checks for inequality.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

Also, if the guess is wrong, don't you want to print out what was the guess, before you generate another guess?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i figured it out. thank you guys for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: guessing game