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)}