package com.itheima;
/*
* 2、 求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
*/
import java.io.*;
import java.util.*;
class Test2
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in); //Resource leak: 'input' is never closed 总是报错警告,怎么避免
int n = input.nextInt(); //键盘输入数列项数
if(n<30)
fibo(n); //函数调用
else
System.out.println("请输入30以内的项数");
}
static int fibo(int y) //函数定义
{
int[] arr=new int[y] ; //数组定义
arr[0]=arr[1]=1;
System.out.print(arr[0]+" "+arr[1]+" ");
for(int x=2;x<y;x++)
{
arr[x]=arr[x-1]+arr[x-2];
System.out.print(arr[x]+" ");
}
return arr[y-1];
}
}
Resource leak: 'input' is never closed 总是报错警告,怎么避免?
|
|