java - 检查乘法表数组

标签 java arrays boolean

我的代码一直返回 false 我做错了什么?这是我得到的反馈。 checkTable() 仅当发现不正确的条目时才返回 false。

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;
        }

最佳答案

现在,如果Array中只有一个条目有效,您将立即返回true:

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

它需要反过来:

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

关于java - 检查乘法表数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52433187/

相关文章:

java - 如何阻止 boolean 返回打破循环?

python : Why does False or 'name' returns 'name' and not False?

java - 使添加的 JPanel 在父 JPanel 中可见

java - Glide 库加载 GIF 很慢

java - 将 int 数组转换为 char 数组

arrays - 通过通用函数更新不同的数组

PHP 通过另一个数组(表)按键对数组进行排序

java - 反转if,尝试了多种方法。找不到代码中的错误

java - 如何在 alamkanak 周 View 中添加事件

Java,如何使用将字符串和整数添加到数组中