1

在以下 groovy 代码中2(i-1)内部 for 循环中的动态片段有什么问题?如果我用2iori+1或任何硬编码的数字(例如6)替换它就可以了。

  1     class Algorithms {
  2      static def printTriangle(n){
  3           for(i in 1..n){
  4            if(i>1){
  5               for(j in 1..2(i-1)){ print 1 }
  6             } else { print 1 } } //end of inner loop
  7               println ""
  8           } //end of outer loop
  9       }

 24       static def main(args){

 26            printTriangles(4)
 27       }
 28   }

堆栈跟踪是

1
Caught: groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.call() is applicable for argument types: (java.lang.Integer) values: [1]
Possible solutions: wait(), any(), abs(), wait(long), wait(long, int), each(groovy.lang.Closure)
groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.call() is applicable for argument types: (java.lang.Integer) values: [1]
Possible solutions: wait(), any(), abs(), wait(long), wait(long, int), each(groovy.lang.Closure)
    at Algorithm.printTriangle(Algorithm.groovy:5)
    at Algorithm$printTriangle.callStatic(Unknown Source)
    at Algorithm.main(Algorithm.groovy:26)
4

1 回答 1

0

啊,我没有做数学,以下代码有效。

  for(j in 1..2*(i-1)){}
于 2013-10-18T16:33:23.423 回答