首页 > 解决方案 > 只在石头剪刀布游戏中获得“平局”结果

问题描述

我目前正在用 Javascript 编写 Rock、Paper、Scissors 游戏,出于某种原因,无论玩家输入什么,我总是得到“平局”结果。在过去的一个小时里,我一直在试图弄清楚,但没有骰子。非常感谢任何帮助。我把我的代码放在下面。

         let computerChoice = Math.random();
        if (computerChoice < 0.34) {
            computerChoice = "rock";
        } else if(computerChoice <= 0.67) {
            computerChoice = "paper";
        } else {
            computerChoice = "scissors";
        }

        let playerPrompt = prompt("Rock, paper, or scissors?")
        let playerChoice = String(playerPrompt).toLowerCase

         function playRound(playerChoice, computerChoice) {
            if (playerChoice === computerChoice) {
                return "Draw!"
            } else if (playerChoice === "rock" && computerChoice === "scissors") {
                return "Player wins!"
            } else if (playerChoice === "paper" && computerChoice === "rock") {
                return "Player wins!"
            } else if (playerChoice === "scissors" && computerChoice === "paper") {
                return "Player wins!"
            } else {
                return "Computer wins!"
            }
        }

        let results = playRound()
        console.log(results)```

标签: javascript

解决方案


如果我没记错的话,你没有将任何参数传递给playRound(),它可能应该是:

let results = playRound(playerChoice, computerChoice)

编辑:正如 Quentin(和 Alon Eitan)所提到的,这不是唯一的问题:

let playerChoice = String(playerPrompt).toLowerCase

实际上将函数分配String.toLowerCaseplayerChoice,如果你想要语法的小写值playerPrompt应该是

let playerChoice = playerPrompt.toLowerCase()

或直接

let playerChoice = prompt("Rock, paper, or scissors?").toLowerCase()

推荐阅读