954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Random number Game I made

0
By kylethedarkn on Sep 14th, 2006 7:18 am

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.

// The "Random_Number_Guesser" class.
import java.awt.*;
import hsa.Console;

public class Random_Number_Guesser
{
    static Console c;
    public static void main (String[] args)
    {
        c = new Console ();
        int on_off = 1;
        long number = (Math.round(Math.random() * 100));
        int guess;

            while (on_off == 1)
        {
          
            c.println ("Guess a number between 1 and 101!");
            guess = c.readInt ();
           
            if (number > guess)
            {
                c.println ("Nope, too low.");
            }

            if (number < guess)
            {
                c.println ("Nope, too high.");
            }

            if (number == guess)
            {
                c.println ("Congrats, You got it!");
                on_off = 0;
            }
        }
    }
}

Where is this hsa package?

Dukane
Posting Whiz in Training
295 posts since Oct 2006
Reputation Points: 45
Solved Threads: 29
 

Hey, I'm just learning about computers and right now I know next to nothing. Where would you input a code like this one?

sk8ndestroy14
Posting Virtuoso
1,852 posts since Mar 2007
Reputation Points: 437
Solved Threads: 1
 

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.

kylethedarkn
A.K.A. The Laughing Man
Team Colleague
628 posts since May 2006
Reputation Points: 55
Solved Threads: 39
 
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;
			}
		}

	}
}
StonedNpwnd
Newbie Poster
1 post since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You