-4

它主要工作,但当它命中时,它不会循环。我在 while 循环中做错了什么?

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 hit the dragon.");
        totalDamage += damageThisRound;
        if(totalDamage>=4){
            console.log("You slew the dragon.");
            slaying=false;
        }else{
            youHit = Math.floor(Math.random()*2);
        }
    }else{
        console.log("The dragon defeated you.");
    }
    slaying = false;
}
4

1 回答 1

2

该变量slaying总是false在循环结束时设置为,因此它永远不会迭代超过一次。

于 2013-06-22T00:00:05.087 回答