我是 Java 编程新手。
有人可以帮助我编写以下代码吗:
public class RandomSeq {
public static void main(String[] args) {
// command-line argument
int N = Integer.parseInt(args[0]);
// generate and print N numbers between 0 and 1
for (int i = 0; i < N; i++) {
System.out.println(Math.random());
}
}
}
尝试编译时收到以下错误消息:
线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0
提前谢谢您。
[1] 我使用 64 位 Java 8(更新 25)SE 实现,使用 64 位 Eclipse IDE for Java Developers (v. 4.4.1)。
最佳答案
如果您运行 main()
如果不给出参数列表( String[] args
),它将需要 args
为空。
所以,索引 0
不是有效的索引,因为 args
为空。
int N =Integer.parseInt(args[0]);// this cause ArrayIndexOutOfBoundsException
如何设置 main()
的参数在 eclipse 中。? Read from here .
关于java - 为什么我会收到 'ArrayIndexOutOfBoundsException' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26418113/