Home>

The following is a quotation snippet from the Java language guessing game:

import java.text.simpledateformat;
 import java.util.date;
 import java.util.scanner;
 //Main function
 public class calssone {
  public static void main (string [] args) {
  //shit + ctrl + o
  int result;
  //randomly generate a number within 100
  int number=(int) (math.random () * 100);
  system.out.println ("\ n *********** Guess the digital game,Can you hold it?********* ");
  system.out.println ("\ n ******** Random number generation:don't tell you! ********* \ n");
  system.out.println ("\ n *********** Answer:" + number + "*************** \ n");
  system.out.println ("Let's use our brains to guess,Tip:he is an integer from 1 to 100 ");
  long starttime=system.currenttimemillis ();//Define a time variable
  for (int i=1;i<100;i ++) {
   system.out.println ("Please enter your" + i + "guess");
   result=calssone.guess (i);//Get the input result by calling the input function
   //Output the console by comparison
   if (result>number)
    system.out.println ("Sorry,The number you guessed is greater than the mystery number! ");
   else if (result<number)
    system.out.println ("Sorry,The number you guessed is less than the mystery number! ");
   else {
    simpledateformat snowdate=new simpledateformat ("yyyy-mm-dd hh:mm:ss");
    long sendtime=system.currenttimemillis ();
    system.out.println ("\ n *********** Correct answer:" + number + "*************** \ n");
    if (i == 1) {
     system.out.println ("perfect !! Congratulations! Hit it in one go !!");
    }
    else if (i<10) {
     system.out.println ("good job! You guessed a total of" + i + "times, and keep going !!");
    }
    else {
     system.out.println ("not bad! You have guessed" + i + "times, a long way to go!");
    }
    system.out.println ("current time:" + snowdate.format (new date ()));//new date () is to get the current system time
    //system.out.println("Current time:"+ snowdate);
    system.out.println ("Elapsed time:" + (sendtime-starttime)/1000 + "second");
   return;
   }
  }
  }
  //input function
  public static int guess (int i) {
   //By importing java.util.scanner class package
  scanner sc=new scanner (system.in);
  int result;
  try {
   //Make the content entered in the console a number
   result=sc.nextint ();
   return result;
  }
  catch (exception e) {
   //todo:handle exception
  system.out.println ("You did not enter a number,Please re-enter the "+ i +" number ");
  //Call this function to re-enter
  guess (i);
  }
  return 0;
  }
 }
  • Previous Record the deadlock process of the company's warehouse database server and its solution
  • Next Android desktop component App Widget complete case