Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么函数if/else if/else if语句返回相同的答案?_Javascript_Function_If Statement - Fatal编程技术网

Javascript 为什么函数if/else if/else if语句返回相同的答案?

Javascript 为什么函数if/else if/else if语句返回相同的答案?,javascript,function,if-statement,Javascript,Function,If Statement,我有一个石头、布、剪刀的游戏,用户点击标有“石头”、“布”、“剪刀”的按钮,这会导致用户选择。 当我运行程序时,compareChoices()函数总是返回“结果是平局,让我们再玩一次!”,我不明白为什么 岩石Rock 纸张 剪刀 功能:剪刀(用户选择){ 警报(“您选择了“+userchoice+”…计算机选择了“+getComputerChoice()+”); 比较(); } 函数getComputerChoice(){ var computerChoice=Math.random()

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

岩石Rock 纸张 剪刀
您没有将任何内容传递到CompareChoice

岩石Rock 纸张 剪刀
您没有将任何内容传递到CompareChoice

岩石Rock 纸张 剪刀
您需要将参数传递给compareChoices()

你应该只提醒一次,因为它们会靠得很近。大概是这样的:

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);
}

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

你应该只提醒一次,因为它们会靠得很近。大概是这样的:

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);
}

您必须将computerchoice放入变量中,并使用相同的computerchoice调用comparechoices函数

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

您必须将computerchoice放入变量中,并使用相同的computerchoice调用comparechoices函数

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

您没有为
compareChoices
传递任何
参数看起来您没有将参数传递到compareChoices()函数这将有助于:
var computed=getComputerChoice();警报(“您选择了“+userchoice+”…计算机选择了“+computed+”);比较(使用)