斐波那契数列

import java.util.Scanner;

public class Fibonacci {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("pls input number");
        int i = sc.nextInt();
        for (int j = 1; j <= i; j++) {
            int number = fibonacci(j);
            System.out.println(number);
        }
    }

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

 

你可能感兴趣的:(java,递归,斐波那契数列)