如何通过“引用参数”传递参数? Java 上的 int 和 float
我知道不存在这样的事情,但我知道你可以使用包装类(?)做类似的事情。我也无法使用 return 语句返回数组或任何内容。 (这是为了大学实践)。
这是我的代码:
//This is the main:
calculadoraMax.devolverParametros(v, min, max, promedio);
System.out.println("min: " + min + " max: " + max + " promedio: " + promedio);
public static void devolverParametros(int[] v, Integer min, Integer max, Double promedio) {
for (int i = 0; i < v.length; i++) {
if(v[i] > max) {
max = v[i];
}
if(v[i] < min) {
min = v[i];
}
promedio = promedio + v[i];
}
}
我基本上需要获取“min”、“max”和“promedio”才能在 devolverParametros 中进行修改并返回到主程序。
我已经尝试将变量的类型声明为包装类和本机类。
I understand that no such thing exists, but I know that you can do something like that with wrapper classes (?). I also can't use the return statement to return an array or anything. (It's for a university practice).
This is the code I have:
//This is the main:
calculadoraMax.devolverParametros(v, min, max, promedio);
System.out.println("min: " + min + " max: " + max + " promedio: " + promedio);
public static void devolverParametros(int[] v, Integer min, Integer max, Double promedio) {
for (int i = 0; i < v.length; i++) {
if(v[i] > max) {
max = v[i];
}
if(v[i] < min) {
min = v[i];
}
promedio = promedio + v[i];
}
}
I basically need to get "min", "max" and "promedio" to get modified in devolverParametros and be returned to the main.
I have already tried declaring the types of the variables both as Wrapped classes and native classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Java 是按值传递(请阅读Java 是“传递”吗? -by-reference”或“pass-by-value”?),因此您需要将这些变量移动到类级别。这是一个链接,看看这是否对您有帮助:https://stackoverflow.com/a/71553502/1643891
Java is pass-by-value (Please read Is Java "pass-by-reference" or "pass-by-value"?), hence you need move these variable to class level for that. Here's a link see if this is helpful to you : https://stackoverflow.com/a/71553502/1643891