我正在尝试创建一个“石头、剪刀、布”游戏,但 if-else-if 并不能 100% 返回应有的正确值。请帮忙! :(

问题描述 投票:0回答:1

所以:我对 Javascript 有点陌生,我正在尝试创建一个石头、剪刀、布游戏。这是我的代码:

let choices= ["Rock", "Paper", "Scissors"];

    function getComputerChoice() {
      let result= choices[(Math.floor(Math.random()* 3))];
      return result;
      }
    
    console.log(getComputerChoice()); //This first function I made was to have the Computer select one out of the three choices I declared in the global "choices" array. I then used the console log to see the output of what the computer picked. This function worked correctly. 

    function checkWinner(computerSelection, playerSelection) {
     if ((computerSelection === "Rock" && playerSelection === "Rock") ||
         (computerSelection === "Paper" && playerSelection === "Paper") ||
         (computerSelection === "Scissors" && playerSelection === "Scissors")) {
         return "Tie"; 
         } 
           else if ((computerSelection == "Rock" && playerSelection == "Scissors") ||
                    (computerSelection == "Paper" && playerSelection == "Rock") ||
                    (computerSelection == "Scissors" && playerSelection == "Paper")) {
                  return "Computer";
                } else if ((computerSelection == "Scissors" && playerSelection == "Rock") ||
                           (computerSelection == "Rock" && playerSelection == "Paper") ||
                           (computerSelection == "Paper" && playerSelection == "Scissors")) {
                  return "Player";
                } else {
                  return "Try again";
                }
            }  

          let computerSelection= getComputerChoice();
          let playerSelection= "Paper";

     console.log(checkWinner(computerSelection, playerSelection)); //This second function is the function I've been having issues with. I don't understand why, but when I view the output of the "checkWinner" function, half of the time it correctly returns "Tie" when "Paper" is entered into "playerSelection", but then other times it incorrectly returns "Player" when "Paper" is entered, even though obviously that's wrong because it SHOULD return "Tie". Then to make matters worse, half of the time it also returns further nonsensical answers as well, such as "Paper" tying with "Rock" (The Computer's choice), "Paper" tying with "Scissors" (The Computer's choice), "Paper" beating "Scissors" (The Computer's choice), etc. 

我不知道这里出了什么问题,而且我已经尝试调试这段代码好几天了!我觉得一些非常小的事情正在抛弃我的整个代码并以某种方式使计算机在某个地方感到困惑!

对于解决方案,我尝试在所有 if-else-if 语句中使用严格相等(这不起作用),在所有 if-else-if 语句中使用常规相等(==)(这不起作用,并且似乎返回更糟糕、更不准确的结果),我严格检查了我的代码是否有任何拼写错误,并且我找不到任何明显的语法错误?我希望计算机以 100% 的准确度返回正确的结果,而不是看起来只有 40%-50% 左右的准确度。请帮忙!

javascript if-statement conditional-statements nested console.log
1个回答
0
投票

if-else-if没有任何问题。

问题是,用

console.log(getComputerChoice())
检查计算机选择的内容并不是正确的方法,因为运行此命令会重新生成计算机的选择。

查看结果的正确方法是将 result 放入 console.log 中,如下所示:

console.log(result);

function getComputerChoice() {
    let result = choices[(Math.floor(Math.random()* 3))];
    console.log(result);
    return result;
}

此外,您不必在平局的情况下检查每个场景,检查

if (computerSelection === playerSelection)
就足够了。

我不明白为什么你最后需要一个else,如果你也不需要,那么它可以进一步简化,将“玩家“Win”放在

else
中。

function checkWinner(computerSelection, playerSelection) {
    console.log(computerSelection);
    console.log(playerSelection);

    if (computerSelection === playerSelection) return "Tie";    
    else if ((computerSelection == "Rock" && playerSelection == "Scissors") ||
            (computerSelection == "Paper" && playerSelection == "Rock") ||
            (computerSelection == "Scissors" && playerSelection == "Paper")) return "Computer";
    else return "Player";
}
© www.soinside.com 2019 - 2024. All rights reserved.