为啥我的函数 if/else if/else if 语句返回相同的答案?

Posted

技术标签:

【中文标题】为啥我的函数 if/else if/else if 语句返回相同的答案?【英文标题】:why is my function if/else if/ else if statement returning the same answer?为什么我的函数 if/else if/else if 语句返回相同的答案? 【发布时间】:2016-02-16 10:00:54 【问题描述】:

我设置了一个石头、纸、剪刀游戏,用户点击标记为“石头”、“纸”、“剪刀”的按钮,这会导致用户选择。 当我运行程序时,compareChoices() 函数总是返回“结果是平局,让我们再玩一次!”,我不明白为什么。

  < article >
    < button onclick = "rockPaperScissors('Rock')" > Rock < /button>
    <button onclick="rockPaperScissors('Paper')">Paper</button >
    < button onclick = "rockPaperScissors('Scissors')" > Scissors < /button>
  </article >

    < script type = "text/javascript" >
    function rockPaperScissors(userchoice) 
      alert("You chose " + userchoice + " ...the computer chose " + getComputerChoice() + ".");
      compareChoices();
    

  function getComputerChoice() 
    var computerChoice = Math.random();
    if (computerChoice < 0.34) 
      computerChoice = "Rock";
     else if (computerChoice < 0.67) 
      computerChoice = "Paper";
     else 
      computerChoice = "Scissors";
    
    return computerChoice;
  

  function compareChoices(userChoice, ComputerChoice) 
    if (userChoice === ComputerChoice) 
      alert("The result is a tie, let's play again!");
     else if (userChoice === "Rock") 
      if (ComputerChoice === "Scissors") 
        alert("Congratulations, you win!");
       else 
        alert("The computer wins! Care to play again?");
      
     else if (userChoice === "Scissors") 
      if (ComputerChoice === "Rock") 
        alert("The computer wins, let's play again!");
       else 
        alert("Yippie! You win!");
      
     else if (userChoice === "Paper") 
      if (ComputerChoice === "Rock") 
        alert("The computer wins. Don't give up, try again!");
       else 
        alert("Hail the all mighty visitor. Give it another go!");
      
    
   < /script>

【问题讨论】:

你没有为compareChoices传递任何arguments 看起来您没有将参数传递给 compareChoices() 函数 这会有所帮助:var computed = getComputerChoice(); alert("You chose " + userchoice + " ...the computer chose " + computed + "."); compareChoices(userchoice, computed); 如果我可以提醒你,不要忘记在 JS 中所有参数都是可选的,所以在调用函数时要小心。 【参考方案1】:

您没有将任何内容传递给 compareChoices

  < article >
    < button onclick = "rockPaperScissors('Rock')" > Rock < /button>
    <button onclick="rockPaperScissors('Paper')">Paper</button >
    < button onclick = "rockPaperScissors('Scissors')" > Scissors < /button>
  </article >

    < script type = "text/javascript" >
    function rockPaperScissors(userchoice) 
      var computerChoice = getComputerChoice();
      alert("You chose " + userchoice + " ...the computer chose " + computerChoice + ".");
      compareChoices(userChoice, computerChoice);
    

  function getComputerChoice() 
    var computerChoice = Math.random();
    if (computerChoice < 0.34) 
      computerChoice = "Rock";
     else if (computerChoice < 0.67) 
      computerChoice = "Paper";
     else 
      computerChoice = "Scissors";
    
    return computerChoice;
  

  function compareChoices(userChoice, ComputerChoice) 
    if (userChoice === ComputerChoice) 
      alert("The result is a tie, let's play again!");
     else if (userChoice === "Rock") 
      if (ComputerChoice === "Scissors") 
        alert("Congratulations, you win!");
       else 
        alert("The computer wins! Care to play again?");
      
     else if (userChoice === "Scissors") 
      if (ComputerChoice === "Rock") 
        alert("The computer wins, let's play again!");
       else 
        alert("Yippie! You win!");
      
     else if (userChoice === "Paper") 
      if (ComputerChoice === "Rock") 
        alert("The computer wins. Don't give up, try again!");
       else 
        alert("Hail the all mighty visitor. Give it another go!");
      
    
   < /script>

【讨论】:

【参考方案2】:

你必须把computerchoice放入变量中,然后用相同的computerchoice调用comparechoices函数。

function rockPaperScissors(userchoice) 
       var computerchoice = getComputerChoice();
       alert("You chose " + userchoice + " ...the computer chose " + computerchoice + ".");
       compareChoices(userchoice, computerchoice);
    

【讨论】:

【参考方案3】:

您需要将参数传递给 compareChoices()。

function rockPaperScissors(userchoice) 
      alert("You chose " + userchoice + " ...the computer chose " + getComputerChoice() + ".");
      compareChoices(userchoice, getComputerChoice());
    

您应该只提醒一次,因为它们会靠得很近。像这样的:

function compareChoices(userChoice, ComputerChoice) 
    var message;
    if (userChoice === ComputerChoice) 
      message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - The result is a tie, let's play again!";
     else if (userChoice === "Rock") 
      if (ComputerChoice === "Scissors") 
        message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - Congratulations, you win!";
       else 
        message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - The computer wins! Care to play again?");
      
     else if (userChoice === "Scissors") 
      if (ComputerChoice === "Rock") 
        message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - The computer wins, let's play again!";
       else 
        message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - Yippie! You win!";
      
     else if (userChoice === "Paper") 
      if (ComputerChoice === "Rock") 
        message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - The computer wins. Don't give up, try again!";
       else 
        message = "Computer: " + ComputerChoice + ", User: " + userChoice + " - Hail the all mighty visitor. Give it another go!";
      
    
  alert(message);

【讨论】:

getComputerChoice() 每次调用都会(可能)返回不同的值。 是的,我明白了。您仍然必须将参数传递给函数。每次有人单击按钮时,您都需要比较选择,因此显然 getComputerChoice() 函数每次调用时都会返回不同的值。这不是重点吗? 检查下面给出的答案......这就是要走的路! 如果每次单击按钮时都调用该函数,则该变量每次都会更改。他们正在做同样的事情。 你错过了var computerChoice = getComputerChoice(); 吗?

以上是关于为啥我的函数 if/else if/else if 语句返回相同的答案?的主要内容,如果未能解决你的问题,请参考以下文章

为啥使用预处理器#if 语句而不是 if() else?

verilog中if else中能套if else吗,有啥错误啊,为啥??? case中能套if else吗 case中能套case吗

verilog中if else中能套if else吗,有啥错误啊,为啥??? case中能套if else吗 case中能套case吗

Python:为啥 if-else 一行语句在 else 中不能与 continue 一起使用?

为啥我不能使用正则表达式来评估 if...else 语句 [重复]

为啥很多程序员不用switch,而是大量的if……else if?