本文介绍了找出某个值是否在 Range Java 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Java 编程还很陌生,我正在尝试完成这个包,但我似乎没有做对.我需要找到我调用的对象是否在范围内,例如,如果我有第 0 行到第 3 行和第 1 列到第 2 列,那么您在此范围内调用方法 inRange 的 DataEntry 对象的行 = 2,列 = 1,它应该返回 true,因为行 (2) 介于 0 和 3 之间,列 (1) 介于 (1 和 2) 之间.我尝试了不同的代码,我拥有的当前代码是我认为最好的选择,但是,当我测试它时,它给了我错误.如果我有 DataEntry entry = new DataEntry(2, 4, 8.88);那么它应该返回 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)) 为 (entry.inRange(0, 0, 2, 3)

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
}

}

推荐答案

如果您从稍微不同的方向处理问题,您可以使代码更加清晰(并使调试更容易).

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;
}

注意我是如何颠倒逻辑的,而不是构建一个复杂的布尔表达式,对于您正在寻找的情况应该是 true,我通过返回 false当我们越界时.

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.

这篇关于找出某个值是否在 Range Java 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 10:45