This article describes the go language to realize the method of guessing the number of small games. Share to everyone for your reference. The specific analysis is as follows:
Randomly generated a number, enter a number to see whether the pair, matching the knot speed, the other tip is big or small
Copy Code code as follows:
Package Main
Import (
"Bufio"
"FMT"
"Math/rand"
"OS"
"StrConv"
"Time"
)
VAR (
Endnum int//Set the range of build number
)
Func Main () {
I: = Createrandomnumber (Endnum)
Fmt. Println ("Generate a specified range of integers:", i)//This sentence to try
Fmt. Println ("Please enter an integer, Range: 0-", Endnum)
Flag: = True
Reader: = Bufio. Newreader (OS. Stdin)
For flag {
Data, _, _: = Reader. ReadLine ()
Command, err: = StrConv. Atoi (String (data))//string to int and judged as input format
If Err!= nil {
Fmt. PRINTLN ("format is not correct, please enter a number")
} else {
Fmt. Println ("The number you entered:", command)
if command = = i {
Flag = False
Fmt. Println ("Congratulations, you answered it)"
else if command < I {
Fmt. Println ("The number you entered is less than the number generated, don't be discouraged!") One More ~ ")
else if command > I {
Fmt. Println ("The number you entered is greater than the number generated, don't be discouraged!") One More ~ ")
}
}
}
}
Func init () {
Endnum = 10
}
Generate an integer within a specified range
Set starting number range, 0 start, Endnum cutoff
Func createrandomnumber (endnum int) int {
r: = Rand. New (Rand. Newsource (time. Now (). Unixnano ()))
Return R.intn (Endnum)
}
I hope this article will help you with your go language program.