0

我需要一些关于我正在研究的 Rock-Paper-Scissors 的基本 JavaScript 版本的帮助。我今天刚开始学习,如果可能的话,我希望尽可能保持代码完整。

我遇到的主要问题是循环。尽管我将“再次”设置为“否”(通过提示),但它仍在继续。此外,计算机似乎总是选择摇滚......我有一种感觉,我只是错过了一些简单的东西:

<html>
<script type="text/javascript">
    var myChoice = "";
    var compChoice = "";
    var again;
    while (again = "yes")
        {
        myChoice = prompt("Do you choose rock, paper or scissors?");
        compChoice = Math.random();
            if (compChoice < 0.33)
                {
                compChoice = "rock";
                }
            else if(compChoice < 0.67)
                {
                compChoice = "paper";
                }
            else
                {
                compChoice = "scissors";
                }
        if (compChoice = myChoice)
            {
            alert("It's a tie!");
            again = prompt("Would you like to play again?(yes/no)");
            }
        else if (compChoice = "rock")
            {
            if(myChoice = "scissors")
                {
                alert("You lose!");
                again = prompt("Would you like to play again?(yes/no)");
                }
            else if (myChoice = "paper")
                {
                alert("You win!");
                again = prompt("Would you like to play again?(yes/no)");
                }
            }
        else if (compChoice = "paper")
            {
            if (myChoice = "rock")
                {
                alert("You lose!");
                again = prompt("Would you like to play again?(yes/no)");
                }
            else if (myChoice = "scissors")
                {
                alert("You win!");
                again = prompt("Would you like to play again?(yes/no)");
                }
            }
        else if (compChoice = "scissors")
            {
            if (myChoice = "rock")
                {
                alert("You win!");
                again = prompt("Would you like to play again?(yes/no)");
                }
            else if (myChoice = "paper")
                {
                alert("You lose!");
                again = prompt("Would you like to play again?(yes/no)");
                }
            }
        };
</script>
</html>
4

2 回答 2

2

您在 while 语句中使用=而不是==在所有 if 语句中也使用相同

=是赋值运算符,==而是比较运算符

var myChoice = "";
var compChoice = "";
var again;
do 
{
    myChoice = prompt("Do you choose rock, paper or scissors?");
    compChoice = Math.random();
    if (compChoice < 0.33)
    {
        compChoice == "rock";
    }
    else if(compChoice < 0.67)
    {
        compChoice = "paper";
    }
    else
    {
        compChoice == "scissors";
    }
    if (compChoice == myChoice)
    {
        alert("It's a tie!");
    }
    else if (compChoice == "rock")
    {
        if(myChoice == "scissors")
        {
            alert("You lose!");
        }
        else if (myChoice == "paper")
        {
            alert("You win!");
        }
    }
    else if (compChoice == "paper")
    {
        if (myChoice == "rock")
        {
            alert("You lose!");
        }
        else if (myChoice == "scissors")
        {
            alert("You win!");
        }
    }
    else if (compChoice == "scissors")
    {
        if (myChoice == "rock")
        {
            alert("You win!");
        }
        else if (myChoice == "paper")
        {
            alert("You lose!");
        }
    }
    again = prompt("Would you like to play again?(yes/no)");

}while (again == "yes");

演示:小提琴

于 2013-07-15T02:50:12.970 回答
1

一个主要问题是您正在分配值而不是在多个位置比较值。

a = 2 // assigns a to value 2, and evaluates to true
a == 2 // compares a to 2, only evaluates to true if a has value 2
于 2013-07-15T02:50:23.790 回答