下面的程序(感谢日晷)计算出一个矩形2-d阵列(行和列)
公共类ComputeTheArea {
public static int areaOfTheRectangle (char[][] table, char ch) {
int[] first = new int[2];
int[] last = new int[2];
for (int i=0; i<3; i++) {
for (int j=0; j<4; j++) {
if(grid[i][j]==ch) {
first[0] = i;
first[1] = j;
}
}
}
for (int i=2; i>=0; i--) {
for (int j=3; j>=0; j--) {
if(grid[i][j]==ch) {
last[0] = i;
last[1] = j;
}
}
}
int answer = ((Math.max(first[0]+1,last[0]+1) - Math.min(first[0]+1,last[0]+1)) *
(Math.max(first[1]+1,last[1]+1) - Math.min(first[1]+1,last[1]+1)));
return answer;
}
然而,在运行时,它输出的区域错误的答案。我知道for循环有什么问题。我是Java新手,我需要您的帮助才能修复此方法。请非常感谢你!
编辑:我编辑了代码以符合迈克尔的答案。
这给出了什么错误的答案? – 2012-07-15 11:21:04
你好@BhavikAmbani!当用户输入'a'时,它将返回6.如果'b,c,d',它将返回0. – 2012-07-15 11:24:41