Java:thinging in java p153 exercise 9

 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

 

posted @ 2015-04-17 11:50  陶修瑕  阅读(140)  评论(0编辑  收藏  举报