【问题标题】:How could I receive the return value of one function into another function in javajava - 如何将一个函数的返回值接收到java中的另一个函数中
【发布时间】:2018-08-26 00:39:25
【问题描述】:
class Abc
{
    public static void main(String args[])
    {

    }

    public double[] set(double new_x, double new_y)
    {
        //body

        return new double[]{x,y};
    }

    public double set2(double g, double x, double y) //This x and y are the same x and y return by function set
    {
        //body

        return goal;
    }
}

我的程序是我想在set2 的参数(参数)中接收与set 函数返回的相同值。如您所见,set 函数返回一个包含两个变量 x 和 y 的数组。这个 x 和 y 值由set2 函数在其函数参数部分中接收。怎么可能?

【问题讨论】:

  • 此代码无法编译。 new_y 缺少类型
  • 流最能满足这种要求。

标签: java function class parameters return-value


【解决方案1】:

这可能对你有帮助:

public class Abc {

public static void main(String[] args) {

    double arrayOfDoubles[] = set(6,55);
    set2(2, arrayOfDoubles[0], arrayOfDoubles[1]);

}

public static double[] set(double new_x, double new_y) {
    //body
    double x = new_x;
    double y = new_y;
    //do what ever you want
    return new double[]{x,y};
}

public static double set2(double g, double x, double y) {
    //body
    double goal = g;
    //do what ever you want
    return goal;
}
}

程序运行方式:(一步一步)

在主函数中:

1.将 set() 函数的结果值分配给 arrayOfDoubles 变量。

2.通过传递参数g、arrayOfDoubles[0]、arrayOfDoubles[1]调用set2()函数。

【讨论】:

    【解决方案2】:

    试试这个

    double[] setResult = set(new_x, new_y);
    set2(g, setResult[0], setResult[1]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 2010-09-15
      • 2021-08-14
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      相关资源
      最近更新 更多