如何用Java表示整数间隔? [英] How can I represent integer intervals in Java?

查看:49
本文介绍了如何用Java表示整数间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道数学上的区间(例如-4≤x≤3).

We all know about intervals in mathematics (e.g -4 < x < 3).

如何用Java表示这些数学区间,例如-4和3之间的数字(-4,-3,...,2、3等)?

How can I represent these mathematic intervals in Java, e.g, the numbers between -4 and 3 (-4, -3, ..., 2, 3 etc)?

推荐答案

检查apache commons-lang

Check apache commons-lang IntRange. So, if you want to check if a number is in a given interval (range), you do:

IntRange range = new IntRange(-4, 3);
if (range.contains(x)) {
   ....
}

这篇关于如何用Java表示整数间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆