在我的代码中,是否可以访问方法calculate()
中的方法calculateSheet()
?这2种方法属于同一类。
方法calculate()
计算2个双精度的总和。
我想要方法calculateSheet()
,使用此方法为数组sheet[] []
中的每个“单元格”计算此值。
//calculates every cell in the array.
public void calculateSheet() {
for(int i = 0 ; i < rows ; i++) { //rows, amount of rows in the array>
for (int j = 0 ; j < cols ; j++) { //cols, amount of colums in the array.
sheet[i] [j].**calculate()** ; //method I want to use in the "calculateSheet" method.
}
}
}
答案 0 :(得分:0)
首先,你需要你想要做的计算,你必须把它们放在一些变量中。
其次我相信你需要你的calculate()方法需要返回结果......
假设您的方法计算类似于:
public double calculate(double a, double b){
return a+b;
}
然后你需要做类似的事情:
public void calculateSheet() {
double total = 0; //The variable when you going to put the result
for(int i = 0 ; i < rows ; i++) { //rows, amount of rows in the array>
for (int j = 0 ; j < cols ; j++) { //cols, amount of colums in the array.
total += calculate(sheet[i], sheet[j]);
}
}
}
答案 1 :(得分:0)
让我们为您制作完整的代码,
类{
public double calculate(double num1, double num2){
return (num1+num2);
}
public void calculateSheet(int[][] sheet) {
for(int i = 0 ; i < rows ; i++) {
for (int j = 0 ; j < cols ; j++) {
sheet[i] [j] = calculate(i, j);
}
}
}
}
这里,2D数组的每个单元格都将存储两个索引的总和。