简体   繁体   English

我不知道为什么我的石头,剪刀布游戏中的得分不起作用

[英]I can't figure out why the scoring isn't working in my rock, paper, scissors game

The game works fine except that the scores won't increment and my "referee" box isn't displaying the text I have assigned it based on the outcome. 该游戏运行正常,除了得分不会增加并且我的“裁判员”框未显示基于结果分配给我的文本。

I used an if else statement to randomly assign the computers choice and replace the image and set the computerChoice variable accordingly. 我使用了if else语句来随机分配计算机选择并替换图像,并相应地设置computerChoice变量。 I used and if elsee statement for the user input to assign the correct image and set the userChoice variable. 我使用if elsee语句为用户输入分配了正确的图像并设置了userChoice变量。 I then used a separate nested set of if else statements to compare the computerCoice and userChoice Variable and output text in the referee box as well as ad 1 point to the winners score box but this part isn't working. 然后,我使用一组单独的if else语句嵌套来比较computerCoice和userChoice变量,并在裁判框中输出文本,以及将广告1指向获胜者得分框,但这部分不起作用。 Any Ideas? 有任何想法吗?

     <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <title>Rock, Paper, Scissors!</title>
    <style>
        .clear {clear: both;}
        .box {width: 160px; height: 150px; float: left; border: 4px solid #000;}
        .wrap {width: 110px; float: left;}
        .wrap input {width: 85px;}
        form {width: 340px; margin: 20px auto 0 auto;}
    </style>

    <script>
    var referee = document.getElementById('ref').value;
    var userScore = document.getElementById('userScore').value;
    var computerScore = document.getElementById('compScore').value;
        userScore = 0;
        computerScore = 0;

        function startGame() {

            var computerChoice = Math.random();

                if (computerChoice >= 0 && computerChoice <= .33) {
                    computerChoice = "rock"
                    document.getElementById("compPic").src = "rock.jpg";

                } else if (computerChoice >= .34 && computerChoice <= .66) {
                    computerChoice = "paper"
                    document.getElementById("compPic").src = "paper.jpg";

                }else if (computerChoice >= .67 && computerChoice <= 1){
                    computerChoice = "scissors"
                    document.getElementById("compPic").src = "scissors.jpg";
                };

            var userChoice = document.getElementById('userChoice').value;

                if (userChoice === "rock" || userChoice === "Rock") {
                    userChoice = "rock";
                    document.getElementById("userPic").src = "rock.jpg";

                } else if (userChoice === "paper" || userChoice === "Paper") {
                    userChoice = "paper";
                    document.getElementById("userPic").src = "paper.jpg";

                } else if (userChoice === "scissors" || userChoice === "Scissors") {
                    userChoice = "scissors";
                    document.getElementById("userPic").src = "scissors.jpg";
                };



                if (userChoice === computerChoice) {
                    referee = "Tie!";
                }else if (userChoice === 'rock') {
                    if (computerChoice === 'paper') {
                        referee = 'Computer Wins!';
                        computerScore = ++computerScore;    
                    } else if (computerChoice === 'scissors') {
                        referee = 'You Wins!';
                        userScore = ++userScore;
                    };
                }else if (userChoice === 'paper') {
                    if (computerChoice === 'scissors') {
                        referee = 'Computer Wins!';
                        computerScore = ++computerScore;    
                    } else if (computerChoice === 'rock') {
                        referee = 'You Wins!';
                        userScore = ++userScore;
                    };
                }else if (userChoice === 'scissors') {
                    if (computerChoice === 'rock') {
                        referee = 'Computer Wins!';
                        computerScore = ++computerScore;    
                    } else if (computerChoice === 'paper') {
                        referee = 'You Wins!';
                        userScore = ++userScore;
                    };
                };

        }
    </script>

</head>
<body>
    <form>

        <div class="box"><img id="userPic" src="rps.jpg"></div>
        <div class="box"><img id="compPic" src="rps.jpg"></div>
        <br class="clear"><br>

        <div class="wrap">
            <label for="userScore">User Score:</label><br>
            <input type="text" name="userScore" id="userScore" value="" readonly>
        </div>
        <div class="wrap">
            <label for="ref">Referee:</label><br>
            <input type="text" name="ref" id="ref" placeholder="Referee's Call..." value="" readonly>
        </div>
        <div class="wrap">
            <label for="compScore">Computer Score:</label><br>
            <input type="text" name="compScore" id="compScore" value="" readonly>
        </div>

        <br class="clear"><br>

        <label for="userChoice">Choose Your Weapon:</label><br>
        <input type="text" name="userChoice" id="userChoice" placeholder="Rock, Paper, or Scissors?">
        <input type="button" value="Battle!" onclick="startGame()">

    </form> 
</body>
</html>

You must right user score to user score input field. 您必须将用户评分右移至用户评分输入字段。 Second you score will be reset on every game.. its not commutative.. I don't you want this or its missed by you for commutative.. define userScore = 0; 其次,您的得分将在每个游戏上重置。.它不具有可交换性。.我不希望这个或它被可交换性所错过。 computerScore = 0; computerScore = 0; before function definition. 在函数定义之前。

Try This 尝试这个

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <title>Rock, Paper, Scissors!</title>
    <style>
        .clear {clear: both;}
        .box {width: 160px; height: 150px; float: left; border: 4px solid #000;}
        .wrap {width: 110px; float: left;}
        .wrap input {width: 85px;}
        form {width: 340px; margin: 20px auto 0 auto;}
    </style>

    <script>
    //var referee = document.getElementById('ref').value;
    //var userScore = document.getElementById('userScore').value;
    //var computerScore = document.getElementById('compScore').value;
        userScore = 0;
        computerScore = 0;

        function startGame() {

            var computerChoice = Math.random();

                if (computerChoice >= 0 && computerChoice <= .33) {
                    computerChoice = "rock"
                    //document.getElementById("compPic").src = "rock.jpg";

                } else if (computerChoice >= .34 && computerChoice <= .66) {
                    computerChoice = "paper"
                    //document.getElementById("compPic").src = "paper.jpg";

                }else if (computerChoice >= .67 && computerChoice <= 1){
                    computerChoice = "scissors"
                    //document.getElementById("compPic").src = "scissors.jpg";
                };

            var userChoice = document.getElementById('userChoice').value;

                if (userChoice === "rock" || userChoice === "Rock") {
                    userChoice = "rock";
                    //document.getElementById("userPic").src = "rock.jpg";

                } else if (userChoice === "paper" || userChoice === "Paper") {
                    userChoice = "paper";
                   // document.getElementById("userPic").src = "paper.jpg";

                } else if (userChoice === "scissors" || userChoice === "Scissors") {
                    userChoice = "scissors";
                   // document.getElementById("userPic").src = "scissors.jpg";
                };



                if (userChoice === computerChoice) {
                    referee = "Tie!";
                }else if (userChoice === 'rock') {
                    if (computerChoice === 'paper') {
                        referee = 'Computer Wins!';
                        computerScore = ++computerScore;    
                    } else if (computerChoice === 'scissors') {
                        referee = 'You Wins!';
                        userScore = ++userScore;
                    };
                }else if (userChoice === 'paper') {
                    if (computerChoice === 'scissors') {
                        referee = 'Computer Wins!';
                        computerScore = ++computerScore;    
                    } else if (computerChoice === 'rock') {
                        referee = 'You Wins!';
                        userScore = ++userScore;
                    };
                }else if (userChoice === 'scissors') {
                    if (computerChoice === 'rock') {
                        referee = 'Computer Wins!';
                        computerScore = ++computerScore;    
                    } else if (computerChoice === 'paper') {
                        referee = 'You Wins!';
                        userScore = ++userScore;
                    };
                };
    document.getElementById('ref').value = referee;
    document.getElementById('userScore').value = userScore;
    document.getElementById('compScore').value =computerScore;
        }
    </script>

</head>
<body>
    <form>

        <div class="box"><img id="userPic" src="rps.jpg"></div>
        <div class="box"><img id="compPic" src="rps.jpg"></div>
        <br class="clear"><br>

        <div class="wrap">
            <label for="userScore">User Score:</label><br>
            <input type="text" name="userScore" id="userScore" value="" readonly>
        </div>
        <div class="wrap">
            <label for="ref">Referee:</label><br>
            <input type="text" name="ref" id="ref" placeholder="Referee's Call..." value="" readonly>
        </div>
        <div class="wrap">
            <label for="compScore">Computer Score:</label><br>
            <input type="text" name="compScore" id="compScore" value="" readonly>
        </div>

        <br class="clear"><br>

        <label for="userChoice">Choose Your Weapon:</label><br>
        <input type="text" name="userChoice" id="userChoice" placeholder="Rock, Paper, or Scissors?">
        <input type="button" value="Battle!" onclick="startGame()">

    </form> 
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 石头剪刀布游戏......无法弄清楚 - Rock Paper Scissors game… CANNOT FIGURE OUT 谁能告诉我为什么我的代码不能正常工作? 游戏石头、纸、剪刀。 Java 脚本 - Can anyone tell me why my code doesn't work properly? Game ROCK, PAPER, SCISSORS. Java Script 为什么我的剪刀石头布程序无法正常工作? - Why is my rock paper scissors program not working? 为什么我的剪刀石头布游戏无法正常工作? 它不断返回“您选择了岩石,您赢了”,我该如何解决? - Why is my Rock Paper Scissors game not working? It keeps returning 'You chose Rock, you win', how do i fix this? 我正在学习Javascript,试图找出为什么我的“摇滚剪刀”游戏无法正常工作。 。 - I'm learning Javascript, I'm trying to find out why my “Rock paper scissors” game wont work . . 剪刀石头布游戏的 If-else 语句没有返回应该返回的结果 - If-else statements for rock, paper, scissors game isn't returning the result that's supposed to return 我的石头剪刀布脚本不起作用。 控制台日志:未捕获的 SyntaxError:已声明标识符“playerSelection” - My Script for Rock Paper Scissors isn't working. Console logs: Uncaught SyntaxError: Identifier 'playerSelection' has already been declared 为什么我的石头剪刀布在第一次点击时不起作用,游戏运行良好但第一次点击没有任何反应 - Why is my rock paper scissors not working on the first click, the game works fine but the first click does nothing 如何运行我的石头剪刀布游戏? - How do I run my Rock Paper Scissors Game? 我的摇滚,纸,剪刀游戏不会重复 - 任何想法? - My Rock, Paper, Scissors Game Won't Repeat Properly - Any Ideas?
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM