package com.itheima;
/**
*求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
*/
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入项数:");
int n = in.nextInt();
System.out.println("斐波那契数列第" + n + "项为:" + method(n));
}
public static int method(int num) {
if (num == 1||num == 2)
return 1;
return method(num - 1) + method(num - 2);
}
}
|
|