如何创建返回计算值的公共计算函数?

Ник*_*јић 0 java android function

我想创建公共函数,用于计算某些值.我希望该值是函数的输出,例如:

public void Calculation(...)
{
x = y+z/2 +i;
if(x >= 10)
{
calculation = 1;
}
else if(x < 10)
{
calculation = 0;
}
}
Run Code Online (Sandbox Code Playgroud)

而且,在以下其他地方使用它:

int final = Calculation(...);
Run Code Online (Sandbox Code Playgroud)

我的计算量要大得多,所以我不喜欢将它放在很多地方,我只是希望它放在一个地方,并返回值,因为我需要多次使用它.我该怎么做?谢谢你的建议.

Far*_*ıcı 5

创建一个名为Util的新类,并添加该方法;

public class Util {

    public static int Calculation(int x, int y, int z, int i) {
        int calculation=0;
        x = y + z / 2 + i;
        if (x >= 10) {
            calculation = 1;
        } else if (x < 10) {
            calculation = 0;
        }

        return calculation;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后从任何地方,你都可以这样称呼它;

int final_value = Util.calculation(1,2,3,4);
Run Code Online (Sandbox Code Playgroud)