-1

所以我开始学习 HTML/CSS/JavaScript 并在尝试制作一个非常简单的石头剪刀布版本时遇到了这个问题。我相信代码工作正常,但我无法检查,因为它拒绝执行。我在互联网上广泛寻找答案,但似乎找不到任何答案。我在 JavaScript 方面的经验很少,我目前正在从Codecademy学习它,但我认为该资源可能已经过时,因为其他网站似乎有语法冲突。简而言之,我做错了什么,哪个网站做对了?

<html>
<head>
    <title>R,P,S!</title>
    <script type="text/javascript">
        function whole(){
            function game(play){
                if (play="yes"){
                    var userChoice = prompt("Do you choose rock, paper or scissors?");
                    var computerChoice = Math.random();
                    if (computerChoice < 0.34) {
                        computerChoice = "rock";}
                    else if(computerChoice <= 0.67) {
                        computerChoice = "paper";}
                    else {
                        computerChoice = "scissors";
                    }
                    function compare(choice1,choice2){
                        if (choice1==choice2){
                            compare(userChoice,computerChoice);
                        }
                        if (choice1=="rock"){
                            if (choice2=="scissors"){
                                document.getElementById("result").innerHTML="Rock wins";
                            }
                            else{
                                document.getElementById("result").innerHTML="Paper wins";
                            }
                        }
                        if (choice1=="paper"){
                            if (choice2=="rock"){
                                document.getElementById("result").innerHTML="Paper wins";
                            }
                            else{
                                document.getElementById("result").innerHTML="Scissors win";
                            }
                        }
                        if (choice1=="scissors"){
                            if (choice2=="paper"){
                                document.getElementById("result").innerHTML="Scissors win";
                            }
                            else{
                                document.getElementById("result").innerHTML="Rock wins";
                            }
                        }
                    };
                    compare(userChoice,computerChoice);
                }
                else{
                    document.writeln("<p>Thanks for playing! This was made by Alex</p>";)
                }
            }
            var start = prompt ("Do you want to play?","Yes");}
    </script>
</head>
<body style="text-align:center">
    <h1>JavaScript on a webpage? This is madness!</h1>
    <h2>Madness? THIS... IS... HTML!!!!</h2>
    <button onclick="whole()">Try it out!</button>
    <p id="result">Who won?</p>

</body>
</html>

**编辑:Codecademy 的词汇表似乎与其他网站一致,他们只是还没有开始编辑他们的课程。*

**编辑:这是我最后的小代码。享受它的简单!*

<html>
<head>
    <title>R,P,S!</title>
    <script type="text/javascript">
        function whole(){
            function game(play){
                if (play=="Yes"||play=="yes"){
                    var userChoice = prompt("Do you choose rock, paper or scissors?");
                    var computerChoice = Math.random();
                    if (computerChoice < 0.34) {
                        computerChoice = "rock";}
                    else if(computerChoice <= 0.67) {
                        computerChoice = "paper";}
                    else {
                        computerChoice = "scissors";
                    }
                    function compare(choice1,choice2){
                        if (choice1==choice2){
                            alert("It was a tie!");
                            game("yes");
                        }
                        if (choice1=="rock"){
                            if (choice2=="scissors"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Rock crushes scissors.";
                                document.getElementById("loss").innerHTML="";
                            }
                            else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";
                                document.getElementById("win").innerHTML="";
                            }
                        }
                        else if (choice1=="paper"){
                            if (choice2=="rock"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Paper smothers rock.";
                                document.getElementById("loss").innerHTML="";
                            }
                            else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";
                                document.getElementById("win").innerHTML="";
                            }
                        }
                        else if (choice1=="scissors"){
                            if (choice2=="paper"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Scissors cut paper.";
                                document.getElementById("loss").innerHTML="";
                            }
                            else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Rock crushes scissors.";
                                document.getElementById("win").innerHTML="";
                            }
                        }
                        else{
                            alert("Very funny. Now do it right.");
                            game("yes");
                        }
                    };
                    compare(userChoice,computerChoice);
                }
                else{
                    document.getElementById("messages").innerHTML="Well alrighty then.";
                    document.getElementById("loss").innerHTML="";
                    document.getElementById("win").innerHTML="";
                }
            }
            var start = prompt ("Do you want to play?","Yes");
            game(start);}
    </script>
    <style>
        body{
            text-align:center;
        }
        #messages{
            font-size:20px;
            color: #00246B;
        }
        #win{
            color: #29A329;
            font-size:18px;
        }
        #loss{
            color:#CC0000;
            font-size:18px;
        }
        a{
            text-decoration:none;
            color:black;
        }
        a:hover{
            font-size:125%;
            color:#B20000;
        }
        button{
            font-size:21px;
        }
    </style>
</head>
<body>
    <a href="http://youtu.be/T8r3cWM4JII">
    <h1>JavaScript on a webpage? This is madness!</h1>
    <h2>Madness? THIS... IS... HTML!!!!</h2>
    </a>
    <button onclick="whole()">Try it out!</button>
    <p id="messages">Who won?</p>
    <p class="result"><span id="loss"></span><span id="win"></span></p>
</body>
</html>
4

2 回答 2

0

您的代码有一些问题:

if语句中的赋值运算符:

if (play="yes")

这只是将字符串分配给"yes"变量play。您应该使用比较运算符:

如果(播放 === “是”)

错位的分号:

document.writeln("<p>Thanks for playing! This was made by Alex</p>";)

应该:

document.writeln("<p>Thanks for playing! This was made by Alex</p>");
于 2013-10-06T22:32:08.930 回答
0

您的代码在语法上是错误的。在括号后面加上分号。

else {
    document.writeln("<p>Thanks for playing! This was made by Alex</p>");
}

现在在函数整体结束时调用闭包内的函数游戏。

IE

function() whole(){ 
   function game(play) {
      // your logic
   }

   var start = prompt("Do you want to play?", "Yes");

   game(start);
}

你的游戏逻辑很容易出错。重新检查你的逻辑。

您正在无限递归地调用函数比较choice1 == choice2

只需评论该行

if(choice1 == choice2) {
    // compare(userChoice, computerChoice);
}

它会起作用的

于 2013-10-06T22:21:47.017 回答