它主要起作用,但当它击中时,它不会循环。我在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;
}
答案 0 :(得分:2)
变量slaying
在循环结束时始终设置为false
,因此它永远不会迭代多次。