如何在Java中使用另一个类的变量
请帮助我如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将把它恢复为一个简单的词:getters。
I'll resume it to one simple word: getters.
您需要在 2DArray 类中创建类级字段,并为这些字段提供 getter 方法。那么 NBC 需要有一个 2DArray 实例,除非您将这些字段/getter 设为静态。
目前,
2DArray
中的d
变量仅在 main 方法的范围内,因此仅提供 getter 并不能解决问题,因为d
仅在方法中具有范围。此外,每个类都有自己的
main
方法。每个应用程序只能执行一个main
。那么您的控制流是如何从2DArray
到NBC
的?如果2DArray
正在调用NBC
,那么您可以将该数组作为参数传递。You need to create class-level fields in the
2DArray
class and provide getter methods to those fields. ThenNBC
would need to have an instance of2DArray
unless you made those fields / getters static.Currently your
d
variable in2DArray
is only in the scope of the main method, therefore just providing a getter won't solve the problem becaused
only has scope in the method.Also, each class has its own
main
method. You can only execute onemain
per application. So how is your flow of control getting from2DArray
toNBC
? If2DArray
is callingNBC
, then you could pass the array as an argument.在 2DArray 中创建一个方法,如下所示:
Create a method in the 2DArray something like this: