基本的JavaScript Rock-Paper-Scissors游戏无法正常工作

时间:2013-07-15 02:48:02

标签: javascript

我需要一些基础JavaScript版本的Rock-Paper-Scissors我正在努力。我刚刚开始学习,并希望尽可能保持代码尽可能完整。

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

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

2 个答案:

答案 0 :(得分: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");

演示:Fiddle

答案 1 :(得分: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