【问题标题】:My switch is not recognizing the variable I defined before it我的开关无法识别我之前定义的变量
【发布时间】:2015-11-15 07:29:53
【问题描述】:

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

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

这是我的代码,带有 cmets:

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。是平手!为我工作
  • 我用调试器运行你的代码,我总是输入“摇滚”的情况——这一切似乎都对我有用。
  • 旁注:您从split 获得的数组中的数组条目是字符串,因此无需对它们调用.toString()
  • 我建议您查看分号所在位置的规则。你已经在上面省略了大部分(这意味着你依赖于 JS 的“自动分号插入”纠错机制),但是有几个分散在错误的地方(你不想在例如,附加到ifelse 的块)。还建议一次声明你的变量,而不是在10个不同的地方(在rpsVerdict的情况下)。在这里 JS 再次是宽松的,忽略重复的声明并将它们托管在函数顶部的控制结构之外,但是......
  • 请注意,这是在 node.js 中运行的。

标签: javascript node.js


【解决方案1】:

问题在于您的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>

【讨论】:

  • @whatmatterson:不用担心。如果这回答了您的问题,SO 的工作方式,您将accept 回答。但前提是它确实回答了您的问题。
猜你喜欢
  • 1970-01-01
  • 2023-01-20
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 2021-11-17
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多