布尔方法中最后返回真和假语句的区别

问题描述 投票:0回答:1

你好,我对布尔方法中的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语句中的值是正确的,就返回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

所以前面的方法返回真值,而nums显然是不同的,但我认为逻辑并没有改变... 是否有某种规则决定了返回语句的顺序? 还是我的代码存在逻辑问题?

java methods return boolean comparison
1个回答
2
投票

所以我不知道你是不是看了太长时间的代码而没有看到这行小代码,但它总是返回真,因为第一条if语句。

    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.EDIT********你的for语句也会在矩阵间第一次匹配时返回true。Lookinbg看if语句,2个矩阵相等的第一个索引,返回会导致代码脱离函数并返回true,不考虑第一次相似后的其他情况。在调用任何返回语句后,函数被放弃,在调用返回语句后将不再做任何代码。


0
投票

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

显然,在你的情况下,你必须检查两个矩阵中的每一个位置,并确保每一个位置都是相等的--这就是为什么第一个实现可以工作,而第二个却不行。


0
投票

当你使用 return 在一个方法里面,它会自动结束该方法的执行.所以想想看,在前两行你告诉你的方法,如果只有两个矩阵的大小是相同的,才会返回true。所以在你举的例子中,只要两个矩阵的大小相同就可以得到true.后面到for循环的时候也是一样,只要在相同的索引上有一个相同的值就可以返回true.这就是为什么你要检查它们是否是 相同,然后返回false。而只有当他们 "通过 "了所有的考试,才可以返回true。


0
投票

下面的代码返回 true 即使不比较数组中的元素,也会导致函数返回。

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

无论数组的内容如何,这种情况都会导致函数返回。true 如果数组的长度相等。

该方法的正确实现应该如下。

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 < b.length; i++) {
        for (int j = 0; j < m[i].length && j < b[i].length; j++) {
            if (m[i][j] != b[i][j]) {
                return false;
            }
        }
    }
    return true;
}

重要的是 请注意我是如何检查 b[][] 的终止条件中,也是如此。for 循环。这个检查非常重要,可以避免 ArrayIndexOutOfBoundsException.

顺便说一下,你不需要下面的语句,当你删除它时,一定要改变方法的返回类型。getMvoid. 我还会把它改名为 setM 在做了这个改变之后,为了使名称不言自明。

return m;

0
投票

你的代码由于很多原因不能工作,例如,为什么你的函数返回了 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;
© www.soinside.com 2019 - 2024. All rights reserved.