写一个方法returns指定斐波那契数列?

Writing a method which returns the specified Fibonacci number?

斐波那契数列是一组数字,其中前两个数字之后的每个数字都是前两个数字的总和,得到以下序列:

0 1 1 2 3 5 6 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

我如何使用递归编写一个方法,它将 return 指定的斐波那契数?我想避免使用数组

这是我目前所拥有的

public static int fibo(int n)
{
    if (n==1)
        return 1;
    else
        return //this is the part i am stuck on

由于某个斐波那契数(除 1 外)在其游标上,因此您调用:

public static int fibo(int n) {
    if (n < 0) {
        throw new IndexOutOfBoundsException("Can't calculate fibonacci number for negative index");
    } else if(n == 0 || n == 1) {
        return n;
    }

    return fibo(n-1) + fibo(n-2);
}
public static int fibo(int n){ 
  if(n<=2)
    return (n-1);
  else 
    return fibo(n-1) + fibo(n-2);
}

每个斐波那契数都是其前两个数之和。你不能只有 n=1 的基本情况,因为在计算 n = 2 时你会发现它是 n = 1 和 n = 0 的总和?这不会被定义。

注意:这是假设您对 fib 数字列表进行 1 索引,即 0 是第一个 1 第二个然后 1 第三个等等

public class Fibonacci
{
    public static int fibonacci(int n)
    {
        if (n == 0)
            return 0;
        else if (n == 1)
            return 1;
        else
            return fibonacci(n - 1) + fibonacci(n - 2);
    }

    public static void main(String[] args)
    {
        System.out.println(fibonacci(11));
    }
}