【发布时间】:2013-12-19 22:27:58
【问题描述】:
我现在有我的石头剪刀布 JS 游戏的代码。在我的比较功能中,我尝试对其进行编程,以便在未输入“Rock”、“Paper”或“Scissors”时显示警告消息。但它不起作用,当我输入与 3 个有效选项不同的字符串时,我没有得到任何响应。
var userChoice = prompt("Rock, Paper, or Scissors");
var computerChoice = Math.random();
var compchoice = function ()
{
if (computerChoice <= 0.34)
{
return computerChoice = "Rock";
}
else if(computerChoice <= 0.67 && computerChoice >= 0.35)
{
return computerChoice = "Paper";
}
if (computerChoice >= 0.68)
{
return computerChoice = "Scissors";
}
};
var compare = function (choice1, choice2)
{
if (computerChoice === "Rock" || "Paper" || "Scissors")
{
if (choice1 === choice2)
{
return alert("The result is a tie!");
}
else if (choice1 === "Rock")
{
if (choice2 === "Scissors")
{
return alert("Rock crushes Scissors!");
}
else if (choice2 === "Paper")
{
return alert("Paper covers Rock!");
}
}
if (choice1 === "Scissors")
{
if (choice2 === "Rock")
{
return alert("Rock crushes Scissors!");
}
else if (choice2 === "Paper")
{
return alert("Scissors cuts Paper!");
}
}
else if (choice1 === "Paper")
{
if (choice2 === "Rock")
{
return alert("Paper covers Rock!");
}
else if (choice2 === "Scissors")
{
return alert("Scissors cuts Paper!");
}
}
}
else
{
return alert("Please type Rock, Paper, or Scissors next time");
}
};
compchoice();
compare(userChoice, computerChoice);
有什么原因吗?
【问题讨论】:
-
大声笑...你能说答案很明显吗...我正在观看更多答案...呵呵
-
现在试试 Rock-paper-scissors-lizard-Spock
-
如果你使用这个,你可以稍微清理一下代码:
var computerChoice = Math.floor((Math.random() * 3));然后computerChoice是0、1或2,你可以使用switch语句。
标签: javascript