如何在中途重启一个while循环?

时间:2013-05-26 03:39:19

标签: javascript

有没有办法可以在不改变条件的情况下重新启动while循环中途?

while(health > 0 &&enemyhealth > 0){
    if(attack)
    {
        attack
    }

    if(view stats)
    {
       console.log(stats)
       restart loop
    }

    enemy attack
}

2 个答案:

答案 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
}