本文介绍了将值从方法返回到另一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以告诉我为什么返回的值是3而不是8. 返回x
语句来自 addFive
方法在 main
方法中更改 x
的值?
Can somebody tell me why the value returned is 3 and not 8. Doesn't the return x
statement from the addFive
method change the value of x
in the main
method?
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;
}
}
推荐答案
你想要 x = addFive(x);
而不仅仅是 addFive(x)
。单独调用 addFive(x)
不会将返回值应用于任何变量。
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.
这篇关于将值从方法返回到另一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!