它主要工作,但当它命中时,它不会循环。我在 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;
}