Random number Game I made

kylethedarkn kylethedarkn is offline Offline Sep 13th, 2006, 10:18 pm |
0
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.
Quick reply to this message  
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. }
0
Dukane Dukane is offline Offline | Dec 18th, 2006
Where is this hsa package?
 
0
sk8ndestroy14 sk8ndestroy14 is offline Offline | 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?
 
0
kylethedarkn kylethedarkn is offline Offline | 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.
 
0
StonedNpwnd StonedNpwnd is offline Offline | Oct 11th, 2007
  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. }
 
 

Message:


Similar Threads
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC