Golang的猜数字小游戏

warning: 这篇文章距离上次修改已过192天,其中的内容可能已经有所变动。

一个用Golan编写的猜数字小游戏

屏幕截图 2021-11-06 221257.png屏幕截图 2021-11-06 221257.png

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    mainGame()
}

func mainGame() {
    var yourNum, myNum int
    fmt.Println("I have a num[0-100], Pleasr enter the first num what you guess!")
    myNum = setNum()
    yourNum = getNum()

PD:
    if myNum > yourNum {
        fmt.Println("Your num is small, Please add")
        yourNum = getNum()
        goto PD
    } else if myNum < yourNum {
        fmt.Println("Your num is big, Pless reduce")
        yourNum = getNum()
        goto PD
    } else {
        fmt.Printf("You Win!nThe num is %dn", yourNum)
        playAgain()
    }
}

func setNum() int {
    return rand.Intn(100)
}

func getNum() int {
    var num int
    fmt.Scanln(&num)
    return num
}

func playAgain() {
    var anser string
    fmt.Println("Are you what to play again?(yes/no)")
    fmt.Scanln(&anser)
    if anser == "yes" {
        mainGame()
    } else if anser == "no" {
        fmt.Println("Thinks for Play")
    } else {

    }
}

none
最后修改于:2021年11月06日 22:13

添加新评论

召唤看板娘