[英]i wrote the code for fibonacci series but i get error ArrayIndexOutofBoundsException. can you please help me
I wrote the code for Fibonacci series but I get error ArrayIndexOutofBoundsException
. 我写了斐波那契数列的代码,但出现错误ArrayIndexOutofBoundsException
。 Can you please help me find source of this exception? 您能帮我找到此异常的来源吗?
class Fib {
public static void main(String args[]) {
int num = Integer.parseInt(args[0]);
System.out.println("Fibonacci Series");
int f1, f2 = 0, f3 = 1;
for (int i = 1; i <= num; i++) {
System.out.print(" " + f3 + " ");
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
}
}
You didn't supply any command-line arguments to your program, so args
is a zero-length array. 您没有向程序提供任何命令行参数,因此args
是零长度的数组。 Any access of a zero-length array will result in an ArrayIndexOutOfBoundsException
. 零长度数组的任何访问都将导致ArrayIndexOutOfBoundsException
。
Check if args.length
is at least 1
before accessing the first element (of index 0
). 在访问第一个元素(索引为0
)之前,请检查args.length
是否至少为1
。
I haven't tested the code you submitted but here is couple of clues: 我尚未测试您提交的代码,但以下是一些提示:
I hope this helps. 我希望这有帮助。
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.