package com.itheima;
import java.util.Scanner;
/*
* 2、求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
*/
public class Test2 {
public static void main(String[] args) {
//用用定义数组的方法求出,[n]=[n-1]+[n-2]
System.out.println("请输入您要求出第几项斐波那契数,输入数应小于30");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
int[] arr = new int[30];
arr[0] = 1; //初始化前两项
arr[1] = 1;
for(int n = 0;n<arr.length;n++){
arr[n] = arr[n-1] + arr[n-2];
arr = arr[n];
}
System.out.println(arr);//输出第n项
}
}
[b]运行会出现错误 |
|