可变参数、递归

Posted on 2024-04-10 22:32  琴音似君语  阅读(5)  评论(0编辑  收藏  举报

可变参数

package com.xqstudy.method;

public class Demo3 {
    public static void main(String[] args) {
        Demo3 demo3 = new Demo3();
        demo3.test(1,2,3,4,5,63);
    }

    public void test(int... i){
        System.out.println(i[0]);
        System.out.println(i[2]);
        System.out.println(i[1]);
        System.out.println(i[3]);
        System.out.println(i[4]);
        System.out.println(i[5]);
    }
}

递归

package com.xqstudy.method;

public class Demo4 {
    //递归思想
    public static void main(String[] args) {
        System.out.println(f(4));
    }
    //阶乘  递归
    //5! 5*4*3*2*1
    public static int f(int n){
        if (n==1){
            return 1;
        }else {
            return n*f(n-1);
        }
    }
}

Copyright © 2024 琴音似君语
Powered by .NET 9.0 on Kubernetes