递归的应用

package lyr;
import java.io.*;
public class B123 {
    public static void main(String args[])throws IOException
    {
        
        int n;
        String str;
        BufferedReader buf;
        buf=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("请输入一个正整数:");
        str=buf.readLine();
        n=Integer.parseInt(str);
        if(n<=0)
            System.out.print("输入错误,应输入正整数。");
        else
            System.out.print("f("+n+")="+f(n)+"\n");
    }
    public static int f(int a)
    {
        if(a==1)
            return (1);
        else if(a%2==1)
            return f(a-1)+1;
        else
            return f(a-1)+2;
    }
}

你可能感兴趣的:(java,eclipse)