用非递归的方法打印Fibonacci数列

Bol5261
新星创作者: Java技术领域
2021-10-17 23:51:46
public class S_Method5 {

    public static void main(String[] args){
        System.out.println("用非递归的方法打印Fibonacci数列:"+method(40));
    }
    public static int method(int n){
        int sum=0;
        int first=1,second=1;
        if(n<1){
            System.out.println("参数无效,输入的应为正整数");
            return -1;
        }
        if(n==1||n==2) return 1;
        for(int i=0;i<n-2;i++){
            sum=first+second;
            first=second;
            second=sum;
        }
        return sum;
    }
}


用非递归的方法打印Fibonacci数列:102334155

Process finished with exit code 0

 

...全文
89 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧