Javascript:Math.random()变量每次引用变量时都会更改?

时间:2015-03-05 19:35:30

标签: javascript while-loop

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;

while(slaying){
    if(youHit){
        console.log("You slice the dragon!")
        totalDamage += damageThisRound;
        if(totalDamage>=4){
            console.log("Dragon is deafeated!");
            slaying=false;
        }
        var youHit = Math.floor(Math.random() * 2);
    }else{
        console.log("Dragon is too strong for you. Defeated.")
        slaying=false;
    }
}

这是一个关于Javascript教程的简短程序。但是,我没有看到DamageThisRound如何改变价值。

使用Math方法在顶部初始化。然后我们在While循环中开始游戏,其中damageThisRound的值永远不会改变。显然它应该在每次循环时改变(对于每个'round'或'hit')但是在循环中没有任何改变。我在这里错过了什么吗?

0 个答案:

没有答案
相关问题