用java计算斐波那契数列
package com.mypractice; import java.util.Scanner; /** * @author 康晨迪 * @class 计科研2021 * @create 2021-10-06 18:50 * 计算斐波那契数列 */ public class FeiBoNaQie { public static void main(String[] args) { System.out.println("请输入你想计算第几项斐波那契数列"); Scanner scanner = new Scanner(System.in); int input = scanner.nextInt(); long d1 = System.currentTimeMillis(); long result = feibo(input); long d2 = System.currentTimeMillis(); System.out.println("第"+input+"项的斐波那契数列为:"+result); System.out.println("斐波那契数列为:"); /** while(input > 0)//打印斐波那契数列 { System.out.print(feibo(input)+" "); input--; }**/ for(int i = 1; i <= input; i++)//打印斐波那契数列 { System.out.print(feibo(i)+" "); } System.out.print("\n"); System.out.println("递归耗时:"+(d2-d1)); } static long feibo(int n){ if(n == 1) { return 1; }else if(n == 2) { return 2; }else{ return (feibo(n-1)+feibo(n-2)); } } }