当我尝试运行此代码时,我收到此错误..我不知道我哪里出错了..
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at numericalComputatio.fibo.main(fibo.java:30)
package numericalComputatio;
public class fibo {
static double c = -0.618;
// double c = [(1-sqrt(5))/2] = - 0.618
/**
* Computes the fibonacci series
* @param n
* @return
*/
private static double fibo(int n){
if (n == 0)
return 1;
else if (n == 1)
return c;
else
{
double result = fibo(n - 1) + fibo(n - 2);
return result;
}
}
public static void main(String[] args) {
int n = 0;
double result = 0.0;
double result1 = 1.000000000;
if (args[0] != null)
n = Integer.parseInt(args[0]);
for(int i = 0; i<=n; i++)
{
result = fibo(i);
System.out.println("fib(" + i + ") = " + result + "Formula value = " + result1);
result1 = result1*c;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里:
args[0]
Run Code Online (Sandbox Code Playgroud)
在第30行
if (args[0] != null)
Run Code Online (Sandbox Code Playgroud)
你必须传递一个论点.
args[0]尝试访问args数组中的第一个元素,因为它是从命令行参数填充的.如果您没有传递任何数组为空的参数,并且尝试访问数组中的非现有元素会给出该异常.
您必须学会阅读异常堆栈跟踪.它们起初似乎毫无意义,但一旦你知道如何阅读它们,它们就会非常有用.这是你的:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at numericalComputatio.fibo.main(fibo.java:30)
Run Code Online (Sandbox Code Playgroud)
它看起来像这样:
public static void main方法启动的流程java.lang.ArrayIndexOutOfBoundsException: 0这意味着有一个涉及的数组,索引尝试访问为0(第一个元素),为您提供了一个很好的线索.fibo.java:30 特别是当您拥有该源文件时,这也非常有用,并且可以直接查看该行号.我希望这有帮助.
| 归档时间: |
|
| 查看次数: |
66459 次 |
| 最近记录: |