我们的项目是使用 Go 制作一款实用的剪刀石头布游戏。我认为这是一个很好的地方,可以就我可能犯的一些明显错误寻求一些指导。
我有几个问题。
无论用户输入什么,程序都会说我总是输入“rock”。
无论我输入什么,程序总是告诉我这是一个“平局”
所以对我来说很明显我的 if/else 语句有问题,但我不确定它在哪里以及到底是什么。我也知道我的PlayerPlay
功能很丑陋,但由于某种原因,当我最初在那里显示我的显示菜单时,它会继续循环回到我的菜单,而不继续执行程序的其余部分。
package main
import (
"fmt"
"math/rand"
"time"
)
func ComputerPlay() int {
return rand.Intn(2) + 1
}
func PlayerPlay(play int) int {
fmt.Scanln(&play)
return play
}
func PrintPlay(playerName string, play int) {
fmt.Printf("%s picked ", playerName)
if play == 0 {
fmt.Printf("rock\n")
} else if play == 1 {
fmt.Printf("paper\n")
} else if play == 2 {
fmt.Printf("scissors\n")
}
fmt.Printf("Computer has chose ")
switch ComputerPlay() {
case 0:
fmt.Println("rock\n")
case 1:
fmt.Println("paper\n")
case 2:
fmt.Println("scissors\n")
}
}
func ShowResult(computerPlay int, humanPlay int){
var play int
computerPlay = ComputerPlay()
humanPlay = PlayerPlay(play)
if humanPlay == 0 && humanPlay == 0 {
fmt.Printf("It's a tie\n")
} else if humanPlay == 0 && computerPlay == 1 {
fmt.Printf(" Rock loses to paper\n")
} else if humanPlay == 0 && computerPlay == 2 {
fmt.Printf("Rock beats scissors\n")
} else if humanPlay == 1 && computerPlay == 0 {
fmt.Printf(" Paper beats rock\n")
} else if humanPlay == 1 && computerPlay == 1 {
fmt.Printf("It's a tie!\n")
} else if humanPlay == 1 && computerPlay == 2 {
fmt.Printf("Paper loses to scissors\n")
} else if humanPlay == 2 && computerPlay == 0 {
fmt.Printf("Scissors loses to rock\n")
} else if humanPlay == 2 && computerPlay == 1 {
fmt.Printf(" Scissors beats paper\n")
} else if humanPlay == 2 && computerPlay == 2 {
fmt.Printf(" It's a tie!\n")
}
}
跃然一笑
相关分类