Groovy while循环未正确执行 [英] Groovy while loop not executed correctly
本文介绍了Groovy while循环未正确执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遵循了Groovy中的简单while循环代码-
I've following simple while loop code in groovy -
def count = 1
while(count <= 5) {
println "$count"
sleep(5000)
println "Sleeping for 5 seconds"
count++
}
这表示在第二次未运行Sleeping for 5 seconds
时,循环仅执行了两次.实际上使用此代码,而代码块预计将执行5次.有人可以帮助理解为什么会有这种奇怪的行为吗?
Which indicates that loop is executed only twice still second time Sleeping for 5 seconds
is not run. Actually with this code, while block is expected to be executed 5 times. Can someone help to understand why such a weird behaviour?
运行此代码时,输出如下-
When this code is run, output is following -
1
Sleeping for 5 seconds
2
推荐答案
这很好:
~ $ cat doit.groovy
def count = 1
while(count <= 5) {
println "$count"
sleep(5000)
println "Sleeping for 5 seconds"
count++
}
~ $ groovy doit
1
Sleeping for 5 seconds
2
Sleeping for 5 seconds
3
Sleeping for 5 seconds
4
Sleeping for 5 seconds
5
Sleeping for 5 seconds
这篇关于Groovy while循环未正确执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文