package main
import (
"fmt"
"math/rand" "time")
func GetNum(randSlice []int, num int) {
randSlice[0] = num / 1000
randSlice[1] = num % 1000 / 100
randSlice[2] = num % 100 / 10
randSlice[3] = num % 10
}
func createNum(p *int) {
rand.Seed(time.Now().UnixNano())
var num int
for {
num = rand.Intn(10000)
if num >= 1000 {
break
}
}
*p = num
}
func onGame(randSlice []int) {
var num int
keySlice := make([]int, 4)
for {
for {
fmt.Println("請輸入一個四位數字")
fmt.Scan(&num)
if 999 < num && num < 10000 {
break
}
fmt.Println("你輸入的數字不符合要求")
}
GetNum(keySlice, num)
n := 0
for i := 0; i < 4; i++ {
if keySlice[i] > randSlice[i] {
fmt.Printf("第%d位大了一點\n", i+1)
} else if keySlice[i] > randSlice[i] {
fmt.Printf("第%d位小了一點\n", i+1)
} else {
fmt.Printf("第%d位猜對了\n", i+1)
n++
}
}
if n == 4 {
fmt.Println("全部猜對了~~~")
}
}
}
func main() {
var randNum int
createNum(&randNum)
randSlice := make([]int, 4)
GetNum(randSlice, randNum)
onGame(randSlice)
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結