本文介绍了Java的while循环中的continue语句导致无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用continue语句创建while
循环.但是,这似乎导致了无限循环,我不知道为什么.
I'm trying to create a while
loop with a continue statement. However it seems to be causing an infinite loop and I can't figure out why.
下面的代码对我来说似乎应该以var tasksToDo
从3开始,然后递减到0并跳过数字2.
The code below to me seems like it should start with the var tasksToDo
at 3 then decrement down to 0 skipping number 2 on the way.
var tasksToDo = 3
while (tasksToDo > 0) {
if (tasksToDo == 2) {
continue;
}
console.log('there are ' + tasksToDo + ' tasks');
tasksToDo--;
}
推荐答案
conitnue
,将返回while循环.并且taskToDo的递减永远不会超过2.
conitnue
, will go back to the while loop. and tasksToDo will never get decremented further than 2.
var tasksToDo = 3
while (tasksToDo > 0) {
if (tasksToDo == 2) {
tasksToDo--; // Should be here too.
continue;
}
console.log('there are ' + tasksToDo + ' tasks');
tasksToDo--;
}
这篇关于Java的while循环中的continue语句导致无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!