Jump to content

(Beginners) Random Number Game

  • Please log in to reply
1 reply to this topic

#1 nujumkey



  • 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.

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.


      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!

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.

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) {

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
     Scanner input = new Scanner(in); //scanner so that keyboard input is possible
     n=(int)(random()*l)+1; //generating the random number

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!");
out.println("Now guess the number!");
out.println("Your guess was too high.");
out.println("Your guess was too low.");
out.println("You have "+m+" tries left.");
while(g!=n || m<t);
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!

#2 AnnaPlub


    Advanced Member

  • Members
  • PipPipPip
  • 39 posts

Posted 09 June 2016 - 10:03 PM

Based on this tip from Calguy1000, the variant to create the random background for the header-container....

This should be put in the template.

Replace the:


XRumer 12.0.17 + SocPlugin : Most popular app for mass posting in FB, VK, OK, Mamba