0

我正在制作一个也有统计数据的小游戏。赢得时间和类似的东西。所以我做了这个html

<div id="stats">
    <h2>Stats</h2>
    <p>Win ratio: </p> <!-- Ignore this one -->
    <p><span id="wincount"></span> wins / <span id="lostcount"></span> losses</p>
</div>

这是Javascript代码。我稍微缩短了它,所以你只能看到重要的东西:

var wontimes = 0;
var losstime = 0;
function getResult(){
    var tie = "It's a tie!";
    var won = "You have won!";
    var lost = "You lost!";
    switch(user){
    case 1: // rock
        switch(npc){
        case 1: // rock
            result = tie;
            break;
        case 2: // paper
            result = lost;
            break;
        }
        break;
    case 2: // paper
        switch(npc){
        case 1: // rock
            result = won;
            break;
        case 2: // paper
            result = tie;
            break;
        }
        break;
    }
    document.getElementById("gameresult").innerHTML = result;
    if (result === won){
        return wontimes = wontimes++;
    } else if (result === lost){
        return losstime = losstime++;
    } else {
        console.log("Tie");
    }
    document.getElementById("wincount").innerHTML = wontimes;
    document.getElementById("lostcount").innerHTML = losstime;
}

当用户单击按钮时调用函数 getResult 并且它可以工作,但是它存在问题。我点击后。它告诉我我是否赢了等等。但它不算赢。我想让它每次用户获胜,wontimes 得到一个加号,然后它被打印出来<span id="wincount"></span>,它甚至不显示默认的 0 获胜。

有人能帮我吗?谢谢你。

4

1 回答 1

6

如果从函数返回,后面的行将被忽略。

改变

    return wontimes = wontimes++;

    wontimes++;

(对于 也是如此losstime

Note also that wontimes++ is enough for incrementing a variable. If you do wontimes = wontimes++, you give to wontimes the value it had before the incrementing, which makes the whole useless.

于 2013-03-30T11:57:45.993 回答