Simple guess the number small game

Source: Internet
Author: User

/**
The simple guess number games require the following:
User input to guess the range of numbers, enter 1000 is the number within the 0~1000, the program will be built into a 1 to 1000 between the number of
For guessing the result, the user guesses this number, the user guesses the result by the system prompt at a time: big, small or
Until the user guesses the result, the game ends. The user can exit the game in advance, that is, during the game
, the game terminates if the user enters the number 0.
Add a new feature: the number of times guessing function, prompt game start time, count the total time of guessing function, prompt game end

Ideas:
1. User input computer generated value range, receive and judge whether it is a reasonable value?
2. The game starts, showing the current system time.
3. Timings start running after the user has entered the correct range of values
4. Determine if user input number is 0,0 end game
5. The rest is judged to be greater than or less than the system number, every time you guess the counter + +
6. When the game is over, display the current system time.
*/

 Public classGuessnumber { Public Static voidMain (string[] args) {Scanner scan=NewScanner (system.in); Date Date=NewDate (); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss E"); String Nowtime=Sdf.format (date); /*The first bit can't be 0, it's 0, quit the game.*/String Userregex= "[1-9][\\d]+"; System.out.println ("Please enter the maximum number of 0~ you want to challenge"); String Userscan=Scan.next (); /*determine if a user entered a number that meets the requirements*/        if(Userscan.matches (Userregex)) {intnum =Integer.parseint (Userscan);        Guessgame (num); }Else{            /*Game End Time*/System.out.println ("The value range is illegal, can't start the game, Goodbye"); System.out.println ("Game End Time:" +nowtime); }    }     Public Static voidGuessgame (intnum) {Date Date=NewDate (); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss E"); String Nowtime=Sdf.format (date); Scanner Scan=NewScanner (system.in); Random Rand=NewRandom (); intCount = 0;//counter        intGuessnum =rand.nextint (num); System.out.println ("System has been generated"); LongStartTime =System.currenttimemillis (); System.out.println ("Please enter the number you guessed (0 exit):"); intScannum =Scan.nextint ();  while(Scannum! =guessnum) {            if(Scannum = = 0){                 Break; }Else if(Scannum >guessnum) {Count++; System.out.println ("Sorry, guess big, please continue to enter"); Scannum=Scan.nextint (); }Else if(Scannum <guessnum) {Count++; System.out.println ("Sorry, guess small, please continue to enter"); Scannum=Scan.nextint (); }        }        if(Scannum = = 0) {System.out.println ("A very bad stay"); System.out.println ("Game End Time:" +nowtime); }Else{            LongEndTime =System.currenttimemillis (); LongTotalTime = (endtime-starttime)/1000; System.out.println ("Congratulations, you used the" +count+ "time to guess right!"); System.out.println ("Your total time:" +totaltime+ "seconds"); System.out.println ("Game End Time:" +nowtime); }    }}
View Code

Simple guess the number small 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.