---
title: "java实现斐波那契数列"
author: "zhglhy"
date: "2017-04-13T15:16:16Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2017-04-13T15:16:16Z"
id: 2368942
url: "https://zhglhy.iteye.com/blog/2368942"
---

```
public class FibTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		
		for(int i=1;i<10;i++){
			System.out.println(fib(i));
			
			System.out.println(getFib(i));
			
			System.out.println("=========");
		}

	}
	
	public static int fib(int n){
		if(n==1 || n==2){
			return 1;
		}
		return fib(n-1)+fib(n-2);
	}
	
	public static int getFib(int n){
		
		if(n==1 || n==2){
			return 1;
		}
		
		int a=1;
		int b=1;
		int c=0;
		
		for(int i=1; i<=n-2; i++){
			c=a+b;
			a=b;
			b=c;
		}
		return c;
	}

}
```