package com.itheima;
import java.util.Scanner;
/*
* @author xujie
* @version 1.0.1
* 问题:
* 求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
*/
public class Test4 {
public static void main(String[] args) {
//输入一个数n
Scanner s=new Scanner(System.in);
int num=s.nextInt();
//调用函数,得到第n项的值
int w=function(num);
System.out.println("第"+num+"项的值为:"+w);
}
private static int function(int num) {
if(num==1||num==2)
return 1;
return function(num-1)+function(num-2);
}
}
|