本帖最后由 My_work 于 2014-3-4 13:31 编辑
- import java.util.Scanner;
- /**
- * 第二题:求斐波那契数列第n项,n<30
- * 斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
- * @author My_work
- * */
- public class Test2 {
- /*public static void main(String[] args){
- int n = 0;
- int fn = FN(n);
- System.out.println("您要求第几项的值,请输入:");
- }
- public static int FN(int n){
- return fn;
-
- }*/
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //获取键盘输入的数值
- Scanner sc= new Scanner(System.in);
- int n = sc.nextInt();
- int result = 0;
- int i=1;
- int j=0;
- for(int r=1;r<=n;r++)
- {
- result=i+j;
- i=j;
- j=result;
-
- }
- //输出第n项的数值
- System.out.print(result);
- }
- }
复制代码
|
|