【问题标题】:Rock, Paper, Scissors Increment Score not working, can somebody tell me what i'm doing wrong?Rock,Paper,Scissors Increment Score 不起作用,有人能告诉我我做错了什么吗?
【发布时间】:2018-10-07 17:08:06
【问题描述】:

我的图片和消息正在更改为显示胜利、失败、平局,但我在 javascript 中的增量似乎不起作用,因为我的分数没有改变。请帮忙:)

<body>

    <h1>Rock, Paper, Scissors</h1>

    <button class="rockbtn" id="playRock">Rock</button>
    <button class="play" id="playPaper">Paper</button>
    <button class="play" id="playScissors">Scissors</button>

    <h2 id="humantitle">Human</h2>
    <img alt="humanPlay" id="human" src="img/scissors.png">

    <h2 id="comptitle">Computer</h2>
    <img alt="compPlay" id="computer" src="img/scissors.png">

    <div id="count">
        Human: <input id="playerWin">
        <br><br> Computer: <input id="compWin">
        <br><br> Draw: <input id="draw">
    </div>

    <h3 id="output"></h3>

    <button id="newgame">New Game</button>

    <script src="rps.js"></script>
</body>


</html>

我在这里建立了我的变量,这对我来说看起来是正确的......

var playerWin = 0;
var compWin = 0;
var draw = 0;
var playerChoice; //Rock = 1, Paper = 2, Scissors = 3

function compChoice() {
    var compChoice = Math.floor(Math.random() * 3 + 1);

    if (compChoice === 1) {
        document.getElementById('computer').src = "img/rock.png";
    }

    if (compChoice === 2) {
        document.getElementById('computer').src = "img/paper.png";
    }

    if (compChoice === 3) {
        document.getElementById('computer').src = "img/scissors.png";
    }

我已经根据游戏结果将我的增量放在下面的 if 语句中,但没有运气......

//draw
        if (playerChoice === compChoice) {
            document.getElementById("output").textContent = "You Tied!";

        document.getElementById("draw").innerHTML = draw++;
    }

    //Lose 
    //Rock vs Paper
    else if (playerChoice === 1 && compChoice === 2) {
        document.getElementById("output").textContent = "You Lose! Paper covers rock!"

        document.getElementById("compWin").innerHTML = compWin++;
    }

    //Scissors vs Rock
    else if (playerChoice === 3 && compChoice === 1) {
        document.getElementById("output").textContent = "You Lose! Rock smashes scissors!"

        document.getElementById("compWin").innerHTML = compWin++;
    }

    //Scissors vs Rock
    else if (playerChoice === 2 && compChoice === 3) {
        document.getElementById("output").textContent = "You Lose! Scissors cuts paper!"

        document.getElementById("compWin").innerHTML = compWin++;
    }

    //Win
    //Rock vs Scissors
    else if (playerChoice === 1 && compChoice === 3) {
        document.getElementById("output").textContent = "You Win! Rock smashes scissors!"

        document.getElementById("playerWin").innerHTML = playerWin++;
        }

    //Scissors vs Paper
    else if (playerChoice === 3 && compChoice === 2) {
        document.getElementById("output").textContent = "You Win! Scissors cuts paper!"

        document.getElementById("playerWin").innerHTML = playerWin++;
    }

    //Rock vs Paper
    if (playerChoice === 2 && compChoice === 1) {
        document.getElementById("output").textContent = "You Win! Paper covers rock!"

        document.getElementById("playerWin").innerHTML = playerWin++;
    }
}

//playerChoice = Rock
document.getElementById("playRock").onclick = function () {
    playerChoice = 1;
    document.getElementById('human').src = "img/rock.png";

    compChoice();
}

//playerChoice = Paper
document.getElementById("playPaper").onclick = function () {
    playerChoice = 2;
    document.getElementById('human').src = "img/paper.png";

    compChoice();
}

//playerChoice = Scissors
document.getElementById("playScissors").onclick = function () {
    playerChoice = 3;
    document.getElementById('human').src = "img/scissors.png";

    compChoice();
}

【问题讨论】:

    标签: javascript


    【解决方案1】:

    ++ 是一个奇怪的运算符。它增加值然后返回原始值

    只需在单独的一行中说draw++;,然后在下一行显示。

    另外,&lt;input&gt; 元素不包含 HTML。设置value 属性,而不是innerHTML 属性。或者最好您可以将它们更改为 &lt;span&gt; 元素或其他内容,因为它们不是输入。

    【讨论】:

    • ++ 在行(或块)之后递增,用于for 循环等——反正我就是这么记得的。
    • @CD-RUM:甚至不一定是“同一行或同一块”,它是表达式级别的东西。最好不要将其他运算符与 ++/-- 混合使用,因为它很快就会变得混乱。
    • 必须按照大纲使用 ,更改为 value 属性,一切都很好!谢谢!
    • ++var = 将增加当前循环中的值。 var++ 将在下一个循环中递增。更多here
    • @hungrykoala:不,时间也与循环无关。例如,如果您有一个 for 循环,那么在增量部分是否有 var++ 或 ++var 都没有区别。
    猜你喜欢
    • 1970-01-01
    • 2021-12-08
    • 2014-04-19
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    相关资源
    最近更新 更多