我想求和單元格。我的意思是,圍繞細胞。添加我要調用的單元格並在其周圍添加值。這是我迄今爲止所擁有的。如何求和矩陣內的單元格和環繞單元
import java.util.Arrays;
import static java.lang.System.*;
public class MatrixSummingRunner {
public static void main(String args[]) {
int[][] mat = { { 0, 1, 0, 0, 0, 1, 0 }, { 1, 8, 4, 3, 4, 5, 1 },
{ 0, 2, 7, 8, 9, 8, 0 }, { 0, 6, 7, 6, 2, 5, 0 },
{ 0, 6, 7, 8, 9, 5, 0 }, { 1, 5, 4, 3, 2, 3, 1 },
{ 0, 1, 0, 0, 0, 1, 0 } };
out.println("Sum of the given values, at {4,2} and the values around it"
+ MatrixSumming(mat, 4, 2));
out.println("Sum of the given values, at {3,3} and the values around it"
+ MatrixSumming(mat, 3, 3));
out.println("Sum of the given values, at {5,4} and the values around it"
+ MatrixSumming(mat, 5, 4));
out.println(toString(mat));
}
}
這是跑步者,現在我想什麼會工作。
import java.util.*;
public class MatrixSumming {
private int[][] m = { { 4, 2 }, { 3, 3 }, { 5, 4 } }; // load in the matrix
// values
public int sum(int[][] mat, int x, int y) {
int sum = 0;
for (int item : mat) {
Sum = getValueAt(x, y) + getValueAt(x++, y) + getValueAt(x--, y)
+ getValueAt(x--, y++) + getValueAt(x--, y--)
+ getValueAt(x++, y++) + getValueAt(x++, y--);
}
return sum;
}
}
顯然,這沒有奏效。它的票他們作爲單個變量,造成嚴重的問題,我...