方法

方法类似于函数,一般由修饰符,返回值类型,方法名,参数类型(形式参数,实际参数)以及方法体构成

返回值:return。

public static void main(String[] args) {   //不需要返回的时候返回值为void,为空。
        //调用方法
        //实际参数 实际调用传递给它的参数
        double add = add(1, 2);
        System.out.println(add);

    }
    // 修饰符  +  返回值类型+  方法名+参数
    public static void main(String[] args) {

         //比较两个值大小
        double max = max(2.6, 2);
        System.out.println(max);

    }
    public static int max(int a,int b){
        int result =0;
        if (a==b){
            System.out.println("a==b");
            return 0;//return  表示终止方法以及返回结果
        }

        if (a>b){
            result =a;
        }else {
            result =b;
        }


        return result;
    }
    public static double max(double a,double b){
        //定义一个返回值  result
        double result =0;
        if (a==b){
            System.out.println("a==b");
            return 0;//return0  表示终止
        }

        if (a>b){
            result =a;
        }else {
            result =b;
        }

          // return 一般写在最后面
        return result;
    }
}

 


posted @ 2020-08-01 17:46  兔兔1234  阅读(97)  评论(0)    收藏  举报