A little game implemented by golang-guess numbers

Source: Internet
Author: User

Synchronize original:Http://www.waylau.com/golang-game-guess-numbe/


Generate a random number and enter a number to check whether the matching result is correct. If the matching result is correct, the system returns a high speed or a small value,

CodeAs follows:

 package mainimport (" bufio "" FMT "" Math/Rand "" OS "" strconv "" time ") vaR (endnum int // set the range of the generated number) func main () {I: = createrandomnumber (endnum) // FMT. println ("generate an integer within the specified range:", I) // FMT is used for debugging this sentence. println ("enter an integer in the range of 0-", endnum) Flag: = truereader: = bufio. n Ewreader (OS. stdin) for flag {data, _, _: = reader. readline () command, err: = strconv. atoi (string (data) // string to int, and determine the input format if Err! = Nil {FMT. println ("Incorrect format, please enter a number")} else {FMT. println ("the number you entered:", command) If command = I {flag = falsefmt. println ("congratulations, you got a correct answer ~ ")} Else if command  I {FMT. println (" the number you entered is greater than the generated number. Don't be discouraged! Next time ~ ") }}} Func Init () {endnum = 10} // generate an integer in the specified range // set the Starting number range to 0, endnum ends at func createrandomnumber (endnum INT) int {R: = Rand. new (Rand. newsource (time. now (). unixnano () return r. INTN (endnum)} 

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.