我的 java 应用程序收到此错误。我相信问题出在我的数组列表的添加函数上。有没有更好的方法来整合这个想法而不变得太复杂? (java的第一周)非常感谢大家!
import java.util.ArrayList;
import java.util.Scanner;
public class PrimeSieve {
public static void main(int[] args) {
System.out.println("Enter the max integer(N value): ");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
System.out.println("Compute prime numbers from 2 to: " + num);
if(num < 2){
System.out.println("N must be greater than or equal to 2.");
}
else if(num == 2) {
System.out.println("Prime numbers: 2");
}
else {
ArrayList<Integer> myArr = new ArrayList<Integer>();
int i = 2;
while(i <= num){
myArr.add(i);
}
System.out.println(myArr);
}
}
}
错误 -
Exception in thread "main" java.lang.NoSuchMethodError: main
最佳答案
将其声明为 public static void main(String[] args)
这样就可以解决问题了。您当前拥有 (int[] args)
。
请注意,这不是 while 循环异常,而是一个错误
由于找不到正确的 main 方法而在运行时生成。
请参阅 JLS 12.1.4: http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.1.4
关于Java while循环异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21540488/