我正在编写一个javascript石头剪刀布应用程序,但是我以前从未编写过该应用程序。谁能帮我吗?对于Rock,它始终会输出平局,
对于Paper,它总是输出损失,
对于剪刀来说,它总是输出胜利。

<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>

最佳答案

RockPaperScissors均为undefined,因此它们将始终相等。设置它们为任何东西,它应该可以工作。

var Rock = 'Rock';
var Paper = 'Paper';
var Scissors = 'Scissors';

关于javascript - 我的剪刀石头布怎么了?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9446739/

10-09 18:06