[英]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!!!)
When I run the test, I get zero errors, but I also get one failure.当我运行测试时,我得到零错误,但我也得到一个失败。 What could be the reaseon for the failure?
失败的原因可能是什么? I don´t get any error informations in my console or next to my code on the left side shown:(
我在控制台或左侧代码旁边没有收到任何错误信息:(
My Code:我的代码:
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
and lowest
should be validated firsthighest
和lowest
lowest
and highest
are within the range [lowest, highest]
, while existing code excludes these values (lowest, highest)
lowest
和highest
在[lowest, highest]
范围内,而现有代码不包括这些值(lowest, highest)
&
is a bitwise operator, &&
should be used as logical AND operator &
是位运算符, &&
应该用作逻辑 AND 运算符return
is redundantreturn
是多余的Thus, the correct code may look like:因此,正确的代码可能如下所示:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.