0

有没有一种方法可以在不改变其条件的情况下重新启动一个 while 循环一半的方式?

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

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

    enemy attack
}
4

2 回答 2

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
    }

    ...
}
于 2013-05-26T03:42:15.570 回答
2

采用continue

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

    if(view stats)
    {
       console.log(stats)
       continue;
    }

    enemy attack
}
于 2013-05-26T03:44:35.103 回答