Java经典编程题50道之二十二

利用递归方法求5!。

public class Example22 {
    public static void main(String[] args) {
        int n = 5;
        long s = sum(n);
        System.out.println(n + "!= " + s);
    }

public static long sum(int n) {
        long s = 1;
        if (n == 1||n==0) {
            s = 1;
        } else {
            s = n * sum(n - 1);
        }
        return s;
    }
}

时间: 2024-08-08 20:57:53

Java经典编程题50道之二十二的相关文章

Java经典编程题50道之五十

有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud"中. public class Example50 {    public static void main(String[] args) {        stud();    } public static void stud() {        Scanner ss = new Scanner(System.in); 

Java经典编程题50道之二十五

一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. public class Example25 {    public static void main(String[] args) {        f2(123454321);    }//方法一    public static void f1(int n) {        if (n >= 10000 && n < 100000) {            String s = S

Java经典编程题50道之二十

有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和. public class Example20 {    public static void main(String[] args) {        sum(20);    } public static void sum(int n) {        double x = 2.0;        double y = 1.0;        double t;        double

Java经典编程题50道之十二

企业发放的奖金根据利润提成:利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成, 高于10万元的部分 ,可提成7.5%:20万到40万之间时,高于20万元的部分,可提成5%:40万到60万之间时,高于40万元的部分,可提成3%:60万到100万之间时 ,高于60万元的部分,可提成1.5%:高于100万元时,超过100万元的部分按1%提成. 从键盘输入当月利润I,求应发放奖金总数? public class Example12 {  

Java经典编程题50道之三十二

取一个整数a从右端开始的4-7位. public class Example32 {    public static void main(String[] args) {        cut(123456789);    } public static void cut(long n) {        String s = Long.toString(n);        char[] c = s.toCharArray();        int j = c.length;      

Java经典编程题50道之四十二

809*??=800*??+9*??+1,其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数.求??代表的两位数,以及809*??后的结果. public class Example42 {    public static void main(String[] args) {        f();    } public static void f() {        int a = 809, b, i;        for (i = 10; i < 13; i++)

Java经典编程题50道之二十六

请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母. public class Example26 {    public static void main(String[] args) {        f();    } public static void f() {        System.out.println("请输入星期的第一个大写字母:");        char ch = getChar();        switch (ch

Java经典编程题50道之二十九

求一个3*3矩阵对角线元素之和. public class Example29 {    public static void main(String[] args) {        int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };        sum(a);    } public static void sum(int[][] a) {        System.out.println("您输入的3*3矩阵是:");

Java经典编程题50道之二十七

求100之内的素数. public class Example27 {    public static void main(String[] args) {        prime();    } public static void prime() {        System.out.print(2 + "\t");        System.out.print(3 + "\t");        int count = 2;        boolea

Java经典编程题50道之二十八

对10个数进行排序. public class Example28 {    public static void main(String[] args) {        int[] s = { 5, 7, 6, 1, 9, 4, 2, 3, 8 };        BubbleSort(s);    } public static void BubbleSort(int[] a) {        System.out.print("原数组为:");        for (int