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

05.Java中方法的使用

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

05.Java中方法的使用

Java中方法的使用

1.一个例子带你快速了解Java中方法的使用2.形参和实参的关系3.方法重载4.方法的递归:

4.1求斐波那契数列某一项的值4.2青蛙跳台阶问题4.3汉诺塔问题

1.一个例子带你快速了解Java中方法的使用
public class TestDemo {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        //循环输入
        while(sc.hasNextInt()){
            int year=sc.nextInt();
            isLeapYear(year);
        }
    }
    //自定义方法————判断闰年
    public static void isLeapYear(int year){
        if((year%4==0&&year%100!=0)||year%400==0){
            System.out.println("闰年");
        }
        else{
            System.out.println("平年");
        }
    }
}
2.形参和实参的关系

在Java中,实参永远都是拷贝到形参中,形参和实参本质是两个实体

public  static void swap(int x,int y){
        int tmp=x;
        x=y;
        y=tmp;
        System.out.println("x="+x+" , y="+y);
    }
    public static void main(String[] args) {
        int a=10;
        int b=20;
        swap(a,b);
        System.out.println("a="+a+" , b="+b);
    }

可以看到, 对 x 和 y 的修改, 不影响 a 和 b
注意:对于基础类型来说, 形参相当于实参的拷贝. 即 传值调用

解决方法:传引用类型参数 (例如数组来解决这个问题)

public static void swap1(int []arr){
        int tmp=arr[0];
        arr[0]=arr[1];
        arr[1]=tmp;
    }

    public static void main(String[] args) {
        int [] arr;
        arr=new int [2];
        arr[0]=10;
        arr[1]=20;
        System.out.println("交换前: arr[0]="+arr[0]+" arr[1]= "+arr[1]);
        swap1(arr);
        System.out.println("交换后: arr[0]="+arr[0]+" arr[1]= "+arr[1]);
    }

运行结果:

3.方法重载

重载规则:

    方法名必须相同参数列表必须不同(参数的个数不同、参数的类型不同、类型的次序必须不同)与返回值类型是否相同无关

例:

public static int add(int x,int y){
        return x+y;
    }
    public static double add(double x,double y){
        return x+y;
    }
    public static double add(double x,double y,double z){
        return x+y+z;
    }
    public static void main(String[] args) {
        int a=10;
        int b=20;
        double c=20.0;
        double d=30.0;
        double e=40.0;
        System.out.println(add(a,b));
        System.out.println(add(c,d));
        System.out.println(add(c,d,e));
    }

编译器在编译代码时,会对实参类型进行推演,根据推演的结果来确定调用哪个方法

4.方法的递归: 4.1求斐波那契数列某一项的值

//求斐波那契数列某一项的值
    public static int fib(int n){
        if(n==1||n==2){
            return 1;
        } else{
            return fib(n-1)+fib(n-2);
        }
    }

    public static void main(String[] args) {
        int n=0;
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt()){
            n=sc.nextInt();
            int ret=fib(n);
            System.out.println(ret);
        }
    }
4.2青蛙跳台阶问题
![在这里插入图片描述](https://img-blog.csdnimg.cn/641c18ee85794c69a47c76681c143b55.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5q-U54m5IGZseQ==,size_20,color_FFFFFF,t_70,g_se,x_16)
//青蛙跳台阶问题
    public static int footStep(int n){
        if(n==1){
            return 1;
        }else if(n==2){
            return 2;
        }else{
            return footStep(n-1)+footStep(n-2);
        }
    }
    public static void main(String[] args) {
        int n=0;//台阶个数
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt()){
            n=sc.nextInt();
            int ret=footStep(n);
            System.out.println(ret);
        }
    }
4.3汉诺塔问题

//汉诺塔问题
    public static int hanoi(int n){
        if(n==1){
            return 1;
        }else{
            return 2*hanoi(n-1)+1;
        }
    }
    public static void main(String[] args) {
        int n=0;
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt()){
            n=sc.nextInt();
            int ret=hanoi(n);
            System.out.println(ret);
        }
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/776303.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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