在循环中找到最小数字并验证它是否等于groovy中的某个整数值

时间:2016-05-16 16:18:33

标签: loops groovy

我对groovy比较新,所以我不确定我是否正确行事

我在循环中运行查询以找到最小值,如果该最小值等于12,则打印它

 connection.eachRow(query) {

    def errorstate = false
    if (y < x && y !=0)
    {
    errorstate = true
    x = y  ---> if y is less than x then set x = value of y
    }
  /* if y is greater then  the value of x will not change */
if (x == 12) 
    printf ("%-30s\t%-20s\t\n" , name,y)

    }

提前致谢!!

1 个答案:

答案 0 :(得分:0)

试试这个:

connection.eachRow(query) {

    def errorstate = false
    if (y != 0)
    {
        errorstate = y < x
        x = Math.min(x, y)
    }

}

/* if y is greater then  the value of x will not change */
if (x == 12) 
    printf "%-30s\t%-20s\t\n" , name, y

这假设eachRow将在返回之前调用每行上给出的闭包。我不知道是否是这种情况,因为您没有发布您正在使用的库。

很可能eachRow将在一个单独的线程中运行闭包,在运行任何循环之前立即返回。查看eachRow的文档,了解是否属于这种情况。