Go language to realize the method of guessing the number of small games _golang

Source: Internet
Author: User
Tags rand

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.

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.