【问题标题】:difference between returning true and false statements at the end in a boolean method Java在布尔方法Java中最后返回真假语句之间的区别
【发布时间】:2020-08-08 08:41:39
【问题描述】:

美好的一天。我对布尔方法中的 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,但是
为什么当我比较它们的正确性并返回 true 如果 if 语句中的 val 正确并在最后返回 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 there is at least one index containing different elements, then the arrays are not equal. 第二个实现不正确,因为它说 if there is at least one index containing the same elements, then the arrays are equal 这显然不是你想要的

标签: java methods return boolean comparison


【解决方案1】:

所以我不确定你是否只是看代码太久而没有看到这一小行代码,但由于第一个 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语句,2个矩阵相等的第一个索引,return导致代码跳出函数返回true,不考虑第一个相似度后的其他情况。任何return语句被调用后,该函数就被放弃,调用return语句后不再做任何代码。

【讨论】:

  • 但是即使发出这一行也不能解决问题
  • 我尝试在没有第一次比较的情况下实现代码,但对于任何给定的数字它仍然返回 true
  • @vm_1_r 你在第一场比赛中返回真,而不是经历整个事情。那里的逻辑有很大的不同。
  • 这是没有第一个 if 的第二个(有问题的)案例的输出
  • 请重新阅读我的帖子和编辑,它将回答您问题的第二部分。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 感谢您的回答,但请尝试使用第一个 if 语句运行此代码并输入超过 9 个值 12 f.e.在第二种情况下做同样的事情,输出仍然是真实的......
    • 即使第一个 if 语句“检查”长度...您甚至可以在第一个矩阵中有 12 个元素,在第二个矩阵中有 20 个元素,输出将是 true
    • 输入第一个矩阵的数值:1 2 3 2 1 2 3 2 1 2 3 输入第二个矩阵的数值:1 2 3 23 23 2 32 32 3 23 2 32 3 23 23 2 3 23 23 2 正确
    • 同样,即使每个中的第一个值相同,该方法也会返回 true。在此示例中,该方法将返回 true,因为两个矩阵的第一个索引处都有 1。因此,当您的方法到达那里时,它将返回 true 并且不会检查其他元素。
    【解决方案4】:

    由于多种原因,您的代码无法运行,例如如果矩阵具有相同数量的行并且第一行具有相同数量的值,为什么您的函数会返回 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;
    

    【讨论】:

    • 谢谢......我同意我的代码中第一个 if 语句的不相关性,但即使它模拟检查输入的值它也没有......
    • 尝试在第一个矩阵中输入超过 9 个值,比如说 11
    • 例如输入第一个矩阵的数字:12 12 12 12 1 32 323 3 12 12 12 1 212 12 12123232 输入第二个矩阵的数字:21212 23 23 24 343 4 54 54 6 456 5 65 76 7 676 767 正确
    • if(m.length==b.length&&m[0].length==b[0].length)
    猜你喜欢
    • 2013-10-01
    • 1970-01-01
    • 2014-12-10
    • 2022-11-11
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    相关资源
    最近更新 更多