我的开关没有识别我之前定义的变量

时间:2015-11-15 07:29:54

标签: javascript node.js

我正在为一个聊天机器人编写一个石头剪刀游戏。我遇到的问题是switch (playerChoice)无法识别playerChoice变量中定义的字符串。

当我运行开关时,默认情况总是输出。它似乎甚至没有在switch()

中获取变量

这是我的代码,注释:

var user = "user"
var message = "/rps rock" //this can be paper or scissors, based on the user's input.
//Here we find out what the player chose from the command '/rps [choice]'
var playerSaid = message.split(" ")
var playerChoice = playerSaid[1].toString()

//This determines the bot's choice of 1-3
var botChoice = (Math.floor(Math.random() * 3) + 1)

//This switch *should* find the user's choice
//The if statements inside each case should get the bot's choice and tell the verdict appropriately
switch (playerChoice) {
  case "rock":
    if (botChoice = 1) {
      var rpsVerdict = "It\'s a tie!"
    } else if (botChoice = 2) {
      var rpsVerdict = "Botticus wins!"
    } else if (botChoice = 3) {
      var rpsVerdict = user + " wins!"
    };
    break;
  case "paper":
    if (botChoice = 1) {
      var rpsVerdict = "It\'s a tie!"
    } else if (botChoice = 2) {
      var rpsVerdict = "Botticus wins!"
    } else if (botChoice = 3) {
      var rpsVerdict = user + " wins!"
    };
    break;
  case "scissors":
    if (botChoice = 1) {
      var rpsVerdict = "It\'s a tie!"
    } else if (botChoice = 2) {
      var rpsVerdict = "Botticus wins!"
    } else if (botChoice = 3) {
      var rpsVerdict = user + " wins!"
    };
    break;
  default:
    var rpsVerdict = "default"
    break
  }

//Here we output a simple sentence telling who won.
console.log('You chose ' + playerChoice + '. I chose ' + botChoice + '. ' + rpsVerdict)

(我意识到botChoice输出一个数字,而不是一个字符串)

1 个答案:

答案 0 :(得分:2)

问题在于if陈述中的比较:

if (botChoice = 1) {
// -----------^

=分配运算符。要进行比较,您需要=====(在这种情况下无关紧要)。

通过作业,这就是你真正在做的事情:

botChoice = 1
if (1) {

...因为使用赋值运算符,它会将值分配给目标(botChoice),并且操作的结果是已分配的值。因此,您始终输入第一个if(因为1是真实的),无论botChoice实际上来自Math.random

您不需要switch,但您可以更简单地确定获胜者,并使用查找重复次数更少。

// Set up data. `choices` is for using Math.random to get
// a choice for the bot. `winLookup` is for looking up the
// player's choice and seeing what it beats.
var choices = ["rock", "paper", "scissors"];
var winLookup = {
    rock: "scissors",
    paper: "rock",
    scissors: "paper"
};

// Bot chooses
var botChoice = choices[Math.floor(Math.random() * 3)];

// Outcome
var rpsVerdict;
if (playerChoice == botChoice) {
    rpsVerdict = "It's a tie!";
} else if (winLookup[playerChoice] == botChoice) {
    rpsVerdict = user + " wins!";
} else {
    rpsVerdict = "Botticus wins!";
}

实例:

document.getElementById("play").onclick = function() {
  var choices = ["rock", "paper", "scissors"];
  var winLookup = {
    rock: "scissors",
    paper: "rock",
    scissors: "paper"
  };
  var user = "user";
  var message = "/rps rock";
  var playerSaid = message.split(" ");
  var playerChoice = document.getElementById("playerChoice").value;
  //var playerChoice = playerSaid[1];
  //// No .toString() --------------^

  // botChoice => rock, paper, or scissors
  var botChoice = choices[Math.floor(Math.random() * 3)];

  // outcome
  var rpsVerdict;
  if (playerChoice == botChoice) {
    rpsVerdict = "It's a tie!";
  } else if (winLookup[playerChoice] == botChoice) {
    rpsVerdict = user + " wins!";
  } else {
    rpsVerdict = "Botticus wins!";
  }

  snippet.log(
    "You chose " + playerChoice + ". I chose " + botChoice +
    ". " + rpsVerdict
  );
};
<label>
  Your choice:
  <select size=3 id="playerChoice">
    <option>rock</option>
    <option>paper</option>
    <option>scissors</option>
  </select>
</label>
<input type="button" id="play" value="Play">
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>