Guess the number game Java applet

Source: Internet
Author: User

/*
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.