【问题标题】:Only getting "draw" result in Rock, Paper, Scissors game只在石头剪刀布游戏中获得“平局”结果
【发布时间】:2019-08-06 15:33:53
【问题描述】:

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

         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)```

【问题讨论】:

  • 你没有把参数传给函数!
  • 你需要做一些基本的调试。使用console.log 找出playerChoicecomputerChoice 实际上是什么。您在调用它时忘记将它们传递给playRound,并且您将playerChoice 设置为toLowerCase 函数,而不是调用该函数并获取返回值。您的问题基本上是拼写错误的集合。

标签: javascript


【解决方案1】:

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

let results = playRound(playerChoice, computerChoice)

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

let playerChoice = String(playerPrompt).toLowerCase

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

let playerChoice = playerPrompt.toLowerCase()

或直接

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

【讨论】:

  • 它将尝试将字符串与当前代码的函数进行比较,因此这不是唯一的问题 - 请参阅 Quentin 的评论
  • 我在发布后立即阅读,我的回答确实很仓促:) 我还没有找到如何删除它,但是
  • 你可以edit你的答案,我认为你不需要删除它
  • 这解决了我的问题,谢谢!我认为这将是非常明显的事情。
【解决方案2】:

错误是 toLowerCase 而不是 toLowerCase(),你错过了括号。尝试运行这个 sn-p,它可以工作

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(playerChoice, computerChoice)
        console.log(results)

【讨论】:

  • 尝试解释你为解决问题所做的工作,你的修改淹没在未更改的代码中。
  • toLowerCase() 而不是 toLowerCase
猜你喜欢
  • 1970-01-01
  • 2016-01-16
  • 2022-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多