1 import java.util.Scanner; 2 3 public class Fibonacci { 4 5 int fibo(int n) { 6 switch (n) { 7 case 1: 8 return 1; 9 case 2: 10 return 1; 11 default: 12 return fibo(n - 1) + fibo(n - 2); 13 } 14 } 15 16 public static void main(String[] args) { 17 18 Scanner scanner = new Scanner(System.in); 19 int n = scanner.nextInt(); 20 21 Fibonacci f = new Fibonacci(); 22 23 for (int i = 0; i < n; i++) { 24 System.out.println(f.fibo(i + 1)); 25 } 26 } 27 }
输出
1 5 2 1 3 1 4 2 5 3 6 5