DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Code Snippet: Random number Game I made (http://www.daniweb.com/forums/thread216790.html)

kylethedarkn Sep 13th, 2006 10:18 pm
Random number Game I made
 
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.

  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. }
Dukane Dec 18th, 2006 4:27 pm
Where is this hsa package?

sk8ndestroy14 Apr 9th, 2007 11:50 pm
Hey, I'm just learning about computers and right now I know next to nothing. Where would you input a code like this one?

kylethedarkn May 12th, 2007 5:09 pm
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.

StonedNpwnd Oct 11th, 2007 11:20 pm
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;
                        }
                }

        }
}


All times are GMT -4. The time now is 11:19 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC