方法案例(较大数、带参数、带返回值、方法重载、数组遍历、)需求、分析、代码

案例:求较大数
需求:
设计一个方法用于打印两个数字中的较大数
思路:
(1)定义一个方法,用于打印两个数中的较大数,如getMax()
(2)方法中定义两个变量,用于保存两个数字
(3)使用分支语句分两种情况对两个数字的大小关系进行处理
(4)在main()方法中调用定义好的方法
完整代码:

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

    }

    public static void getMax() {
        int a = 10;
        int b = 11;

        if(a > b){
            System.out.println(a);
        }else{
            System.out.println(b);
        }
    }
}

运行结果:

11

带参数方法练习
需求:
设计一个方法用于打印两个数中的较大值,数据来自于方法参数
思路:
(1)定义一个方法,用于打印两个数字中的较大数,如getMax()
(2)为方法定义两个参数,用于接收两个数字
(3)使用分支语句分两种情况对两个数字的大小关系进行处理
(4)在main()方法中调用定义好的方法(使用常量)
(5)在main()方法中调用定义好的方法(使用变量)
完整代码:

public class MethodDemo {
    public static void main(String[] args) {
        //常量
        getMax(10,20);

        //变量
        int a = 10;
        int b = 20;
        getMax(a,b);
    }

    public static void getMax(int a, int b){
        if(a > b){
            System.out.println(a);
        }else{
            System.out.println(b);
        }
    }
}

运行结果:

20
20

带返回值方法练习
需求:
设计一个方法可以获取两个数的较大值,数据来自于参数
思路:
(1)定义一个方法,用于获取两个数字中的较大数
(2)使用分支语句分两种情况对两个数字的大小关系进行处理
(3)根据题设分别设置两种情况下对应的返回结果
(4)在main方法中调用定义好的方法并使用变量保存
(5)在main方法中调用定义好的方法并直接打印结果
完整代码:

public class MethodDemo {
    public static void main(String[] args) {
        int max = getMax(10,20);
        System.out.println(max);

        //直接打印
        System.out.println(getMax(10,20));

    }

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

运行结果:

20
20

方法重载练习
需求:
使用方法重载的思想,设计比较两个整数是否相同的方法,兼容全整数类型(byte,short,int,long)
分析:
(1)定义比较两个数字是否相同的方法compare()方法,参数选择两个int型参数
(2)定义对应的重载方法,变更对应的参数类型,参数变更为两个long型参数
(3)定义所有的重载方法,两个byte类型和两个short类型参数
(4)完成方法的调用,测试运行结果
完整代码:

public class MethodDemo {
    public static void main(String[] args) {
        System.out.println(compare(10,20));

        System.out.println(compare((byte)10,(byte)20));

        System.out.println(compare((short)10,(short)20));

        System.out.println(compare(10L,20L));
    }

    public static boolean compare(int a, int b){
        System.out.println("int");
        return a == b;
    }

    public static boolean compare(long a, long b){
        System.out.println("long");
        return a == b;
    }

    public static boolean compare(byte a, byte b){
        System.out.println("byte");
        return a == b;
    }

    public static boolean compare(short a, short b){
        System.out.println("short");
        return a == b;
    }


}

运行结果:

int
false
byte
false
short
false
long
false

Process finished with exit code 0

案例:数组遍历
需求:
设计一个方法用于数组遍历,要求遍历的结果是在一行上的,例如[11,22,33,44,55]
分析:
(1)因为要求结果在一行上输出,用System.out.print(“内容”);
(2)定义一个数组,用静态初始化完成数组元素初始化
(3)定义一个方法,用数组遍历格式对数组进行遍历
(4)用新的输出语句修改遍历操作
(5)调用遍历方法
完整代码:

public class MethodDemo {
    public static void main(String[] args) {
        int[] arr = {11,22,33,44,55};
        printArray(arr);

    }

    public static void printArray(int[] arr) {
        System.out.print("[");
        for(int x=0; x<arr.length; x++){
            if(x == arr.length-1){
                System.out.print(arr[x]);
            }else{
                System.out.print(arr[x]+",");
            }

        }
        System.out.println("]");
    }

}

运行结果:

[11,22,33,44,55]

案例:数组最大值
需求:
设计一个方法用于获取数组中元素的最大值,调用方法并输出结果
分析:
(1)定义一个数组,用静态初始化完成数组元素初始化
(2)定义一个方法,用来获取数组中的最大值
(3)调用获取最大值方法,用变量接收返回结果
(4)把结果输出在控制台
完整代码:

public class MethodDemo {
    public static void main(String[] args) {
        int[] arr = {11,22,33,45,65,14};

        int number = getMax(arr);
        System.out.println(number);
    }

    //明确:返回值类型(int);参数(int[] arr)
    public static int getMax(int[] arr) {
        int max = arr[0];

        for(int x=1; x<arr.length; x++) {
            if(arr[x] > max){
                max = arr[x];
            }
        }

        return max;
    }

}

运行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值