方法的定义调用和重载

/*
1.Java方法是语句的集合,它们在一起执行一个功能。
2.方法是解决-类问题的步骤的有序组合
3.方法包含于类或对象中
4.方法在程序中被创建,在其他地方被引用
最好保持方法的原子性,就是一个方法只完成1个功能,这样利于我们后期的扩展。
设计方法的原则:方法的本意是功能块,我们设计方法的时候,就是实现某个功能的语句块的集合。 
 */

方法

//方法:类似于其他语言的函数
public class Demo01 {
    //main方法
    public static void main(String[] args) {

        //实际参数 a:1,b:2:实际调用传递给它的参数
        int sum = add(1, 2);//add(); 快捷键:Alt+回车
        System.out.println(sum);

        System.out.println("####################");

        test();

    }
    //加法
    //修饰符:public static  返回值类型:int  方法:add()
    //形式参数int a,int b:用于定义作用的
    public static int add(int a ,int b){
        return a+b;
    }
    public static void test(){
        for (int i = 0; i <= 1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){
                System.out.println();
                //System.out.print("\n"); 换行
            }
        }
    }

方法的定义与调用

public class Demo02 {
    public static void main(String[] args) {

        int max = max(10,20);
        System.out.println(max);
        int max1=max1(20,29);
        System.out.println(max1);
    }


    //比较大小
    public static int max(int num1, int num2){
        int result ;

        if (num1==num2){
            System.out.println("num1==num2");
            return 0;//终止方法
        }

        if (num1>num2){
           result = num1;
        }else{
            result = num2;
        }
        return result;

    }

    //快捷方法比较大小
    public static int max1(int num3,int num4){
        int a;
        a=num3>num4 ? num3:num4;

        return a;
    }
}

方法的重载

/*
◆重载就是在一个类中,有相同的函数名称,但形参不同的函数。
◆方法的重载的规则:
◆方法名称必须相同。
◆参数列表必须不同(个数不同、或类型不同、参数排列顺序不同等)。
◆方法的返回类型可以相同也可以不相同。
◆仅仅返回类型不同不足以成为方法的重载。
◆实现理论:
◆方法名称相同时,编译器会根据调用方法的参数个数、参数类型等去逐个匹配,以选择对
应的方法,如果匹配失败,则编译器报错。
 */
//方法重载
public class Demo03 {
    public static void main(String[] args) {
        int max = max(10,20);
        //double max = max(10,20);
        System.out.println(max);
    }

    //比较大小
    public static int max(int num1, int num2){
        int result =0;

        if (num1==num2){
            System.out.println("num1==num2");
            return 0;//终止方法
        }

        if (num1>num2){
            result = num1;
        }else{
            result = num2;
        }
        return result;

    }

    //比较大小
    public static double max(double num1, double num2){
        double result =0;

        if (num1==num2){
            System.out.println("num1==num2");
            return 0;//终止方法
        }

        if (num1>num2){
            result = num1;
        }else{
            result = num2;
        }
        return result;

    }
}

命令行传参

//命令行传参
public class Demo04 {
    public static void main(String[] args) {
        //args.Length 数组长度
        for (int i = 0; i < args.length; i++) {
            System.out.println("args["+i+"]:"+args[i]);
        }
        //cmd 中操作
        /*
        javac Demo04.java  编译生成class文件
        先回退到src目录 通过cd ../操作
        java com.feng.method.Demo04
         */
    }
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.feng.method;

public class Demo04 {
    public Demo04() {
    }

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

    }
}

可变参数

/*
    可变参数:
    在方法声明中,在指定参数类型后加一个省略号(...)
    一个方法中只能指定一个可变参数,它必须是方法的最后一个参数。任何普通的参数必须在它之前声明。.
     */
}
//可变参数(不定项参数)
public class Demo05 {
    public static void main(String[] args) {

        Demo05 demo05 = new Demo05();
        demo05.test(1,2,3,4,5);
    }

    public void test(int ... i){
        System.out.println(i[4]);

    }
}

递归讲解

// 递归算法 阶乘函数 斐波那切数列 汉诺塔问题
//递归算法要素:
// 1.递归边界:确定递归何时结束
// 2.递归体:确定递归求解是的递推关系
public class Demo06 {

    //
    public static void main(String[] args) {
        //5!=5*4*3*2*1
        System.out.println(f(4));

    }
    public static int f(int n){

        if (n==1){
            return 1;
        }else{
            return n*f(n-1);
        }
    }
}
// 递归算法 汉诺塔算法
public class Demo07 {

    public static void main(String[] args) {
        System.out.println("汉诺塔的移动步骤:");
        hanoi(3,'a','b','c');
    }

    private static void move(char a, char c) {

        System.out.println("move:"+a+"--->"+c);
    }

    public static void hanoi(int n, char a, char b, char c){
        if (n==1) {
            move(a, c);
        }
        else{
            hanoi(n-1,a,c,b);
            move(a,c);
            hanoi(n-1,b,a,c);
        }

    }

}

简易计算器(实现加减乘除)

import java.util.Scanner;

//使用switch实现简易的计算器(实现加减乘除);
public class Demo08 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入:");
        int sum = 0;
        int a = scanner.nextInt();
        char c= scanner.next().charAt(0);
        int b = scanner.nextInt();

        switch(c) {
            case '+':
                jia(a, b);
                break;
            case '-':
                jian(a,b);
                break;
            case '*':
                cheng(a,b);
                break;
            case '/':
                chu(a,b);
                break;
            default:
                System.out.println("输入错误");
                break;
        }
        scanner.close();
    }
    public static void jia(int a, int b){
        int sum=a+b;
        System.out.println("结果为:"+sum);
    }
    public static void jian(int a, int b){
        int sum=a-b;
        System.out.println("结果为:"+sum);
    }
    public static void cheng(int a, int b){
        int sum=a*b;
        System.out.println("结果为:"+sum);
    }

    public static void chu(int a, int b){
        if (b==0){
            System.out.println("除数不能为0!");
        }else{
            int sum=a/b;
            System.out.println("结果为:"+sum);
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

聲聲是仙女

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值