main.java

来自「用java程序现实的计算斐波那契数列的非递归程序」· Java 代码 · 共 57 行

JAVA
57
字号
/*
 * Main.java
 *
 * Created on 2007年3月17日, 下午12:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package sore;

import java.io.*;

/**
 *
 * @author Administrator
 */
public class Main {
    
    /** Creates a new instance of Main */
    public Main() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static int F(int n) {
        int sum;
        if(n>=2)
            sum=F(n-2)+F(n-1);
        else
            if(n==1)
               sum=1;
            else
               sum=0;
        return sum;
    }
    
    public static void main(String[] args) throws IOException{
        // TODO code application logic here
        BufferedReader keyin = new BufferedReader( new          //接收字符
                    InputStreamReader(System.in));
        int num;
        do{
            System.out.println("请输入整数N(N>=0):");        //建立键盘上的输入
            num = Integer.parseInt(keyin.readLine());
            if(num<0)
            {
                System.out.println("输入错误!");
            }
        }while(num<0);
        long sum=F(num);
        System.out.println("结果为:"+sum);
    }
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?