开发者社区 问答 正文

用递归的方法计算n!的值,n从键盘输入

用递归的方法计算n!的值,n从键盘输入

展开
收起
知与谁同 2018-07-19 17:33:18 1882 分享 版权
1 条回答
写回答
取消 提交回答
  • class TestFive{
    public static void main(String[] args) throws IOException {
    Scanner in=new Scanner(System.in);
    int n = in.nextInt();  
    System.out.println(jiecheng(n));
    }
    public static int jiecheng(int n){
    if(n == 1){
    return 1;
    }else{
    return n*jiecheng(n-1);
    }
    }
    }
    2019-07-17 22:55:00
    赞同 展开评论
问答地址: