Jump to content


(Beginners) Random Number Game


  • Please log in to reply
No replies to this topic

#1 nujumkey

nujumkey

    Member

  • Members
  • PipPip
  • 10 posts

Posted 15 February 2012 - 12:46 AM

Giving you the code is one thing but understanding what i did is another.

      Firstly using // causes java to void anything you write after, which is great for taking notes for yourself or anyone using your program.

ex
int n=0; //number to be guessed

      Also to produce a random number i used Math.random(), which generates a random number but in decimal form. so you have to multiply that number in order for the user to be able to guess it. also you have to typecast it as an int.

ex
n=(int)(Math.random()*l);

      Next up is input. Input can come from another file or the keyboard. In order to produce any game you need the user to enter something before someone did something. first you identify your scanner variable, then you name the scanner then make that scanner equal to new Scanner (System.in) so that input will come from the keyboard. I dont know how to get it from a text file but if you do please comment!

ex
Scanner input = new Scanner(in);

   remember to initialize something you have to first write what type it is, then give it a name, give it its value, then finish by any documentary.

ex.
int t=7; //tries

     and finally understand how if/else and do/while loops work. if/else loops work if the condition is true, do/while loops are the same but always run once and puts the conditional at the end.

and heres the code!

import java.awt.*; //tool kit
import java.awt.event.*; //key and mouse pressing in new window
import java.io.*; //for file/keyboard input
import java.net.*;  //for the internet
import java.util.*;
import java.applet.*; //adds extra graphics
import javax.swing.JOptionPane;
import static java.lang.Math.*;
import static java.lang.System.*;

public class numbergame {

    public static void main(String[] args) {

//definitions----------------------------------------------------------------------------------
String e=""; //players name
int l=100; //highest number
int t=7; //tries
int m=0; //missed
int n=0; //number
int x=0; //loop control variable
int g=0; //the user's guess
//input/processing-----------------------------------------------------------------------------
     Scanner input = new Scanner(in); //scanner so that keyboard input is possible
     n=(int)(random()*l)+1; //generating the random number
     m=t;
//game-----------------------------------------------------------------------------------------

out.println(" Hello, welcome to a number guessing game!."); //intoduction
out.println(" You have "+t+" tries to guess a number between 1 and "+l);
out.println(" Type your name to start.");
e=input.nextLine(); //substitue for the start button, not really needed
out.println(" Hello "+e+" have fun!");
do
{
out.println("Now guess the number!");
g=input.nextInt();
out.println("");
if(g>n)
{
out.println("Your guess was too high.");
}
if(g<n)
{
out.println("Your guess was too low.");
}
m--;
out.println("You have "+m+" tries left.");
out.println("");
}
while(g!=n || m<t);
if(g==n)
{
out.println("Oh hey, you got the number.");
out.println("   Pity it was fun playing.");

out.println("congradulations "+e);
}
if(t==0 &&x!=n)
{
out.println("Aww, wanna try again?");
out.println("This games so fun!!");
}

    } //BTW try to make it fun, it is a game
} // you dont want people to play it just once! try to at least customize the messages!