我有這樣的代碼,現在我的剪刀石頭布遊戲JS。在我的比較功能中,我試圖對其進行編程,以便在未輸入「搖滾」,「紙張」或「剪刀」時顯示警告消息。它雖然不起作用,但當我輸入的字符串不同於3個選項時,我沒有迴應。剪刀石頭布的JavaScript函數
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);
任何原因爲什麼?
大聲笑...你能否告訴答案顯而易見...我正在看更多的答案幻燈片... HEHE –
現在試試Rock-paper-scissors-lizard-Spock – Willem
你可以清理代碼如果你使用這個:'var computerChoice = Math.floor((Math.random()* 3));'然後computerChoice是0,1或2,你可以使用switch語句。 –