A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 龙旋 中级黑马   /  2016-8-7 23:54  /  423 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

求n的阶乘 !3 = 3*2*1;

2 个回复

倒序浏览
        public static void main(String[] args) {
                System.out.println(fun(3));
        }
        public static int fun(int i) {
                if(i == 1)
                        return 1;
                else
                        return i * fun(i - 1);
        }
回复 使用道具 举报
import java.util.Scanner;  //导包,键盘录入

public class Test_ToString {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int x = sc.nextInt();
                System.out.println(fun(x));
        }

        public static int fun(int i) {
                if (i == 1)
                        return 1;
                else
                        return i * fun(i - 1);
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马