java返回参数中几种常见的方法

1.有参数有返回值


public class text_1 {
    1)创建add方法

    public int add(int i, int j) {
        int res = i + j;
        return res;
    }

 2)方法的调用

    public static void main(String[] args) {
        text_1 a = new text_1();// 创建方法对象
        int k = a.add(3, 4);// 用对象调用方法体
        System.out.println(k);||System.out,println(a.add(3,4));

  //但是a.add(3,4)在有返回值return中是错误的。

}


 

 

2.有参数无返回值


 

1)创建方法avg

  public void avg(int i, int j) {
        double avg = (i + j) / 2.0;
        System.out.println("平均分" + avg);
    }2)avg方法的调用

public static void main(String[] args) {

   text_1 a = new text_1();// 创建方法对象

   a.avg(4, 6);

}

注意:①参数:结合1和2可知:有参数的方法在调用的时候需要给变量赋值传递给形参;

   ②返回值:有返回值是在方法中用return在返回,利用System.out.println(a.avg(4,6))来输出;没有返回值时(void)在方法中System来结束,调用时用   a.avg(4, 6)直接来输出。


 

3.无参数有返回值


 

1)方法的创建

 public double te() {
        double a = 1.2;
        double b = 1.8;
        return a + b;
    }

2)调用方法te();

public static void main(String[] args) {

     text_1 a = new text_1();// 创建方法对象  

      double add = a.te();
      System.out.println(add);

}


4.无参数无返回值


1)创建方法

 public void text() {
        System.out.println("今天起大雾");
    }

2)调用text()方法

public static void main(String[] args) {

   text_1 a = new text_1();// 创建方法对象

   a.text();

}


 

5.上面用到的方法

package day_01;
//创建加法方法
public class text_1 {
    // 有参数返回值的方法
    public int add(int i, int j) {
        int res = i + j;
        return res;
    }

    // 无返回值无参数的方法
    public void text() {
        System.out.println("今天起大雾");
    }

    // 无参数有返回值的的方法
    public double te() {
        double a = 1.2;
        double b = 1.8;
        return a + b;
    }

    // 有参数无返回值的方法
    public void wu(int i, int j) {
        double avg = (i + j) / 2.0;
        System.out.println("平均分" + avg);
    }

    public static void main(String[] args) {
        text_1 a = new text_1();// 创建方法对象
        int k = a.add(3, 4);// 用对象调用方法体
        // System.out.println(k);
        // a.text();
        double add = a.te();
        // System.out.println(add);
        a.wu(4, 6);

    }
}

转载于:https://www.cnblogs.com/helei747123/p/6265388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值