栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Day03 JAVA学习笔记之方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Day03 JAVA学习笔记之方法

简单的方法返回
public class Demo01 {
    //main方法
    public static void main(String[] args) {
        int sum = add(1,2);
        System.out.println(sum);

        test();
    }

    //加法方法
    public static int add(int a,int b){
        return a+b;
    }

    public static void test(){
        int[] numbers = {10,20,30,40,50};
        for(int x:numbers){
            System.out.println(x);
        }
    }
}
方法的定义 

 方法内的参数为形参,main方法内调用方法用的为实参。

方法调用

 

如果有返回值一定要有return;return是方法终止符

方法的重载 

 

 可变参数

 

public class Demo02 {
    public static void main(String[] args) {
        //调用可变参数的方法
        pirntMax(34,4,5,6);
        pirntMax((new double[]{1,3,5}));
    }

    private static void pirntMax(double... numbers){
        if(numbers.length == 0){
            System.out.println("no argument passed");
            return;
        }

        double result = numbers[0];

        //排序!
        for(int i = 1; i result){
                numbers[i] = result;
            }
        }
    }
}
递归

 

 

public class Demo03 {
    public static void main(String[] args) {
        System.out.println(f(5));
    }

    //5! 5*4*3*2*1
    public static int f(int n){
        if(n==1){
            return 1;
        }else {
            return n*f(n-1);
        }
    }
}

能不用递归就不用递归,java是栈机制。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/713684.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号