2016-02-28 25 views
0

我一直在Groovy中做一些测试。我已经完成了这段代码,并得到这个错误groovy.lang.GroovyRuntimeException - Groovy错误

抓住:groovy.lang.GroovyRuntimeException:此脚本或类无法运行。 它应该:

  • 具有主要方法,

  • 是JUnit测试或延伸的GroovyTestCase,

  • 实现Runnable接口,
  • 或是与注册的脚本转轮兼容。已知的参赛者: 无

class Prime { 

public static def prime(int x){ 
    boolean result = true; 
    int i, j, temp; 
    temp = 0; 

    if (x < 2){ 
     result = false; 
    }else { 
     for(i = 2; i < x && j == 0; i++){ 
      temp = x % i; 
      if(temp == 0){ 
       result = false; 
      } 
     } 
    } 
    return result; 
} 

static void main() { 

    long time_start, time_end, time_exe; 
    int primes = 0; 
    int N = (int) Math.pow(8, 5); 

    time_start = System.currentTimeMillis(); 

    def fichero = new File("salida2.out") 

    for (int i = 0; i <= N; i ++) { 
     if (prime(i) == true) { 
     String line = "" + i + " is prime."; 
     fichero.append(line); 

     } 
    } 

    time_end = System.currentTimeMillis(); 

    time_exe = time_end - time_start; 

    println("Execution time: " + time_exe + "milliseconds"); 
    println("Prime Numbers Found: " + primes); 
} 

} 
+0

我假设的问题,那么,是“我究竟做错了什么?”如果是这样,你的帖子应该清楚地表明问题是什么。 – Castaglia

回答

1

main方法的签名是不正确(需要字符串参数... args)。

将其更改为:

public static void main(String... args) { 
+0

是的,@pczeus,你完全正确,祝你有美好的一天。谢谢yoy – Manuel

+0

@Manuel,如果这个答案解决了你的问题,请考虑接受它。 –

+2

在常规'静态主(args)'已经足够 –