简体   繁体   English

我的代码失败,但我不知道原因

[英]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;
    }
}
  1. The arguments highest and lowest should be validated first应首先验证 arguments highestlowest
  2. There is a requirement that lowest and highest are within the range [lowest, highest] , while existing code excludes these values (lowest, highest)要求lowesthighest[lowest, highest]范围内,而现有代码不包括这些值(lowest, highest)
  3. & is a bitwise operator, && should be used as logical AND operator &是位运算符, &&应该用作逻辑 AND 运算符
  4. The last return is redundant最后的return是多余的

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.

相关问题 我的代码似乎正在循环,我不知道为什么 - My code seems to be looping and I don't know why 我不知道为什么我的代码给出了错误的答案 - I don't know why my code is giving wrong answer 我不知道如何使用工具提示 - I don't know how to get my tooltips to work 交换机正在以红色取消,我不知道原因 - Switch is being undelined in red and i don't know the reason 我的Java Chatbot代码不起作用,我也不知道为什么 - My Java Chatbot code don't work and i don't know why 我必须编写有关软件销售的源代码。 我不知道我的错误是什么。 - I have to write a source code on software sales. I don't know what my mistake is. 我的代码中有一个nullPointerException,我不知道如何解决 - I have a nullPointerException in my code that I don't know how to resolve 我的代码出现错误,我不知道如何修复(Java fx stackPane) - I am getting errors in my code and i don't know how to fix(Java fx stackPane) 我想将最后三位设置为零,但是我不知道我的代码在哪里? - I want to set last three bit to zero,but I don't know where My code wrong? 帮助我不知道如何处理此错误(java.lang.RuntimeException:EMBEDDED Broker启动失败:代码= 1) - Help I don't know how to handle this error (java.lang.RuntimeException: EMBEDDED Broker start failure:code = 1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM