有没有一种方法可以在不改变其条件的情况下重新启动一个 while 循环一半的方式?
while(health > 0 &&enemyhealth > 0){
if(attack)
{
attack
}
if(view stats)
{
console.log(stats)
restart loop
}
enemy attack
}
有没有一种方法可以在不改变其条件的情况下重新启动一个 while 循环一半的方式?
while(health > 0 &&enemyhealth > 0){
if(attack)
{
attack
}
if(view stats)
{
console.log(stats)
restart loop
}
enemy attack
}
听起来像你想要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
}
...
}
采用continue
while(health > 0 && enemyhealth > 0){
if(attack)
{
attack
}
if(view stats)
{
console.log(stats)
continue;
}
enemy attack
}