猜数字游戏(Go实现)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Pg_dog/article/details/79821171

后面会使用Unity3D加上UI

package main

import (
    "fmt"
    "bufio"
    "os"
    "strconv"
    "math/rand"
    "time"
)

var RANGENUM int = 20

func CreateRangeNum(num int) int {
    ret := rand.New(rand.NewSource(time.Now().UnixNano()))  //使用时间种子来获取不同的结果
    //ret := rand.Intn(num)
    return ret.Intn(num)
}

func main() {
    i := CreateRangeNum(RANGENUM)

    fmt.Println("Please enter a numble in the range:0 -",RANGENUM)
    reader := bufio.NewReader(os.Stdin)    //类比C中的标准化输入

    for {
        data,_,_ := reader.ReadLine()
        command,err := strconv.Atoi(string(data))

        if err != nil {
            fmt.Println("The format is wrong, please enter the number!!!")
        } else {
            if command == i {
                fmt.Println("Congratulations, you got it")
                break
            } else if command > i {
                fmt.Println("The number you enter is greater than the generated number. Don't be discouraged!Again")
            } else {
                fmt.Println("The number you enter is less than the generated number. Don't be discouraged!Again")
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Pg_dog/article/details/79821171
今日推荐