Java 从一个方法返回一个值到另一个方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29154983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:27:37  来源:igfitidea点击:

returning a value from a method to another method

java

提问by flutter

Can somebody tell me why the value returned is 3 and not 8. Doesn't the return xstatement from the addFivemethod change the value of xin the mainmethod?

谁能告诉我,为什么返回的值是3个,而不是8.不将return x语句从addFive方法变化的值xmain方法?

public class App {
    public static void main(String[] args) {
        int x=3;
        addFive(x);
        System.out.println("x = " + x);
    }

    private static int addFive(int x) {
        x += 5;
        return x;
    }
}

回答by slnowak

Because you're simply not using the computation of your function. It doesn't change value of x, it returns new value.

因为您根本没有使用函数的计算。它不会改变 x 的值,而是返回新值。

You should do something like:

你应该做这样的事情:

int y = addFive(x);

回答by Sidmeister

You are calling the method addFive(int x)with x, but not assigning the returned value to anything. So, inside main()'s scope xremains as before, 3 - which is what is being printed. So, you can either store the returned value to xitself:

您所呼叫的方法addFive(int x)x,但不是指派返回值到任何东西。所以, insidemain()的范围x仍然像以前一样, 3 - 这是正在打印的内容。因此,您可以将返回的值存储到x自身:

x = addFive(x);

or make the function call within print statement:

或在打印语句中进行函数调用:

System.out.println("x = " + addFive(x));

回答by Evan Bechtol

You have to set the returned value to a variable, otherwise it is lost and you are retrieving the value of "x" in your main method. Do this instead to capture the return value.

您必须将返回值设置为变量,否则它会丢失并且您将在主方法中检索“x”的值。这样做是为了捕获返回值。

   public static void main(String[] args) {
       int x=3;
       x = addFive(x);
       System.out.println("x = " + x);
   }

If you only want to see the returned value and not store it, you can even put the function call inside the System.out.println.

如果您只想查看返回值而不存储它,您甚至可以将函数调用放在System.out.println.

   public static void main(String[] args) {
       int x=3;
       System.out.println("x = " + addFive(x));

   }

回答by Jaken Herman

You want x=addFive(x);rather than just addFive(x). Calling addFive(x)on it's own does not apply the returned value to any variable.

你想要的x=addFive(x);不仅仅是addFive(x). 调用addFive(x)它自己不会将返回值应用于任何变量。

回答by adamclmns

Like everyone else is saying, you need to assign your return value. Because you're doing "addFive(x)" instead of "x=addFive(x);" you're just printing the instance of "x" in main, and not ever getting the value that your function returns.

就像其他人所说的那样,您需要分配返回值。因为你在做“addFive(x)”而不是“x=addFive(x);” 您只是在 main 中打印“x”的实例,而从未获得您的函数返回的值。

This is because "x" in your main function is an instance variable, and your "x" in addFive() is a local variable. These are not the same variable, even if they have the same name. This might clarify a bit - http://www.tutorialspoint.com/java/java_variable_types.htm

这是因为主函数中的“x”是一个实例变量,而 addFive() 中的“x”是一个局部变量。即使它们具有相同的名称,它们也不是相同的变量。这可能会澄清一点 - http://www.tutorialspoint.com/java/java_variable_types.htm

回答by Mehmood Arbaz

The value in the main function is completely different from the value in the addFive(int x )function. You send an xfrom main to addFive(int x)method.

main 函数中的值与addFive(int x )函数中的值完全不同。您x从 main发送到addFive(int x)方法。

JVM makes a copy of x and send it to addFive(int x)method. Then xchanges in the addFive(int x)method. But the xin main()method remains unchanged.

JVM 复制 x 并将其发送到addFive(int x)方法。然后x改变addFive(int x)方法。但是xinmain()方法保持不变。

If you want to get the changed value returned by addFive(int x)from main method you can do the following -

如果您想获取addFive(int x)main 方法返回的更改值,您可以执行以下操作 -

int returnedValueFromAddFive = addFive(x)  

Hope it will help.
Thanks a lot.

希望它会有所帮助。
非常感谢。

回答by Rafael Reis

when you call a method in java and you don't assign for any variable, the changes will happen in the method call, after that this value will be lost and go back to the value that you assign. to see the result you should do that System.out.println(addFive(x));but if you want to change the value of xyou have to assign x = addFive(x);

当你在java中调用一个方法并且你没有为任何变量赋值时,方法调用中会发生变化,之后这个值将丢失并回到你分配的值。要查看结果,您应该这样做, System.out.println(addFive(x));但是如果您想更改值,x则必须分配x = addFive(x);

回答by Ski

The method does return a value, but you have to set the value to a variable too when you return it, or else how would it know which variable you want to return the value to? You can have 10 variable, and if you just call the method, how will it know which variable to return the number to? That's why you have to set the returning number it to a variable like this:

该方法确实返回了一个值,但是您在返回它时也必须将该值设置为一个变量,否则它如何知道要将值返回给哪个变量?您可以有 10 个变量,如果您只是调用该方法,它如何知道将数字返回给哪个变量?这就是为什么您必须将返回数字设置为这样的变量:

x = addFive(x);