JAVA:方法的使用 笔记

方法的使用

1.方法的重载:
* 方法名相同,参数列表不同(1.个数不同,2.类型不同,3.顺序不同),返回值不作要求
add(_double,_double,_int)
add(_double,_int,_double)
2.递归
* 将大问题化解为小问题,处理方式一样
* 1.要调用自己本身
* 2.要有趋向于终止的条件
* 方法的调用需要在栈上开辟内存
*先从栈底开辟空间,然后向上 栈底是高地址,堆相反

练习: //1.按顺序打印一个数字的每一位1234 打印出 1 2 3 4(递归)
//原本1234%10=4 1234%10=3…
//2.递归求 1 + 2 + 3 + … + 10
//3.写一个递归方法,输入一个非负整数,返回组成它的数字之和. 例如,输入 1729, 则应该返回1+7+2+9,

package NewJavaClass.Test2007;
import java.util.Scanner;
//IDEA自动添加类注释
/**
 * @ Author     :CYD
 * @ Date       :Created in 8:02 2020/7/21
 * @ Description:
 * @ Modified By:
 * @Version: $
 */

/*
homework:
* 1.有一组数据,只有一个数字是出现一次,其他是两次,请找出这个数字。
* 2.求斐波那契数列的第n项。(迭代实现)
* 3.调整数组顺序使得奇数位于偶数之前。调整之后,不关心大小顺序。
* 4. 创建方法求两个数的最大值max2,随后再写一个求3个数的最大值的函数max3。
要求:在max3这个函数中,调用max2函数,来实现3个数的最大值计算
* 5.求N的阶乘
* 6.求1!+2!+3!+4!+........+n!的和

=====================================================================================


方法的使用:

1.方法的重载:
     * 方法名相同,参数列表不同(1.个数不同,2.类型不同,3.顺序不同),返回值不作要求
     add(_double,_double,_int)
     add(_double,_int,_double)
2.递归
    * 将大问题化解为小问题,处理方式一样
    * 1.要调用自己本身
    * 2.要有趋向于终止的条件
    * 方法的调用需要在栈上开辟内存
    *先从栈底开辟空间,然后向上  栈底是高地址,堆相反

练习: //1.按顺序打印一个数字的每一位1234 打印出 1 2 3 4(递归)
      //原本1234%10=4  1234%10=3......
      //2.递归求 1 + 2 + 3 + ... + 10
      //3.写一个递归方法,输入一个非负整数,返回组成它的数字之和. 例如,输入 1729, 则应该返回1+7+2+9,




* */
public class Test0721 {

    //1.有一组数据,只有一个数字是出现一次,其他是两次,请找出这个数字。
       /* public static void main8(String[] args) {
            int []arr={1,1,2,2,4,3,4,5,5};
            int i=0;
            int j=0;
            int count;
            for(i=0;i<9;i++){
                count =0;
                for(j=0;j<9;j++){
                    if(arr[i]==arr[j])
                        count ++;

                }if(count==1){
                    System.out.println(arr[i]);
                }
            }
        }

*/

    //2.求斐波那契数列的第n项。(迭代实现)

    //1  1  2  3 5 8 13 21
    //f1 f2 f3
    //   f1 f2 f3
        /*public static int fbnq(int n){
            if(n<=0){
                return -1;//标识当前n不合法
            }
            if(n==1||n==2){
                return 1;
            }
            int f1=1;
            int f2=1;
            int f3=0;

            for(int i=3;i<=n;i++){
                f3=f1+f2;
                f1=f2;
                f2=f3;
            }
            return f3;
        }

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

    }*/

    //3.调整数组顺序使得奇数位于偶数之前。调整之后,不关心大小顺序。
  /*  public static void swapNum(int[] array) {

        int i = 0;
        int j = array.length - 1;

        while (i < j) {
            while (i < j && array[j] % 2 == 0) {
                j--;
            }
        }

        while (i < j && array[i] % 2 != 0) {
            i++;
        }

        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }


    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        swapNum(array);
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

*/
    //4.  创建方法求两个数的最大值max2,随后再写一个求3个数的最大值的函数max3。

    // ​ 要求:在max3这个函数中,调用max2函数,来实现3个数的最大值计算

   /* public static int max2(int a, int b) {
        return a > b ? a : b;
    }

    public static int max3(int a, int b, int c) {
        int maxTmp = max2(a, b);
        int max = max2(c, maxTmp);
        return max;
    }

    public static void main(String[] args) {
        System.out.println(max3(20, 19, 34));

    }*/


   //4.求N的阶乘
   /*public static int fauc(int a){

       if(a==1){
           return 1;
       }
       return a*fauc(a-1);
   }
    public static void main(String[] args) {
       Scanner scan=new Scanner(System.in);
        System.out.println("请输入N");
       int a=scan.nextInt();
       System.out.println(fauc(a));
    }
*/

  // 6.求1!+2!+3!+4!+........+n!的和
  /*public static int facadd(int n) {
      int sum=0;
      for(int i=1;i<=n;i++) {
          int ret=1;
          for(int j=1;j<=i;j++) {
              ret*=j;
          }
          sum+=ret;
      }
      return sum;
  }

      public static void main(String[] args) {
          Scanner scan = new Scanner(System.in);
          int n = scan.nextInt();
          int add=facadd(n);
          System.out.println(add);
      }
*/

//=====================================================================================



    /*
     * 1.方法的重载:
     * 方法名相同,参数列表不同(1.个数不同,2.类型不同,3.顺序不同),返回值不作要求
     *
     *
     * */
    public static int add(int a,int b){
        return a+b;
    }

/*public static double add(double c,double d){
        return c+d;
}*/

    public static void main1(String[] args) {
        int a=10;
        int b=20;
        System.out.println(add(a,b));

        //若为小数
      /*  double c=12,5;
        double d=13,6;
        System.out.println(add(a,b));*/
    }


    /*
    *2.递归
    * 将大问题化解为小问题,处理方式一样
    * 1.要调用自己本身
    * 2.要有趋向于终止的条件
    * 方法的调用需要在栈上开辟内存
    * */
public static int fac(int n){//5 4 3 2 1
    //5的阶乘
    if(n==1){
        return 1;
    }
    return n*fac(n-1);
}
    public static void main2(String[] args) {

    System.out.println(fac(5));
    }


//1.按顺序打印一个数字的每一位1234 打印出 1 2 3 4(递归)
    //原本1234%10=4  1234%10=3......
public static void func(int n){
        if (n > 9) {//两位数以上
            func(n/10);
        }
        System.out.println(n%10+" ");

    }

public static void main4(String[] args) {
    func(1234);

}

//2.递归求 1 + 2 + 3 + ... + 10

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

}

    public static void main5(String[] args) {
        System.out.println(sum(10));
    }


//3.写一个递归方法,输入一个非负整数,返回组成它的数字之和. 例如,输入 1729, 则应该返回1+7+2+9,
//它的和是19

public static int fuc(int n){
    if(n<10){
        return n;
    }else{
        return n%10+fuc(n/10);
    }
}

    public static void main6(String[] args) {
        System.out.println(fuc(1729));
    }

}























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值