[上]JAVA学习系列模块九第三章179.递归_练习2&阶乘
[上]JAVA学习系列模块九第三章179.递归_练习2&阶乘
2024-06-14 0 评论 0 阅读 0 点赞

[上]JAVA学习系列模块九第三章179.递归_练习2&阶乘

daimafengzi
2024-06-14 / 0 评论 / 0 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2024年06月14日,已超过750天没有更新,若内容或图片失效,请留言反馈。

[上]JAVA学习系列模块九第三章179.递归_练习2&阶乘

视频

笔记

示例二:求n!(n的阶乘)

1.需求:定义一个方法,完成3的阶乘
  3*2*1
  1. 分析:假如定义一个方法,代表n的阶乘 -> method(n) -> n接收几,就代表几的阶乘

    method(1) 1
    method(2) 21 -> 2method(1)
    method(3) 321 -> 3*method(2)

    method(n) -> n*method(n-1)

    public class Demo03Recursion {
     public static void main(String[] args) {
         int method = method(3);
         System.out.println("method = " + method);
     }
    
     public static int method(int n){
        if (n==1){
            return 1;
        }
        return n*method(n-1);
     }
    }

    图片

0

评论 (0)

取消