Golang implementation of a small game – guess numbers

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Original text sync to: http://www.waylau.com/golang-game-guess-numbe/


Randomly generate a number, enter a number to see if the pair, matching the junction speed, the opposite hint is big or small,

The code is as follows:


Package Mainimport ("Bufio" "FMT" "Math/rand" "OS" "StrConv" "Time") var (endnum int//Set the range of generated numbers) Func main () {i: = Createrandomnumber (Endnum)//fmt. PRINTLN ("Generates an integer within the specified range:", i)    //This sentence is debugged with FMT. Println ("Please enter an integer, Range: 0-", endnum) Flag: = Truereader: = Bufio. Newreader (OS. Stdin) for flag {data, _, _: = Reader. ReadLine () command, err: = StrConv. Atoi (String)//string to int and makes an input format to determine if err! = Nil {fmt. PRINTLN ("Format is incorrect, enter a number")} else {fmt. Println ("You enter the number:", command) if command = = i {flag = falsefmt. Println ("Congratulations, right")} else if command < I {FMT. Println ("The number you entered is less than the number generated, don't be discouraged!") One more Time ~ ")} else if command > I {fmt. Println ("The number you entered is greater than the number generated, don't be discouraged!") One more Time ~ ")}}}}func init () {endnum = 10}//generates integers within the specified range//Set starting numeric range, 0 start, Endnum cutoff func createrandomnumber (endnum int) int {r: = Rand. New (Rand. Newsource (time. Now (). Unixnano ())) return R.intn (Endnum)}


Related Article

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.