在布尔方法Java中,在末尾返回true和false语句之间的区别



您好。我对布尔方法中的true\false返回调用有点混淆。所以代码是:

public class CheckOut {
public static void main(String[] args) {
int[][] m = new int[3][3];
int[][] m1 = new int[m.length][m[0].length];
System.out.println("Enter the nums for the first matrix : ");
getM(m);
System.out.println("Enter the nums for the second matrix : ");
getM(m1);
System.out.println(strictlyIdentical(m, m1));
}
static int[][] getM(int[][] m) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
m[i][j] = sc.nextInt();
}
}
return m;
}
static boolean strictlyIdentical(int[][] m, int[][] b) {
if (m.length != b.length && m[0].length != b[0].length) {
return false;
}
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
if (m[i][j] != b[i][j]) {
return false;
}
}
}
return true;
}
}

上面的方法工作得很好,如果两个矩阵相同,则返回true,但
为什么当我比较它们的正确性值时,如果if语句中的vals正确,则返回true,并在最后返回false,我没有得到所需的输出。(任何输入的数字都是如此(考虑一下:

static boolean strictlyIdentical(int[][] m, int[][] b) {
if (m.length == b.length && m[0].length == b[0].length) {
return true;
}
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
if (m[i][j] == b[i][j]) {
return true;
}
}
}
return false;
}
}

现在我在比较它们的相似性而不是差异,如果我可以这么说的话。。。如果给定以下输入,则该代码的输出如下:

Enter the nums for the first matrix : 
12 3 4 3 2 1 2 3 3
Enter the nums for the second matrix : 
1 2 3 2 3 2 1 2 3 
true

因此,前面的方法返回true,而nums显然不同。但在我看来,逻辑并没有改变。。。有没有一条规则规定了返回语句的顺序?或者我的代码中存在逻辑问题?

因此,我不确定您是否只是看代码太久而没有看到这一小段代码,但由于第一个if语句,它总是返回true。

static boolean strictlyIdentical(int[][] m, int[][] b) {
if (m.length == b.length && m[0].length == b[0].length) {
return true;
}
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
if (m[i][j] == b[i][j]) {
return true;
}
}
}
return false;
}

如果长度相等,则第一个if语句将始终返回true。您给出的示例具有相同长度的矩阵,因此返回true。编辑*****在矩阵之间的第一次匹配时,for语句也将返回true。查看if语句,即两个矩阵相等的第一个索引,返回会导致代码脱离函数并返回true,而不考虑第一个相似性之后的其他情况。调用任何返回语句后,该函数将被放弃,并且在调用返回语句后将不再执行任何代码。

第二个实现肯定有一个逻辑错误。不同的是,如果任何两个元素不相等,第一个将返回false,而当任何两个元素相等时,第二个将立即返回true,更不用说,正如JakeTheSnake所指出的,第一个检查也是错误的。

显然,在您的情况下,您必须遍历两个矩阵中的每个位置,并确保每个位置都相等——这就是为什么第一个实现有效,而第二个实现无效。

在方法内部使用return时,它会自动结束该方法的执行。所以想一想,在前两行,如果只有两个矩阵的大小相同,你会告诉你的方法返回true。所以在你给出的例子中,两个矩阵大小相同就足够了。同样的事情也会发生在之后的for循环中,在同一索引中有一个相同的值就足以返回true。这就是为什么您应该检查它们是否相同,然后返回false。只有他们"通过"了所有的考试,你才能返回真。

由于多种原因,您的代码无法工作,例如,如果矩阵的行数相同,而第一行的值数相同,为什么函数会返回True

if (m.length == b.length && m[0].length == b[0].length) {
return true;
}

您不检查行内的值

顺便说一句,我写下我的解决方案,试试这个,如果你有问题,我会在这里回答。

static boolean strictlyIdentical(int[][] m, int[][] b) {
// Check if both matrices have the same amount of row, if not then return false
if (m.length == b.length) {
// If yes save that number
int rowAmount = m.length;
// Start control the i-th row
for (int i = 0; i < rowAmount; i++) {
// Check if the i-th row of both matrices have the same amount of values, if not then return false
if(m[i].length != b[i].length) return false;
// If yes save that number
int rowLength = m[i].length;
// Check if values are equal, if not then return false
for (int j = 0; j < rowLength; j++) {
if (!(m[i][j] == b[i][j])) return false;
}
}
// Matrices passed all controls, they are IDENTICAL
return true;
}
return false;

最新更新