Finding out if a certain value is in Range Java

落爺英雄遲暮 提交于 2019-12-25 01:43:42

问题


I'm pretty new at programming in Java and I'm trying to complete this package but I seem not to be getting it right. I need to find is the object I call is in range for example if I have rows 0 to 3 and columns 1 to 2 the DataEntry object on which you call the method inRange with this range has row = 2, column = 1, it should return true, as the row (2) is between 0 and 3 and the column (1) is between (1 and 2). I've tried different codes and the current code I have is what I thought is the best option, however, it's giving me errors when I test it. if i have DataEntry entry = new DataEntry(2, 4, 8.88); then it should return true for(entry.inRange(3, 5, 2, 4) return true for(entry.inRange(5, 4, 2, 5) return false for(entry.inRange(5, 4, 3, 5) return false for (entry.inRange(0, 0, 2, 3)

private int row, column;
private double value;

public DataEntry(int r, int c, double val) {
    setRow(r);
    setColumn(c);
    value = val;
}

public void setRow(int r) {
    row = Math.max(0, r);
}

public void setColumn(int c) {
    column = Math.max(0, c);
}

public void setValue(double val) {
    value = val;
}

public int getRow() {
    return row;
}

public int getColumn() {
    return column;
}

public double getValue() {
    return value;
}

//DO NOT MODIFY ANY CODE ABOVE THIS COMMENT

/**
 * @param row1
 * @param column1
 * @param row2
 * @param column2
 * @return true if the current item is in the range provided
 * i.e., between rows row1 and row 2 (inclusive) and between 
 * columns column1 and column2 (inclusive), false otherwise
 */
public boolean inRange(int row1, int column1, int row2, int column2) {
if (this.row <row1){
return false;
}
if (this.row>row2 )
return false;
}
if  (this.column <column1) {
return false;
}
if (this.column> column2){
    return false;
}
        return true;

    //this is my code
}

}


回答1:


You can add a great deal of clarity to your code (and make it easier to debug) if you approach the problem from a slightly different direction.

Something like this should help you track down the issue.

public boolean inRange(int row1, int column1, int row2, int column2) {
    // Rows first.
    if ( row < row1 ) {
        return false;
    }
    if ( row > row2 ) {
        return false;
    }
    // Then columns.
    if ( column < column1) {
        return false;
    }
    if ( column > column2) {
        return false;
    }
    // Everything is in range.
    return true;
}

Notice how I have reversed the logic and instead of building a complex boolean expression that should be true for the case you are looking for, I eliminate the bad states by returning false when we are out of bounds.

NB: The comparisons I have used may or may not be correct but at least you should be able to work out what is going wrong more easily.



来源:https://stackoverflow.com/questions/47010404/finding-out-if-a-certain-value-is-in-range-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!