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

编程入门 行业动态 更新时间:2024-10-10 03:32:56
本文介绍了将值从方法返回到另一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人可以告诉我为什么返回的值是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.

更多推荐

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

本文发布于:2023-11-05 02:46:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559697.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!