Advertisement
|
This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Random 1-100
Posted by Patti on February 18, 2002 at 4:06 PM
Im supposed to write a program to generate a random 1-100, and then tell me whether I'm hi or lo or correct. It is also supposed to give a total count of the number of guesses. I'm not sure how to set the random between 1 and 100. I'm also having trouble getting the loops. Please help. import java.io.*; public class HiLo
{ public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); int answer = (int) (Math.random()); int totalguesses = 0; System.out.println ("Im thinking of a number between 1 and 100. Guess what it is:"); int guess = Integer.parseInt (stdin.readLine()); while (guess != answer) { if ( guess < answer) { System.out.println (" That is incorrect. You are too low. Try again"); totalguesses++; } else if (guess > answer) { System.out.println ("That is incorrect. You are too high. Try again."); totalguesses++; } else System.out.println ("You are corrrect. You win!!!"); System.out.println ("Total guesses :" + totalguesses); System.out.println ("Play again or choose 0 to QUIT"); } } }
Replies:
- java.util.Random senthoor February 19, 2002 at 7:11 AM
(4)
- Random Matt Gerrans February 19, 2002 at 1:00 PM
(3)
|