简体   繁体   English

检查乘法表数组

[英]checking multiplication table arrays

My code keeps returning false what am I doing wrong? 我的代码不断返回false我在做什么错? This is the feedback I am getting. 这是我得到的反馈。 checkTable() should only return false when it finds an incorrect entry. checkTable()仅应在找到不正确的条目时返回false。

Failed: java.lang.AssertionError: checkTable() should return false when entry at index 0 is not equal to 0. 失败: java.lang.AssertionError: checkTable()当索引0处的条目不等于0时, java.lang.AssertionError: checkTable()应该返回false。

/**
         * This method checks if an array of ints contains a correctly calculated
         * multiplication table.<br/>
         * Precondition: multTable IS NOT null. (PRECONDITIONS SPECIFY RESPONSIBILITIES
         * ON A METHOD'S CALLER; YOU SHOULD ASSUME THIS IS TRUE.)
         *
         * @param constant
         *            Value used to check the multiplication table.
         * @param multTable
         *            Table to be checked.
         * @return True if every entry in the array stores the value of {@code constant}
         *         * the index for that entry; or false if at least one entry is
         *         incorrect.
         */
        public static boolean checkTable(int constant, int[] multTable) {
            int i=0;

            for(i=0;i<multTable.length;i++){
                int mult = constant*i;
                if(mult==multTable[i]) {
                    return true;
                }

            }
            return false;
        }

Right now you are immediately returning true if only one entry in the Array is valid: 现在,如果Array只有一个有效条目,您将立即返回true

if(mult==multTable[i]) {
     return true;
}

It needs to be the other way around: 它必须是相反的方式:

for(i=0;i<multTable.length;i++){
     int mult = constant*i;
     if(mult!=multTable[i]) {
         return false;
     }
 }
 return true;

Just step through the code, line by line. 只需逐行浏览代码。 Let's say the constant is '5', and the input is new int[] {0, 15, 38}; 假设常量为“ 5”,输入为new int [] {0,15,38}; which is clearly not a multiplication table. 这显然不是乘法表。

int i = 0; : i now exists. :我现在存在。 The value of i is now 0. i的值现在为0。

for (int i = 0; i < multTable.length; i++) { we're going to loop 3 times. for (int i = 0; i < multTable.length; i++) {我们将循环3次。 We enter the loop with the first value (0). 我们使用第一个值(0)进入循环。

int mult = constant * i; : mult now exists. :现在存在多个。 The value of mult is 0 (because 5*0 is 0). mult的值为0(因为5 * 0为0)。

if (mult == multTable[i]) return true; : mult is 0; :倍数为0; the first item in our example multTable input is 0. Therefore the if triggers and the entire method returns, with value true. 示例multTable输入中的第一项为0。因此,如果if触发器且整个方法返回,则值为true。 The remaining elements aren't checked at all. 其余元素完全不会检查。

The fix is obviously NOT to return when the first element is correct. 解决方法显然是在第一个元素正确时返回。 When you hit a 'mistake' you know immediately that the answer to the question "Is this a valid multiplication table" is false. 当您遇到“错误”时,您立即知道问题“这是一个有效的乘法表”的答案为假。 But when you hit a correct entry that doesn't mean you know the answer. 但是,当您输入正确的条目并不表示您知道答案。 You'd have to keep going. 您必须继续前进。

This smells of homework so I'll let you do the honours of figuring out what that means and how you can fix your code. 这有点作业的味道,所以我让您荣幸地弄清这意味着什么以及如何修复代码。

NB: This is how you solve problems with code: You reason through it. 注意:这是解决代码问题的方法:通过代码进行推理。 You check what the code actually does (using a debugger, or if that's a bridge too far to learn for now, add a lot of System.out.println statements). 您检查代码的实际作用(使用调试器,或者如果这是目前System.out.println了解的桥梁,请添加许多System.out.println语句)。 The point where your code doesn't match what you thought it should do is the point where you figured out the bug. 您的代码与您认为的应做的不匹配的地方就是您发现错误的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM