Java基础语法之方法调用、方法重载、数组1、获取索引

Java基础语法之方法调用、方法重载、数组1、获取索引

一、概念

1.1 概念

​ 方法:程序中最小的执行单元

注:方法必须先创建才可以使用,该过程成为方法定义。
方法创建后并不是直接可以运行的,需要手动使用后,才执行,该过程成为方法调用。

二、方法定义和调用

2.1 无参方法定义和调用

方法定义:

    public static void 方法名称(){
    
    
        方法体代码;
    }

注:方法必须先定义,后调用,否则程序将报错

无参数方法举例:求两数中的较大数
代码示例

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

    //定义方法getMax()
    public static void getMax() {
    
    
        int m = 6;
        int n = 9;
        if(m > n) {
    
    
            System.out.println(m);
        } else {
    
    
            System.out.println(n);
        }
    }

运行结果
在这里插入图片描述

2.2 带参方法定义和调用

(1)参数:由数据类型、变量名组成,如:int m

public static void 方法名 ([参数类型 形参 ...]) {
    
    
	方法体;
}

public static void 方法名 (参数1, 参数2, ...) {
    
    
	方法体;
}

注:方法定义时,参数中的数据类型与变量名都不能缺少,缺少任意一个程序将报错。
方法定义时,多个参数之间使用逗号( ,)分隔。

调用举例

扫描二维码关注公众号,回复: 17069331 查看本文章
  isNumber(10);
  getMax(6,9);
  • 方法调用时,参数的数量与类型必须与方法定义中的设置相匹配,否则程序将报错。

(2)形参和实参

  • 形参:方法定义中的参数,等同于变量定义格式,例如:int n
  • 实参:方法调用中的参数,等同于使用变量或常量,例如: 6 n

带参数方法举例:求两数中的较大数
代码示例

public static void main(String[] args) {
    
    
        //在main()方法中调用定义好的方法(使用变量)
        int a = 9;
        int b = 6;
        getMax(a, b);
    }

    public static void getMax(int a, int b) {
    
     //为方法定义两个参数,用于接收两个数字
        if(a > b) {
    
    
            System.out.println("a="+a);
        } else {
    
    
            System.out.println("b="+b);
        }
    }

运行结果
在这里插入图片描述

2.3. 带返回值方法的定义和调用

(1)方法定义

  public static 数据类型 方法名称 ( 参数 ) {
    
    
      return 数据 ;
  }

示例:

  public static int getMax( int m, int n ) {
    
    
      return  10 ;
  }

注:方法定义时,return后的返回值与方法定义上的数据类型要匹配,否则程序将报错。

调用格式

方法名 ( 参数 ) ;
数据类型 变量名 = 方法名 ( 参数 ) ;

示例

isEvenNumber ( 8 ) ;
boolean  flag =  isEvenNumber ( 8); 

注:方法的返回值通常会使用变量接收,否则该返回值将无意义。

(2) 举例:同上

代码示例

public static void main(String[] args) {
    
    
          //在main()方法中调用定义好的方法并直接打印结果
          System.out.println(getMax(10,20));

          //在main()方法中调用定义好的方法并使用变量保存
          int res = getMax(23,32);
          System.out.println("res="+res);
      }

      public static int getMax(int a, int b) {
    
    
          if(a > b) {
    
    
              return a;
          } else {
    
    
              return b;
          }
      }

运行结果
在这里插入图片描述
(3)举例:比较两个圆的面积

代码示例

public static void main(String[] args) {
    
    
        Scanner s = new Scanner(System.in);
        System.out.print("请输入第一个圆的半径:");
        int r1 = s.nextInt();
        System.out.print("请输入第二个圆的半径:");
        int r2 = s.nextInt();

        double area1 = getArea(r1);
        double area2 = getArea(r2);
        if(area1 > area2){
    
    
            System.out.println("第一个圆面积大");
        }else{
    
    
            System.out.println("第二个圆面积大");
        }
    }

    public static double getArea(int r) {
    
    
        double area = 3.14 * r * r;
        return area;
    }

运行结果
在这里插入图片描述

三、 方法的注意事项

3.1 方法不能嵌套定义

(1)代码示例:

    public static void main(String[] args) {
    
    
    }

    public static void add() {
    
    
        public static void add() {
    
    
            // 编译错误
        }
    }

注:void表示无返回值,可以省略return,也可以单独的书写return,后面不加数据

(2) * 代码示例:

    public static void main(String[] args) {
    
    

    }
    public static void add() {
    
    
        //return 10; 编译错误,因为没有具体返回值类型
        return;
        //System.out.println(10); return语句后面不能跟数据或代码
    }

四、 方法重载

(1)方法重载:同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载

  • 多个方法在同一个类中
  • 多个方法具有相同的方法名
  • 多个方法的参数不相同,类型不同或者数量不同

代码示例

    public class MethodDemo {
    
    
        public static void ad(int a) {
    
    
            //方法体
        }
        public static int ad(double a) {
    
    
            //方法体
        }
    }

    public class MethodDemo {
    
    
        public static float ad(int a) {
    
    
            //方法体
        }
        public static int ad(int a , int b) {
    
    
            //方法体
        }
    }

(2)方法重载举例:设计比较两个整数是否相同的方法,例如整数类型
代码示例

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

    public static boolean compare(int m, int n) {
    
    
        return m == n;
    }

运行结果
在这里插入图片描述

五、数组

(1)数组遍历
举例:计一个方法用于数组遍历,要求遍历的结果是在一行上的。例如:[11, 22, 33, 44, 55]
代码示例

    public static void main(String[] args) {
    
    
        int[] arr = {
    
    1,4,5,3,6};
        pArr(arr);
    }

    public static void pArr(int[] a){
    
    
        System.out.print("[");
        for (int i = 0; i < a.length; i++) {
    
    
            if(i == a.length - 1){
    
    
                System.out.println(a[i] + "]");
            }else{
    
    
                System.out.print(a[i] + ", ");
            }
        }
    }

(2) 数组最大值
举例:求数组中元素的最大值
代码示例

public static void main(String[] args) {
    
    
        int[] a = {
    
    1,4,5,3,6};
        int maxnumber = getMax(a);
        System.out.println("maxnumber=" + maxnumber);
    }

    public static int getMax(int[] arr) {
    
    
        int max = arr[0];
        for(int a=1; a<arr.length; a++) {
    
    
            if(arr[a] > max) {
    
    
                max = arr[a];
            }
        }
        return max;
    }

运行结果
在这里插入图片描述

六、获取索引

举例:定义一个方法获取数字,在数组中的索引位置,将结果返回给调用处,如果有重复的,只要获取第一个即可。
代码示例

public class Main {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    2,4,3,9,5};
        int index = cons(arr, 9);
        System.out.println(index);
    }

    public static int cons(int[] a, int n) {
    
    
        //遍历arr得到每一个元素
        for (int i = 0; i < a.length; i++) {
    
    
            if(a[i] == n){
    
    
                return i;
            }
        }
        return -1;
    }

运行结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/lolly114/article/details/130279004
今日推荐