Java 如何将数据返回到函数?

Java 如何将数据返回到函数?,java,Java,假设我有以下代码: MyFunction1(){ DoSomething (Value1); } MyFunction2(){ DoSomething(Value2); } DoSomething(int Amount) { int i = Amount; if (i > 1) { i=0; } if (i>2) { i=1; } if (i>=3){ i = 2; } etc ... // how do I say i = Value; if I want to re

假设我有以下代码:

MyFunction1(){
DoSomething (Value1);
}
MyFunction2(){
DoSomething(Value2);
}

DoSomething(int Amount) {
int i = Amount;

if (i > 1) {
i=0;
}

if (i>2) {
i=1;
}

if (i>=3){
i = 2;
}
etc ...
// how do I say i = Value; if I want to reuse this function to send Value1, Value2, Value3??
}

如何重用to function将其发送回调用函数?

我相信您是在问如何在Java中从函数返回值。如果我不正确,请忽略此响应。声明函数
DoSomething
时,还需要声明函数返回值的类型。在这种情况下,您可能会使用
int
,因此函数看起来更像:

int DoSomething(int Amount){
    int i = Amount;

    if (i=2){
        i = 1;
    }

    return i;
}
要从这个函数中接收一个值,需要将它赋给一个变量。因此,请使用您的示例:

MyFunction1(){
    int answer;
    int Value2 = 2;

    answer = DoSomething(Value2);
}

如果你能提供一个更具体的例子来说明你正在尝试做什么,也许我可以提供更多的帮助,但希望这能让你开始。

我不确定你想要实现什么。你能更详细地描述一下吗?另外,
if(i>2)
if(i>3)
将永远不会执行,因为它将由
if(i>1)
处理。关键是我如何将不同的值传递到同一个函数中,该函数随后会更改并返回相同的值作为方法参数传递值有什么问题<代码>myMethod(int myArgument){if(myArgument==1)doAction1();}if(myArgument==2)doAction2();}然后您可以将此方法与
intx=2一起使用;myMethod(x)
谢谢@Pshemo的输入,这可能非常有用,从accepted answer method的参数来看,这正是您要找的:)很高兴我能帮上忙。谢谢,这正是我要找的。很抱歉不够清晰,我在手机上。。。但这正是我一直想知道的问题