Java学习笔记--方法的重载和递归

目录

 

方法的重载

方法的递归

1.用方法的递归计算1+2+3+4+……+10 

2.输入一个非负整数,返回组成它的数字之和

 3.递归打印数字的每一位

4. 递归求 N 的阶乘

5.递归求斐波那契数列的第 N 项

6.递归求解汉诺塔问题

 

 


方法的重载

Java允许在一个程序中定义许多个名称相同,但是参数的类型或者个数不同的方法,这就是方法的重载

代码示例

public class TestDemo{

    //找出两个整数中最大的数
    public static int maxTwo(int a,int b) {
        int max=0;
        if(a>b){
            return a;
        } else {
            return b;
        }
    }
    
    //找出两个小数中最大的数
    public static double maxTwo(double a,double b) {
        double max=0;
        if(a>b){
            return a;
        } else {
            return b;
        }
    }

    //找出三个数中最大的数
    public static double maxTwo(double a,double b,int c) {
        double max=0;
        if(maxTwo(a,b)>c){
            return maxTwo(a,b);
        } else {
            return c;
        }
    }

 public static void main(String[] args) {
        System.out.println(maxTwo(20,30));//调用的是两个整数中找出最大数的方法maxTwo(int a,int b);
        System.out.println(maxTwo(12.5,13.5));//调用的是两个浮点数中找出最大数的方法maxTwo(double a,double b);
        System.out.println(maxTwo(12.8,15.6,19));//调用的是三个数中找出最大数的方法maxTwo(double a,double b,int c);
    }
}

上述代码中定义了三个同名的maxTwo()方法,但是他们的参数个数或参数类型不同,从而实现了方法的重载。在main()方法中调用maxTwo()方法时,通过传入不同的参数便可以确定调用哪个重载的方法。

注意:方法的重载与返回值类型无关,它只需要满足两个条件:一是方法名相同,二是参数个数或是参数类型不同

方法的递归

方法的递归是指在一个方法的内部调用自身的过程。递归必须有约束条件,不然就会陷入无限递归地状态,永远无法结束调用。

1.用方法的递归计算1+2+3+4+……+10 

public class TestDemo {
    public static void main(String[] args) {
        System.out.println(add(10));
    }

    public static int add(int n) {
        if(n==1){
            return 1;
        }
      return   n+add(n-1);

    }
}

//执行结果
//55

2.输入一个非负整数,返回组成它的数字之和

  public class TestDemo{
    public static void main(String[] args) {
          System.out.println(sumAdd(123));
    }

    public static int sumAdd(int n) {
        if(n<10){
          return n;

        }
        return sumAdd(n/10)+n%10;

    }
}

 3.递归打印数字的每一位

public class TestDemo{
    public static void main(String[] args) {
        print(1314);
    }
    public static void print(int n) {
        if (n/10!=0){
            print(n/10);
        }
        System.out.print((n%10)+"  ");
    }
}

4. 递归求 N 的阶乘

public class TestDemo{

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

    public static int nFactor(int n) {
        if(n==1){
            return 1;
        }
        return n*nFactor(n-1);

    }

}

5.递归求斐波那契数列的第 N 项

public class TestDemo{

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

    public static int  fibOr(int n) {
        if(n==1){
            return 1;
        }
        if(n==2){
            return 2;
        }
        return fibOr(n-1)+fibOr(n-2);

    }

}

6.递归求解汉诺塔问题

public class TestDemo{

   
    public static void main(String[] args) {
        hanoiTower(1,'A','B','C');//执行结果A->C
        System.out.println();
        hanoiTower(2,'A','B','C');//执行结果A->B A->C B->C
        System.out.println();
        hanoiTower(3,'A','B','C');//执行结果A->C A->B C->B A->C B->A B->C A->C
        System.out.println();
    }
    public static void move(char pos1,char pos2) {
        System.out.print(pos1+"->"+pos2+" ");

    }

    public static void hanoiTower(int n,char pos1,char pos2,char pos3) {
        if(n==1){
            move(pos1,pos3);
            return;
        }
        hanoiTower(n-1,pos1,pos3,pos2);
        move(pos1,pos3);
        hanoiTower(n-1,pos2,pos1,pos3);


    }

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值