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.

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 main

Import ("Bufio"    "FMT"    "Math/rand"    "OS"    "StrConv"    "Time")var(Endnumint //Set the range of build numbers)func main(){i: = Createrandomnumber (Endnum)//fmt. Println ("generates integers within a specified range:", I)//This sentence is used for debuggingFmt. Println ("Please enter an integer, Range: 0-", endnum) Flag: =trueReader: = Bufio. Newreader (OS. Stdin) forFlag {Data, _, _: = Reader. ReadLine () command, err: = StrConv. Atoi (string(data))//string to int, and make the input format judgment        ifErr! = Nil {fmt. Println ("format is incorrect, please enter a number")        }Else{FMT. Println ("The number you entered:", command)ifCommand = = I {flag =falseFmt. Println ("Congratulations, you're right.")            }Else ifCommand < I {FMT. Println ("The number you entered is less than the number generated, don't lose heart!" One more time. ")            }Else ifCommand > I {FMT. Println ("The number you entered is greater than the number generated, don't lose heart!" One more time. ")            }        }    }}func init(){Endnum =Ten}//generate integers within a specified range//Set starting digital range, 0 start, endnum cutofffunc createrandomnumber(endnum int) int {r: = rand. New (Rand. Newsource (time. Now (). Unixnano ()))returnR.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.