Javascript 我的开关无法识别之前定义的变量
我正在为聊天机器人编写一个石头剪刀游戏。我遇到的问题是Javascript 我的开关无法识别之前定义的变量,javascript,node.js,Javascript,Node.js,我正在为聊天机器人编写一个石头剪刀游戏。我遇到的问题是开关(playerChoice)无法识别playerChoice变量中定义的字符串 当我运行开关时,默认情况总是输出。看起来它甚至没有拾取开关()中的变量 以下是我的代码和注释: 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 p
开关(playerChoice)
无法识别playerChoice
变量中定义的字符串
当我运行开关时,默认情况总是输出。看起来它甚至没有拾取开关()中的变量
以下是我的代码和注释:
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输出的是一个数字,而不是一个字符串)问题在于您的
if
语句中的比较:
if (botChoice = 1) {
// -----------^
=
是赋值运算符。要进行比较,您需要==
或===
(在这种情况下,哪一个都不重要)
对于作业,以下是您真正要做的:
botChoice = 1
if (1) {
…因为使用赋值运算符,它将值赋值给目标(botChoice
),操作的结果就是所赋值的值。因此,如果(因为1是真实的),则始终输入第一个,无论botChoice
实际上来自Math.random
您不需要使用开关
,但是,您可以使用查找更简单、重复次数更少地确定获胜者
// 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选项=[“石头”、“布”、“剪刀”];
var winLookup={
石头:“剪刀”,
报纸:“摇滚”,
剪刀:“布”
};
var user=“user”;
var message=“/rps rock”;
var playerSaid=message.split(“”);
var playerChoice=document.getElementById(“playerChoice”).value;
//var playerChoice=playerSaid[1];
////否.toString()--------------^
//botChoice=>石头、布或剪刀
var botChoice=choices[Math.floor(Math.random()*3)];
//结果
var-rpsVerdict;
如果(playerChoice==botChoice){
rpsVerdict=“这是一个平局!”;
}else if(winLookup[playerChoice]==botChoice){
rpsVerdict=user+“wins!”;
}否则{
rpsVerdict=“波提库斯赢了!”;
}
snippet.log(
你选择了“+玩家选择+”。我选择了“+玩家选择”+
“+rpsVerdict
);
};代码>
您的选择:
岩石
纸张
剪刀
我在日志中看到以下内容:您选择了摇滚乐。我选择了1。这是一条领带!适用于meI使用调试器运行您的代码,并且我总是在“rock”的情况下输入代码-这一切似乎都适用于我。旁注:从split
中获得的数组项是字符串,因此无需对它们调用.toString()
。我建议您查看分号的位置规则。您已经省略了上面的大部分内容(这意味着您依赖于JS的“自动分号插入”错误更正机制),但是有一些分散在错误的位置(例如,您不希望在附加到if
或else
的块后有分号)。还建议只声明一次变量,而不是在10个不同的位置(在rpsVerdict
的情况下)。同样,JS在这里也很宽容,忽略重复的声明,并将它们托管在函数顶部的失控结构中,但是……可能需要注意的是,这是在node.JS中运行的。@whatmatterson:不用担心。如果这回答了你的问题,那么你会找到答案。但前提是它确实回答了你的问题。