[英]Return the boolean value
I have to write such a program: 1.two two-dimensional arrays have the same dimensions m × n, 2.satisfy the condition that aij = bij for i = 0,. 我必须编写这样一个程序:1.两个二维数组具有相同的维数m×n,2.满足aij = bij且i = 0的条件。 .
。 .
。 , m and j = 0,.
,m和j = 0 ,。 .
。 .
。 , n The method returns the result in the form of a logical value true only when both of the above are met simultaneously.
,n仅当同时满足以上两个条件时,该方法才以逻辑值true的形式返回结果。 what does it mean "returns the result"?
“返回结果”是什么意思? This means that it displays on the screen?
这意味着它显示在屏幕上吗? I wrote something like that:
我写了这样的东西:
static boolean isEqual(int[][] a, int[][] b) {
int ia = 0;`
int ib = 0;
int ja = 0;
int jb = 0;
boolean equal=true;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
ia++;
}
ja++;
}
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
ib++;
}
jb++;
}
if (ia == ib && ja == jb) {
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
if (a[i][j] != b[i][j]) {
equal= false;
}
}
}
} else {
equal= false;
}
return equal;
}
} }
Your code doesn't need ia
, ib
, ja
, and jb
. 您的代码不需要
ia
, ib
, ja
和jb
。 Since you've been told that both arrays have the same dimensions (m × n), you can simply get those two values using int m = a.length; int n = a[0].length;
既然已经知道两个数组的维数相同(m×n),则可以使用
int m = a.length; int n = a[0].length;
来简单地获得这两个值int m = a.length; int n = a[0].length;
int m = a.length; int n = a[0].length;
That then simplifies your code to: 然后将您的代码简化为:
static boolean isEqual(int[][] a, int[][] b) {
int m = a.length;
int n = a[0].length;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (a[i][j] != b[i][j])
return false;
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.