java的递归(基础)

阶乘:
一个正整数的阶乘(factorial)是所有小于及等于该数的正整数的积,并且0的阶乘为1。 自然数n的阶乘写作n!。

如:2! = 21 3! = 32*1
此案例使用阶乘的计算来方便理解递归思想,但日常编辑代码时请减少/不使用递归。

public static void main(String[] args) {
    
    
        int i = 5;
        System.out.println(i + "!=" + ii(i));
    }
public static int ii(int i) {
    
    
        if (i == 1) {
    
    
            return 1;
        } else if (i > 0) {
    
    
            return i * ii(i - 1);
        } else {
    
    
            System.out.println("输出有误。");
            return i;
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_45380885/article/details/109127710