package main import ( "fmt" "math/rand" "time" ) func main() { randNum := creatRandNum() randSlice := make([]int, 4) inputSlice := make([]int, 4) famtterNum(randSlice, randNum) fmt.Println(randNum) //打印随机数 var inputNum int for true { fmt.Println("请输入一个四位数:") fmt.Scan(&inputNum) if inputNum > 999 && inputNum < 10000 { famtterNum(inputSlice, inputNum) for i := 0; i < 4; i++ { if randSlice[i] > inputSlice[i] { fmt.Printf("第%d数字小了\n", i+1) } else if randSlice[i] < inputSlice[i] { fmt.Printf("第%d数字大了\n", i+1) } else { fmt.Printf("第%d数字猜对了\n", i+1) } } } } } //生成随机数 func creatRandNum() int { rand.Seed(time.Now().Unix()) var num int for true { num = rand.Intn(10000) if num > 999 && num < 10000 { break } } return num } //将随机数存到切片中 func famtterNum(numSlice []int, num int) { numSlice[0] = num / 1000 numSlice[1] = num % 1000 / 100 numSlice[2] = num % 100 / 10 numSlice[3] = num % 10 }
来源:https://www.cnblogs.com/mask-fan/p/9912473.html