/*
Guess number game:
1, generating a random number.
2, get keyboard entry.
3, the input data into a number, and the random number comparison.
Give a hint.
4, repeat the process, if guessed, the program is over.
Note: For numbers other than input 1~100, as well as non-numbers to give a hint.
*/
Import java.io.*;
Import java.util.*;
Class Guessnumber
{
private int rannum;
Guessnumber ()
{
Random r = new Random ();
Rannum = R.nextint (+) +1;//(int) math.random () *100+1;
}
public void Play ()
{
system.out.println ("Game start: Enter a number between 1~100:");
boolean B = false;
while (!b)
{
try
{
int num = Getnum ();
if (num>=1 && num<=100)
b = IsLuck (num);
else
system.out.println ("Value out of range");
}
catch (IOException e)
{
throw New RuntimeException ("Data entry failed");
}
catch (numberformatexception e)
{
SYSTEM.OUT.PRINTLN ("Illegal data entered");
}
}
Get the number of the keyboard
private int Getnum () throws Ioexception,numberformatexception
{
BufferedReader bufr = new BufferedReader (new InputStreamReader (system.in));
int num = Integer.parseint (Bufr.readline ());
return num;
}
and random number comparisons
Private boolean isluck (int num)
{
Boolean B = false;
if (num>rannum)
System.out.println ("Big, continued");
else if (num<rannum)
System.out.println ("Small, continue");
Else
{
System.out.println ("In, congratulations");
B = true;
}
return b;
}
}
Class Guessnumberdemo
{
public static void Main (string[] args)
{
New Guessnumber (). Play ();
}
}
Guess the Java applet for the number game