有没有一种优雅的方法在 while 循环中跳过一次迭代?
我想要做的事情是:
while(rs.next())
{
if(f.exists() && !f.isDirectory()){
//then skip the iteration
}
else
{
//proceed
}
}
有没有一种优雅的方法在 while 循环中跳过一次迭代?
我想要做的事情是:
while(rs.next())
{
if(f.exists() && !f.isDirectory()){
//then skip the iteration
}
else
{
//proceed
}
}
继续
while(rs.next())
{
if(f.exists() && !f.isDirectory())
continue; //then skip the iteration
else
{
//proceed
}
}
虽然你可以使用continue
,但为什么不直接反转你的if语句的逻辑呢?
while(rs.next())
{
if(!f.exists() || f.isDirectory()){
//proceed
}
}
如果if
条件不满足,程序会继续执行下去,因此你甚至不需要一个else {continue;}
。
||
而不是 &&
。 - Rohit Jain在您想要跳过1次迭代的位置添加continue;
。
与break关键字不同,continue不会终止循环。相反,它跳到循环的下一次迭代,并停止执行本次迭代中的任何进一步语句。这允许我们绕过当前序列中其余的语句,而不停止通过循环的下一次迭代。
http://www.javacoffeebreak.com/articles/loopyjava/index.html
continue;
语句。不需要跳过迭代,因为其他部分在else
语句中,只有当条件不成立时才会执行。
但是如果你真的需要跳过它,可以使用continue;
语句。
while (rs.next())
{
if (f.exists() && !f.isDirectory())
continue;
//proceed
}
f.isDirectory()
的if
语句呢? - Rohit Jain