我的代码一直返回 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/