我有一個簡單的岩石剪刀剪刀遊戲的代碼,但是當我運行它時,它只是要求我輸入Rock,Paper或Scissors,然後再沒有其他任何東西。之後沒有任何警報。Javascript中的岩石紙剪刀遊戲功能
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);
順便說一句,我使用的是在線編輯器。
您可能會遇到混合情況下的問題。 「剪刀」!==「剪刀」。 – showdev
var userChoice = prompt(「你選擇石頭,紙還是剪刀?」); var computerChoice = Math.random(); if(computerChoice <0.5){ alert(「Computer Wins!」) }':) –
Duh!謝謝showdev! –