方法

  •  

     

    方法是语句的集合,是执行一个功能,最好是只执行一个功能(原子性)(起名方式:ohmy)

  • 创建加法方法

package Ohh.Ehh;

public class Ahh {

   public static void main(String[] args) {
       int sum =add(1,2);
       System.out.println(sum);
      }
       //方法,加法
   public static  int add(int a,int b){
       return a+b;
  }
}

两种调用方法

  • 公共模块直接调用

package Ohh.Ehh;

public class Ahh {

   public static void main(String[] args) {
       text();
  }
   public static void text(){
       int sum=0;
       int sum1=0;
       for (int i=0;i<100;i++){
           if (i%2==0) {
               sum+=i;
          }else {
               sum1+=i;
          }
      }
       System.out.println(sum);
       System.out.println(sum1);
  }
}
  • 方法类似于函数,

  • 修饰符(public...) 返回值类型(void为空,无返回值) 方法名(参数类型 参数名){方法体}return 返回值;

  • 返回值为空调用语句,返回值为数调用数值

实参:实际输入的,形参:方法公式定义的

  • 比大小

package Ohh.Ehh;

public class Ahh {

   public static void main(String[] args) {
       int max = max (2 , 3);
       System.out.println(max);
  }
   public static int max(int num1,int num2) {
       int result = 0;
       if (num1 == num2) {
           System.out.println("相等");
           return 0;//终止方法,省流
      }
       if (num1 > num2) {
           result = num1;
      } else {
           result = num2;
      }
       return result;
  }
}
  • Java是值传递

  • 方法的重载:方法名相同,类型不同(参考同类方法,浮点数和整数)个数不同,或者参考排列顺序不同等

package Ohh.Ehh;

public class Ahh {

   public static void main(String[] args) {
       double max = max (2 , 3);
       System.out.println(max);
  }
   public static int max(int num1,int num2) {
       int result = 0;
       if (num1 == num2) {
           System.out.println("相等");
           return 0;//终止方法,省流
       if (num1 > num2) {
           result = num1;
      } else {
           result = num2;
      }
       return result;
  }
   public static double max ( double num1,double num2) {
       double result = 0.0;
       if (num1 == num2) {
           System.out.println("相等");
           return 0;//终止方法,省流
       if (num1 > num2) {
           result = num1;
      } else {
           result = num2;
      }
           return result;
  }
}
  • 命令行传参

package Ohh.Ehh;

public class Ahh {

   public static void main(String[] args) {
       //args.length   数组长度
       for (int i = 0; i < args.length; i++) {
           System.out.println("args["+i+"]"+args[i]);
      }



  }
}
在文件路径,使用cmd 先编译生成class文件,再回退路径至src,再编译并输入内容,输出a,b,c


  • 可变参数(不定项)

    • package Ohh.Ehh;

      public class Ahh {

         public static void main(String[] args) {
             //三种写法
             Ahh ahh =new Ahh();
             ahh.text(1,2,);
             //text(0,1,2,3,4);
             //text(new int[]{1,2,3,4,5});
        }

         public static void text(int... i) {
             System.out.println(i[1]);
             System.out.println(i[2]);
        }
      }
    • 递归(能不用就不用,来回运算很占运存)(自己调用自己):栈,三要素:边界条件(1),前阶段,推出text2-4,返回阶段,得出结果

    递归头:边界

    递归体:阶段路径

    阶乘:3!=3*2 *1=6

    package Ohh.Ehh;

    public class Ahh {

       public static void main(String[] args) {
           System.out.println(text(4));

      }

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

    逻辑:从结果(主方法)逆推到text4,text4须先知text3。。。。到text1以后有确切数值,再逆回来得出结果

    • 作业:计算器

    package Ohh.Ehh;
    import java.util.Scanner;
    public class Ahh {

           public static void main(String[] args) {
               for (; ; ) {

                   System.out.println("请选择计算方式:");
                   System.out.println("1.加");
                   System.out.println("2.减");
                   System.out.println("3.乘");
                   System.out.println("4.除");
                   Scanner c = new Scanner(System.in);

                   if (c.hasNextInt()) {
                       int e = c.nextInt();

                       if (e<=0 && e>=5 ){
                           System.out.println("方法输入错误");
                           continue;}
                           System.out.println("请输入两个数字");
                           Scanner a = new Scanner(System.in);
                           Scanner b = new Scanner(System.in);
                         if (e == 1) {

                               int sum1 = text1(a.nextInt(), b.nextInt());
                               System.out.println("=" + sum1);

                      } else if (e == 2) {

                               int sum2 = text2(a.nextInt(), b.nextInt());
                               System.out.println("=" + sum2);

                      } else if (e == 3) {

                               int sum3 = text3(a.nextInt(), b.nextInt());
                               System.out.println("=" + sum3);

                      } else if (e == 4) {

                               int sum4 = text4(a.nextInt(), b.nextInt());
                               System.out.println("=" + sum4);
                      }
                  } else {
                       System.out.println("方法输入错误");
                  }
              }
          }












       public static int text1(int a,int b) {
           return a+b;
      }
       public static int text2(int a,int b) {
           return a-b;
      }
       public static int text3(int a,int b) {
           return a*b;
      }
       public static int text4(int a,int b) {
           return a/b;
      }


    }

     

软玉染温香

2021-11-17

 
posted @ 2021-11-17 21:59  软玉染温香  阅读(46)  评论(0)    收藏  举报