944,129 Members | Top Members by Rank

Ad:
  • Java Code Snippet
  • Views: 41593
  • Java RSS
0

Random number Game I made

by on Sep 13th, 2006
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 Code Snippet (Toggle Plain Text)
  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. }
Comments on this Code Snippet
Dec 18th, 2006
0

Re: Random number Game I made

Where is this hsa package?
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006
Apr 9th, 2007
0

Re: Random number Game I made

Hey, I'm just learning about computers and right now I know next to nothing. Where would you input a code like this one?
Posting Virtuoso
sk8ndestroy14 is offline Offline
1,851 posts
since Mar 2007
May 12th, 2007
0

Re: Random number Game I made

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.
A.K.A. The Laughing Man
kylethedarkn is offline Offline
600 posts
since May 2006
Oct 11th, 2007
0

Re: Random number Game I made

Java Syntax (Toggle Plain Text)
  1. import java.util.Random;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class NumberGame
  7. {
  8. public static void main(String[] args)
  9. {
  10. Random generator = new Random();
  11. Scanner in = new Scanner(System.in);
  12.  
  13. int number = generator.nextInt(100) + 1;
  14. int on_off = 1;
  15.  
  16. while(on_off == 1){
  17. System.out.print("Guess a number between 1 and 100: ");
  18. int guess = in.nextInt();
  19.  
  20. if(guess < number){
  21. System.out.println("Too Low");
  22. }
  23.  
  24. if(guess > number){
  25. System.out.println("Too High");
  26. }
  27.  
  28. if(guess == number){
  29. System.out.println("Correct! You win!");
  30. on_off = 0;
  31. }
  32. }
  33.  
  34. }
  35. }
Newbie Poster
StonedNpwnd is offline Offline
1 posts
since Oct 2007
Message:
Previous Thread in Java Forum Timeline: jvar.getText(long)??????????
Next Thread in Java Forum Timeline: Arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC