简体   繁体   中英

checking multiplication table arrays

My code keeps returning false what am I doing wrong? This is the feedback I am getting. checkTable() should only return false when it finds an incorrect entry.

Failed: java.lang.AssertionError: checkTable() should return false when entry at index 0 is not equal to 0.

/**
         * 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:

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}; which is clearly not a multiplication table.

int i = 0; : i now exists. The value of i is now 0.

for (int i = 0; i < multTable.length; i++) { we're going to loop 3 times. We enter the loop with the first value (0).

int mult = constant * i; : mult now exists. The value of mult is 0 (because 5*0 is 0).

if (mult == multTable[i]) return true; : mult is 0; the first item in our example multTable input is 0. Therefore the if triggers and the entire method returns, with value 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). The point where your code doesn't match what you thought it should do is the point where you figured out the bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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