Groovy while loop not executed correctly

坚强是说给别人听的谎言 提交于 2019-12-11 15:27:30

问题


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++
}

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

回答1:


This works fine:

~ $ 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


来源:https://stackoverflow.com/questions/58395746/groovy-while-loop-not-executed-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!