我的代码失败了,但我不知道原因
I get a failure for my code but I don´t know the reason
/**
* Checks whether a given value is in a given range defined by its lowest and highest possible value
* Both of the borders of the range (lowestPossible, highestPossible) are considered to be inside of the range
* An IllegalArgumentException is thrown if the highestPossible value is less than the lowestPossible value
* @param lowestPossible The lowest possible value of the range
* @param highestPossible The highest possible value of the range
* @param guess The value that has been guessed
* @return <code>true</code> if the guess is withing the given range; <code>false</code> otherwise
*/
public static boolean isWithinBorders(int lowestPossible, int highestPossible, int guess) {
// return false; // TODO: IMPLEMENT ME (AND DELETE THIS COMMENT AFTERWARDS!!!)
当我 运行 测试时,我得到了零个错误,但也有一个失败。失败的原因可能是什么?我没有在我的控制台中或左侧显示的代码旁边收到任何错误信息:(
我的代码:
if (guess > lowestPossible & guess < highestPossible) { //is value guess between highest & lowest number?
//if (lowestPossible < highestPossible && highestPossible > lowestPossible) { //
return true;
}else if(highestPossible < lowestPossible) {
throw new IllegalArgumentException("lowestPossible can´t be higher then highestPossible");
}else {
return false;
}
}
- 应首先验证参数
highest
和 lowest
- 要求
lowest
和 highest
在 [lowest, highest]
范围内,而现有代码不包括这些值 (lowest, highest)
&
是按位运算符,&&
应该用作逻辑AND运算符
- 最后一个
return
多余
因此,正确的代码可能如下所示:
public static boolean isWithinBorders(int lowest, int highest, int guess) {
if (highest < lowest) {
throw new IllegalArgumentException("Lowest must be less than highest");
}
return lowest <= guess && guess <= highest;
}
/**
* Checks whether a given value is in a given range defined by its lowest and highest possible value
* Both of the borders of the range (lowestPossible, highestPossible) are considered to be inside of the range
* An IllegalArgumentException is thrown if the highestPossible value is less than the lowestPossible value
* @param lowestPossible The lowest possible value of the range
* @param highestPossible The highest possible value of the range
* @param guess The value that has been guessed
* @return <code>true</code> if the guess is withing the given range; <code>false</code> otherwise
*/
public static boolean isWithinBorders(int lowestPossible, int highestPossible, int guess) {
// return false; // TODO: IMPLEMENT ME (AND DELETE THIS COMMENT AFTERWARDS!!!)
当我 运行 测试时,我得到了零个错误,但也有一个失败。失败的原因可能是什么?我没有在我的控制台中或左侧显示的代码旁边收到任何错误信息:(
我的代码:
if (guess > lowestPossible & guess < highestPossible) { //is value guess between highest & lowest number?
//if (lowestPossible < highestPossible && highestPossible > lowestPossible) { //
return true;
}else if(highestPossible < lowestPossible) {
throw new IllegalArgumentException("lowestPossible can´t be higher then highestPossible");
}else {
return false;
}
}
- 应首先验证参数
highest
和lowest
- 要求
lowest
和highest
在[lowest, highest]
范围内,而现有代码不包括这些值(lowest, highest)
&
是按位运算符,&&
应该用作逻辑AND运算符- 最后一个
return
多余
因此,正确的代码可能如下所示:
public static boolean isWithinBorders(int lowest, int highest, int guess) {
if (highest < lowest) {
throw new IllegalArgumentException("Lowest must be less than highest");
}
return lowest <= guess && guess <= highest;
}