Random number Game I made

Please support our Java advertiser: Programming Forums
Sep 13th, 2006
Views: 19,165
Thread Rating: 4 votes, 4.0000 average.
AddThis Social Bookmark Button
Java Code that creates a random number between 1 and 100 and then asks the user to guess the number and tells if the guess was too high, too low or, the right number. Loops until right number is guessed.
java Syntax
  1. // The "Random_Number_Guesser" class.
  2. import java.awt.*;
  3. import hsa.Console;
  4.  
  5. public class Random_Number_Guesser
  6. {
  7. static Console c;
  8. public static void main (String[] args)
  9. {
  10. c = new Console ();
  11. int on_off = 1;
  12. long number = (Math.round(Math.random() * 100));
  13. int guess;
  14.  
  15. while (on_off == 1)
  16. {
  17.  
  18. c.println ("Guess a number between 1 and 101!");
  19. guess = c.readInt ();
  20.  
  21. if (number > guess)
  22. {
  23. c.println ("Nope, too low.");
  24. }
  25.  
  26. if (number < guess)
  27. {
  28. c.println ("Nope, too high.");
  29. }
  30.  
  31. if (number == guess)
  32. {
  33. c.println ("Congrats, You got it!");
  34. on_off = 0;
  35. }
  36. }
  37. }
  38. }
StonedNpwnd | Newbie Poster | Oct 11th, 2007
import java.util.Random;

import java.util.Scanner;


public class NumberGame
{
	public static void main(String[] args)
	{
		Random generator = new Random();
		Scanner in = new Scanner(System.in);
		
		int number = generator.nextInt(100) + 1;
		int on_off = 1;
		
		while(on_off == 1){
			System.out.print("Guess a number between 1 and 100: ");
			int guess = in.nextInt();
			
			if(guess < number){
				System.out.println("Too Low");
			}
			
			if(guess > number){
				System.out.println("Too High");
			}
			
			if(guess == number){
				System.out.println("Correct! You win!");
				on_off = 0;
			}
		}

	}
}
 
kylethedarkn | A.K.A. The Laughing Man | May 12th, 2007
You would input it into a IDE that would run it. As for the Hsa Console change the c to System.out in the printlns and delete the c in the c.readInt. That would probably work.  
sk8ndestroy14 | Posting Virtuoso | Apr 9th, 2007
Hey, I'm just learning about computers and right now I know next to nothing. Where would you input a code like this one?  
Dukane | Posting Whiz in Training | Dec 18th, 2006
Where is this hsa package?  

Only community members can submit or comment on code snippets. You must register or log in to contribute.

Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:06 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC