有没有办法可以在不改变条件的情况下重新启动while循环中途?
while(health > 0 &&enemyhealth > 0){
if(attack)
{
attack
}
if(view stats)
{
console.log(stats)
restart loop
}
enemy attack
}
答案 0 :(得分:4)
听起来像你想要continue
。
while (health > 0 && enemyhealth > 0){
...
if (...)
{
...
continue; // This will skip the rest of the loop body,
// check the loop condition again, and keep going
// if the while condition is still true
}
...
}
答案 1 :(得分:2)
使用continue
while(health > 0 && enemyhealth > 0){
if(attack)
{
attack
}
if(view stats)
{
console.log(stats)
continue;
}
enemy attack
}