我正在寫一個JavaScript岩石紙剪刀應用程序,但我從來沒有寫過應用程序。誰能幫我嗎?對於Rock來說,它總是爲Paper輸出一個平局, ,它總是爲Scissors輸出一個損失 ,它總是輸出一個勝利。我的搖滾紙剪刀有什麼問題?
<script type="text/javascript">
var Rock;
var Paper;
var Scissors;
var choice = prompt("Specify your choice of 'Rock', 'Paper', or 'Scissors'.");
var game = [Rock, Paper, Scissors];
document.write("Rock.");
document.write("Paper..");
document.write("Scissors...");
var result = game[Math.floor(Math.random() * game.length)];
if (result === Rock) {
if (choice === "Rock") {
document.write("It's a draw! Try again.");
}
else if (choice === "Paper") {
document.write("You lose! Unlucky.");
}
else if (choice === "Scissors") {
document.write("You win! Nice.");
}}
else if (result === Paper) {
if (choice === "Rock") {
document.write("You win! Nice.");
}
else if (choice === "Paper") {
document.write("It's a draw! Try again.");
}
else if (choice === "Scissors") {
document.write("You lose! Unlucky.");
}}
else if (result === Scissors) {
if (choice === "Rock") {
document.write("You lose! Unlucky.");
}
else if (choice === "Paper") {
document.write("You win! Nice.");
}
else if (choice === "Scissors") {
document.write("It's a draw! Try again.");
}}
</script>
非常感謝,非常有幫助 – loucidity 2012-02-25 19:22:49
只要我們希望用戶在提示中輸入整個字符串,最好使用「字符串數組」方式,而不是數字。 – raina77ow 2012-02-25 20:13:20
好,不錯。如果我必須寫出上述邏輯,我會扭轉這種說法:如果我們處理數字,邏輯將會簡單得多。 – 2012-02-25 20:57:41