如何通过“引用参数”传递参数? Java 上的 int 和 float

发布于 2025-01-16 10:37:08 字数 754 浏览 2 评论 0原文

我知道不存在这样的事情,但我知道你可以使用包装类(?)做类似的事情。我也无法使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扭转时空 2025-01-23 10:37:08

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文