<> preface

Blogger Xiaobai , Just learning Go language . I heard that many friends like to play games with their girlfriends . Bloggers are not good at playing games , It's all about playing games , But after the number guessing game , Bloggers have regained their confidence in this game . I believe in the eyes of many female friends , A boy who can play number guessing games , It must be a charming boy ! Let's go to the waves together (Let’s
Golang)!

<> demand

Design a number guessing game , Do a random generation guessNum, Then enter the number you guessed , When guessing big, output guessing big , Guess small output guess small , If you guessed right, you guessed right . Then output some surprise words .

<> Construct the basic framework
package main import ( "fmt" "math/rand" "strconv" "time" ) func main() { //
-----1---- rand.Seed(time.Now().Unix()) answer := rand.Intn(1000) for{ fmt.
Println(" Please input your guess :( input “ end ” End the game )") var guess string fmt.Scan(&guess) //------2-----
guessNum, _ := strconv.Atoi(guess) //------3----- if guess == " end "{ fmt.Println
("I Love You!Good Bye!") break } //-----4------ switch { case guessNum > answer:
fmt.Println(" Guess big ") case guessNum < answer: fmt.Println(" Guess it's too small ") default: fmt.
Println(" That's right ") break } } }
Let's explain the code :
Code snippet 1: If not, replace one in real time Seed, that rand.Intn(1000) The random number may not achieve the random effect , So here we put it Seed Over time .

Code snippet 2: hold guessNum Conversion from string to number .

Code snippet 3: Set exit mode , And say a word of surprise .

Code snippet 4: Judge whether the number you want to guess is the number you want to guess , And prompt the size .

<> First improvement

Just play and think if you can customize the scope , Do what you say , It's also easy to implement . be careful rand.Intn() The value in is int type .
package main import ( "fmt" "math/rand" "strconv" "time" ) func main() { fmt.
Println(" baby , Please enter the maximum range of your guess : ") var area int fmt.Scan(&area) rand.Seed(time.Now().
Unix()) answer := rand.Intn(area) for{ fmt.Println(" baby , Please input your guess :( input “ end ” End the game )")
var guess string fmt.Scan(&guess) guessNum, _ := strconv.Atoi(guess) if guess ==
" end "{ fmt.Println("I Love You!Good Bye!") break } switch { case guessNum >
answer: fmt.Println(" Guess big ") case guessNum < answer: fmt.Println(" Guess it's too small ") default:
fmt.Println(" That's right ") break } } }
<> Second improvement

In the process of playing the game , Found that the guess did not exit the program , So I put it switch In the sentence default In break Change to return
package main import ( "fmt" "math/rand" "strconv" "time" ) func main() { fmt.
Println(" baby , Please enter the maximum range of your guess : ") var area int fmt.Scan(&area) rand.Seed(time.Now().
Unix()) answer := rand.Intn(area) for{ fmt.Println(" baby , Please input your guess :( input “ end ” End the game )")
var guess string fmt.Scan(&guess) guessNum, _ := strconv.Atoi(guess) if guess ==
" end "{ fmt.Println("I Love You!Good Bye!") break } switch { case guessNum >
answer: fmt.Println(" Guess big ") case guessNum < answer: fmt.Println(" Guess it's too small ") default:
fmt.Println(" That's right ") return } } }
<> The third improvement

Since it's a surprise , Let's output the following sentence every time .

* I don't really have much talent , Just because I'm lucky to see you , So this ordinary heart just gave birth to a lot of romance out of thin air .
* The order of life is very important , I'm a little late , I'm not with you all the time , But wait a little longer , I'd like to spend the rest of my life with you .
* Here's the first bite of strawberry cake , Warm sunshine in winter , Fresh air after rain for you , I'll give you my first look in the morning . Give you a hug , Tenderness for you , Happy to you , All the good things are for you .
* You are the cutest , I don't have time to think when I say it . And after thinking , Still say so .
* You don't have everything I like , It's just that I like all of you .
* I can't say I'll be better with you by my side , But at least there will be more courage to face the world , Even walking will have a little confidence , It's the power of my love .
* from now on All kinds of hard work will be like a long rain But as long as I'm with you I can bear it
* Looking back on the past years and months, my heart beats You're still at the top ​​​
* My dream. , It's just hope that in your wishes , Me, too
* We all like light Though fleeting But you are still you There is a name that makes my heart tremble when I shout
* I really want to spend a snowy day with you We walked together in the snow He said that he would grow old together all the way Make a wish under the Christmas tree And I You can also put your red hands in your arms
I'll tell you in a low voice how much I like you How do you like your winter
* I can't give you the whole world , however , My whole world is for you .
* This is my love , Take it , This is my heart , Don't break it , This is my hand , Hold it .
* I only hold your hand in this life , Because you are enough in this life .
* I like you! , So I hope you're surrounded , So the road you take should be full of flowers , We need a lot of voices .
* It snows in spring , It rains heavily in summer , It's windy in autumn , There is a bright sun in winter , There will be many accidents throughout the year , But the most fascinating , Or met you . ​​
*
I like the cold half watermelon in summer , I like the sweet strawberry popsicles in summer , I like the singing of cicada in summer , I like the gentle River breeze in summer , I like the stars in the night sky in summer , But actually , What I like most is to spend the summer with me !
*
I met a lot of people . Someone gave me a fever , I thought it was love , As a result, it burned out everything . Someone makes me cold , From then on disappeared in life . Someone makes me feel warm , But it's just warmth . only you , Let my temperature rise 0.2℃.
package main import ( "fmt" "math/rand" "strconv" "time" ) func
StringWithCharset(length int) string { strlist1 := []string{
" I don't really have much talent , Just because I'm lucky to see you , So this ordinary heart just gave birth to a lot of romance out of thin air .",
" The order of life is very important , I'm a little late , I'm not with you all the time , But wait a little longer , I'd like to spend the rest of my life with you .",
" Here's the first bite of strawberry cake , Warm sunshine in winter , Fresh air after rain for you , I'll give you my first look in the morning . Give you a hug , Tenderness for you , Happy to you , All the good things are for you .",
" You are the cutest , I don't have time to think when I say it . And after thinking , Still say so .", " You don't have everything I like , It's just that I like all of you .",
" I can't say I'll be better with you by my side , But at least there will be more courage to face the world , Even walking will have a little confidence , It's the power of my love .", " from now on All kinds of hard work will be like a long rain
But as long as I'm with you I can bear it ", " Looking back on the past years and months, my heart beats You're still at the top ", " My dream. , It's just hope that in your wishes , Me, too ", " We all like light Though fleeting
But you are still you There is a name that makes my heart tremble when I shout ", " I really want to spend a snowy day with you We walked together in the snow He said that he would grow old together all the way Make a wish under the Christmas tree And I
You can also put your red hands in your arms I'll tell you in a low voice how much I like you How do you like your winter ", " I can't give you the whole world , however , My whole world is for you .",
" This is my love , Take it , This is my heart , Don't break it , This is my hand , Hold it .", " I only hold your hand in this life , Because you are enough in this life .",
" I like you! , So I hope you're surrounded , So the road you take should be full of flowers , We need a lot of voices .",
" It snows in spring , It rains heavily in summer , It's windy in autumn , There is a bright sun in winter , There will be many accidents throughout the year , But the most fascinating , Or met you .",
" I like the cold half watermelon in summer , I like the sweet strawberry popsicles in summer , I like the singing of cicada in summer , I like the gentle River breeze in summer , I like the stars in the night sky in summer , But actually , What I like most is to spend the summer with me !"
,
" I met a lot of people . Someone gave me a fever , I thought it was love , As a result, it burned out everything . Someone makes me cold , From then on disappeared in life . Someone makes me feel warm , But it's just warmth . only you , Let my temperature rise 0.2℃."
, } rand.Seed(time.Now().Unix()) b := strlist1[rand.Intn(len(strlist1))] return
string(b) } func main() { fmt.Println(" baby , Please enter the maximum range of your guess : ") var area int fmt.Scan(
&area) rand.Seed(time.Now().Unix()) answer := rand.Intn(area) for{ fmt.Println(
" baby , Please input your guess :( input “ end ” End the game )") var guess string fmt.Scan(&guess) guessNum, _ :=
strconv.Atoi(guess) if guess == " end "{ fmt.Println("I Love You!Good Bye!") break
} switch { case guessNum > answer: fmt.Println(" Guess big ") case guessNum < answer:
fmt.Println(" Guess it's too small ") default: fmt.Println(" That's right ") fmt.Println( StringWithCharset(1)
) return } } }
<> epilogue

Finally, I finished this blog , Happy Valentine's day in advance . The blogger is a sophomore , I hope you can support me a lot . I hope I can find a girlfriend in the new year , I hope my single little sister can send me a private message ~ I wish you guys with girlfriends and girlfriends a long time , If you don't have a girlfriend, don't rob me .

Technology