如何在Java中使用另一个类的变量

发布于 2024-12-16 13:50:35 字数 2001 浏览 0 评论 0原文

请帮助我如何使用 deviation_2DArray.java 中的变量 进入 NBC.java,在 NBC.java 中,我想通过 d[i][j]< 平均 b[i] /code> 和 c[j]

示例:

b[1]=avg (d[1][1]+d[1][2]+.....+d[1][5])

提前致谢。

2DArray.java

public class 2DArray {

    public static void main(String[] args) {

        double[][] d = new double[6][4];

        double[][] e = {
            {}, {
                0.0, 0.6, 0.0, 0.0
            }, {
                0.0, 0.2, 0.5, 0.1
            }, {
                0.0, 0.2, 0.5, 0.4
            }, {
                0.0, 0.2, 0.5, 0.7
            }, {
                0.0, 0.0, 0.0, 0.9
            }
        };

        double[] avg = new double[4];
        double[] sum = new double[4];
        int i, j, k;

        //average of column
        for (j = 1; j < e[1].length; j++) {
            for (i = 1; i < e.length; i++)
            System.out.println("e[" + i + "][" + j + "] = " + e[i][j]);
        }

        for (j = 1; j < e[1].length; j++) {

            sum[j] = 0.0;
            k = 0;
            for (i = 1; i < e.length; i++)

            if (e[i][j] > 0.0) {
                sum[j] += e[i][j];
                k++;
            }
            avg[j] = sum[j] / k;

            System.out.println("Average of j[" + j + "] = " + avg[j]);

        }

        for (j = 1; j < e[1].length; j++) {
            for (i = 1; i < e.length; i++)

            if (e[i][j] > 0.0) {

                d[i][j] = Math.abs(e[i][j] - avg[j]);
                System.out.println("d[" + i + "][" + j + "] = " + d[i][j]);

            }
        }

    }
}

NBC.java

public class NBC {
    public static void main(String[] args) {

        double[] b = new double[6];
        double[] c = new double[4];
        int count;

        b[i] = d[i][j] / count;
    }

}

Please help me how can I use a variable from deviation_2DArray.java
into NBC.java, in NBC.java I want to average b[i] by d[i][j] and c[j]

Example:

b[1]=avg (d[1][1]+d[1][2]+.....+d[1][5])

Thanks in advance.

2DArray.java

public class 2DArray {

    public static void main(String[] args) {

        double[][] d = new double[6][4];

        double[][] e = {
            {}, {
                0.0, 0.6, 0.0, 0.0
            }, {
                0.0, 0.2, 0.5, 0.1
            }, {
                0.0, 0.2, 0.5, 0.4
            }, {
                0.0, 0.2, 0.5, 0.7
            }, {
                0.0, 0.0, 0.0, 0.9
            }
        };

        double[] avg = new double[4];
        double[] sum = new double[4];
        int i, j, k;

        //average of column
        for (j = 1; j < e[1].length; j++) {
            for (i = 1; i < e.length; i++)
            System.out.println("e[" + i + "][" + j + "] = " + e[i][j]);
        }

        for (j = 1; j < e[1].length; j++) {

            sum[j] = 0.0;
            k = 0;
            for (i = 1; i < e.length; i++)

            if (e[i][j] > 0.0) {
                sum[j] += e[i][j];
                k++;
            }
            avg[j] = sum[j] / k;

            System.out.println("Average of j[" + j + "] = " + avg[j]);

        }

        for (j = 1; j < e[1].length; j++) {
            for (i = 1; i < e.length; i++)

            if (e[i][j] > 0.0) {

                d[i][j] = Math.abs(e[i][j] - avg[j]);
                System.out.println("d[" + i + "][" + j + "] = " + d[i][j]);

            }
        }

    }
}

NBC.java

public class NBC {
    public static void main(String[] args) {

        double[] b = new double[6];
        double[] c = new double[4];
        int count;

        b[i] = d[i][j] / count;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

神经暖 2024-12-23 13:50:35

我将把它恢复为一个简单的词:getters

I'll resume it to one simple word: getters.

≈。彩虹 2024-12-23 13:50:35

您需要在 2DArray 类中创建类级字段,并为这些字段提供 getter 方法。那么 NBC 需要有一个 2DArray 实例,除非您将这些字段/getter 设为静态。

目前,2DArray 中的 d 变量仅在 main 方法的范围内,因此仅提供 getter 并不能解决问题,因为 d仅在方法中具有范围。

此外,每个类都有自己的 main 方法。每个应用程序只能执行一个main。那么您的控制流是如何从 2DArrayNBC 的?如果 2DArray 正在调用 NBC,那么您可以将该数组作为参数传递。

You need to create class-level fields in the 2DArray class and provide getter methods to those fields. Then NBC would need to have an instance of 2DArray unless you made those fields / getters static.

Currently your d variable in 2DArray is only in the scope of the main method, therefore just providing a getter won't solve the problem because d only has scope in the method.

Also, each class has its own main method. You can only execute one main per application. So how is your flow of control getting from 2DArray to NBC? If 2DArray is calling NBC, then you could pass the array as an argument.

单调的奢华 2024-12-23 13:50:35

在 2DArray 中创建一个方法,如下所示:

public double getElement(int i, int j) {
  return d[i][j];
}

Create a method in the 2DArray something like this:

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