golang石头剪刀布程序
时间: 2023-11-02 20:09:19 浏览: 44
以下是一个简单的石头剪刀布游戏的 Golang 代码示例:
```go
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
// 初始化随机数生成器
rand.Seed(time.Now().Unix())
// 定义游戏选项
options := []string{"石头", "剪刀", "布"}
// 询问用户选择
var userChoice string
fmt.Print("请出拳(石头/剪刀/布):")
fmt.Scanln(&userChoice)
// 随机生成电脑选择
computerChoice := options[rand.Intn(len(options))]
// 输出结果
fmt.Println("你出了", userChoice)
fmt.Println("电脑出了", computerChoice)
switch userChoice {
case "石头":
switch computerChoice {
case "石头":
fmt.Println("平局")
case "剪刀":
fmt.Println("你赢了")
case "布":
fmt.Println("你输了")
}
case "剪刀":
switch computerChoice {
case "石头":
fmt.Println("你输了")
case "剪刀":
fmt.Println("平局")
case "布":
fmt.Println("你赢了")
}
case "布":
switch computerChoice {
case "石头":
fmt.Println("你赢了")
case "剪刀":
fmt.Println("你输了")
case "布":
fmt.Println("平局")
}
default:
fmt.Println("无效的选择")
}
}
```
运行代码,你可以和电脑进行石头剪刀布游戏。