Groovy,range操作符有问题

Groovy,range操作符有问题,groovy,operators,range,Groovy,Operators,Range,为了多次执行同一代码,我在Groovy中使用range运算符编写了一个方法: /** * Prints the {@code files} {@code copyCount} times using * {@code printService}. * <p> * Exceptions may be thrown. * @param printService Print service * @param files List of {@code File} objects

为了多次执行同一代码,我在Groovy中使用range运算符编写了一个方法:

/**
 * Prints the {@code files} {@code copyCount} times using 
 * {@code printService}.
 * <p>
 * Exceptions may be thrown.
 * @param printService Print service
 * @param files List of {@code File} objects
 * @param copyCount Number of copies to print
 */
private static void printJob(
        PrintService printService, 
        List<File> files, 
        int copyCount) {

    // No multiple copy support for PS files, must do it manually
    for ( i in 1..copyCount ) {
        // Print files
    }
}
同样在Groovy
a中。。[a..b-1]
定义为:
u(0)=a
,对于
[1,| b-a |-1]
中的所有
i
u(i)=u(i-1)+sgn(b-a)

我注意到下面的代码也适用于
copyCount
正或零:

    for ( i in 0..<copyCount ) {
        // Print files
    }
至少在这个解决方案中,我得到了一个
GroovyRuntimeException:Infinite循环
,以防出现负
copyCount
。它非常棒,但不是很漂亮,我觉得我在玩火

也有这种解决办法,但我觉得很难看

    for ( i in 0..<[0,n].max() ) {
        // Print files
    }

for(i in 0...
copyCount.times{…}
?负数不需要迭代。这是groovy!谢谢hsan!
    0.step(copyCount, 1) {
        // Print files
    }
    for ( i in 0..<[0,n].max() ) {
        // Print files
    }
    for ( i = 1 ; i <= copyCount ; i++ ) {
        // Print files
    }