兔子斐波那契java

public class Demo {  
  
  
    public static int f(int n) throws Exception {  
        if(n==0){  
           throw new Exception("参数错误!");  
        }  
        if (n == 1 || n == 2) {  
            return 1;  
        } else {  
            return f(n-1)+f(n-2);//自己调用自己  
        }  
 }  
  
  
    public static void main(String[] args) throws Exception {  
        for (int i = 1; i <=10; i++) {  
            System.out.print(f(i)+" ");  
        }  
    }    
}  


猜你喜欢

转载自blog.csdn.net/qq_40221345/article/details/79356441
今日推荐