Guava可以在“介于”之间建立两个条件的范围。
我正在努力制定范围条件以评估价值。
对于每个符号检查值,我已经制定了开关条件,以生成句子以灵活地评估值。
低于用于检查值的代码有效吗? (例如300< = value< = 500)。
我已经尝试了几次,但是无法制作范围。
(如果这些方法无效,我会将它们更改为.clate等。)
谢谢!
public void symbolToRange(RangeSet<Integer> rangeSet, String symbol, int targetNum) {
switch (symbol) { // <, <=, =, >=, >
case ">":
// targetNum < value
rangeSet.add(Range.greaterThan(targetNum));
break;
case ">=":
// targetNum <= value
rangeSet.add(Range.atLeast(targetNum));
break;
case "<":
// targetNum > value
rangeSet.add(Range.lessThan(targetNum));
break;
case "<=":
// targetNum >= value
rangeSet.add(Range.atMost(targetNum));
break;
case "=":
rangeSet.add(Range.singleton(targetNum));
break;
}
}
i'm struggling with making range conditions to evaluate values.
For checking values per symbol, I have made switch condition to generate sentence for evaluating values flexibly.
Is below code valid for checking values? (e.g. 300 <= value <= 500).
I have tried a few times, but it was not possible to make range.
(If those method are not effective, i would change them to .closed etc.)
Thanks!
public void symbolToRange(RangeSet<Integer> rangeSet, String symbol, int targetNum) {
switch (symbol) { // <, <=, =, >=, >
case ">":
// targetNum < value
rangeSet.add(Range.greaterThan(targetNum));
break;
case ">=":
// targetNum <= value
rangeSet.add(Range.atLeast(targetNum));
break;
case "<":
// targetNum > value
rangeSet.add(Range.lessThan(targetNum));
break;
case "<=":
// targetNum >= value
rangeSet.add(Range.atMost(targetNum));
break;
case "=":
rangeSet.add(Range.singleton(targetNum));
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会返回
范围
,而不是添加到rangeset
上,然后在无效的符号上抛出异常:除这些更改外,您的功能似乎还不错。
I would have the function return a
Range
instead of adding to aRangeSet
, and throw an exception on an invalid symbol:Other than those changes your function seems fine.