我有一个简单的石头剪刀布游戏的代码,但是当我运行它时,它只要求我输入石头、剪刀、布,然后就没有其他内容了。之后就没有任何警报了。
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
var compare = function (choice1, choice2)
{
if (choice1 === choice2)
{
return alert("The result is a tie!");
}
if (choice1 === "Rock")
{
if (choice2 === "Scissors")
{
return alert("Rock wins!");
}
else if (choice2 === "Paper")
{
return alert("Paper wins!");
}
}
else if (choice1 === "Scissors")
{
if (choice2 === "Rock")
{
return alert("Rock wins!");
}
else if (choice2 === "Paper")
{
return alert("Schissors wins!");
}
}
};
compare(userChoice, computerChoice);
顺便说一句,我正在使用在线编辑器。
最佳答案
你的问题是你混合了大小写。您将 computerChoice
设置为 "rock"
、"paper"
或 "scissors"
,但随后在您正在与“Rock”
、“Paper”
或“Scissors”
进行比较的函数。
仅使用小写字母,并在 prompt()
调用后添加 userChoice = userChoice.toLowerCase()
。
关于javascript - Javascript 石头剪刀布游戏函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20671454/