我如何在java中将另一个函数的返回值接收到另一个函数中

时间:2018-03-16 20:51:42

标签: java function class parameters return-value

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的{​​{1}}函数返回的set参数中获得相同的值。如您所见,set函数返回一个由两个变量x和y组成的数组。此x和y值由set2函数在其函数参数部分中接收。怎么可能?

2 个答案:

答案 0 :(得分:0)

试试这个

func printResults(forIssue: String, withVotes: Bool) -> String {
    positive = 0
    negative = 0
    for votes in withVotes {
        if votes == true {
            positive += 1
        } else {
            negative += 1
        }
    }
    return "\(forIssue) \(positive) yes, \(negative) no"
}

答案 1 :(得分:0)

这可能对您有所帮助:

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()函数。