我有这个代码:
public class OddSum1 {
public OddSum1() {
}
public static void main(String[] args) {
int OddLimit = Integer.parseInt(args[0]);
int sum = 0;
for (int i = 1; i <= OddLimit; i += 2) {
sum += i;
}
System.out.println("The sum of odd numbers from 1 to " + OddLimit + " is " + sum);
}
}
每当我运行它时,我都会收到此错误:
java.lang.ArrayIndexOutOfBoundsException: 0
at OddSum1.main(OddSum1.java:7)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
它应该提示用户输入一个数字,然后将奇数从 1 与输入的数字相加,我猜这是一个问题int OddLimit = Integer.parseInt(args[0])
;
但我只是不知道如何解决它,任何帮助都会很棒。