I have a program here that requires a user to enter a guess from 1-100 and then tells the user whether he/she went to high or to low. I'm very new to JAVA and trying to figure all this stuff out..heres a copy of the program. The program comes up with no compiler errors, but when i enter the guess the program freezes on me and doesnt go with the loop..any suggestions?
The problem is the semicolon before the body of your while loop: while(number != guess);
If you remove that semicolon (and start count at 1 rather than 0), the program seems to work fine...
[ October 14, 2004: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Oct 14, 2004
Thank you much, Sometimes I seem to overthink the situation with programs. It's good to see that I had the whole random number thing correct, Couldnt find anything on it in the book I have. I have a handful of other programs with while and do..while loops within them that I had the semicolon there at the end, which put a hault on them aswell. Thank you again, Shawn
First key point: Math.random() generates a double between 0.0 and 1.0. Technically, this interval includes 0.0, but not 1.0. (I say "technically" because the chances of actually getting 0.0 is... Well, rather low.)
So multiplying the double Math.random() by 100 then gives a double between 0.0 and 100.0 -- including 0.0, but not 100.0. Adding 1 then gives a double between 1.0 and 101.0 (not including 101.0).
Second key point: Casting a floating-point primitive to a integral primitive always truncates (note that an explicit cast is always needed in these cases, even if going from a 32-bit float to a 64-bit long). So casting the above double to an int will give an int between 1 and 100 inclusively (since the double could be up to -- but not actually including -- 101.0). [ October 15, 2004: Message edited by: marc weber ]