如何检查int是否在值范围内 [英] How to check whether an int is within a range of values

查看:412
本文介绍了如何检查int是否在值范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在特定的 if-else 中,我必须确定给定值是否在一个范围内。条件部分如下所示:

In a particular if-else, I have to identify if a given value falls within a range. The condition part looks like this:

else if (a==2 && b.equalsIgnoreCase("line")
     && <<Here I need to search if c falls within a range>> && d)

其中a是int,b是字符串,c是int,d是布尔值。现在,如果c落在1到8之间,则条件为真,否则为假。我怎么能这样做?

where a is int, b is string, c is int and d is a boolean. Now if c falls within 1 to 8, the condition is true, else it is false. How can I do that?

推荐答案

我认为你需要这个条件 c

I think you need you this condition for c

(c > 1 && c < 8) // 1 and 8 are exclusive
(c => 1 && c <= 8) // 1 and 8 are inclusive

完整样本

else if (a==2 && b.equalsIgnoreCase("line")
     && (c > 1 && c < 8) && d)

如果你需要检查值是否属于一组值,你需要使用 Set ,然后检查 c 属于或不属于。在您的原始问题中,它被称为范围,因此是答案。无论如何,这是你如何检查集合中的值。

If you need to check if the values belongs to a set of values, you need to use a Set and then check if the c belongs to that or not. In your original question, it was mentioned as a range and thus the answer. Anyways, this is how you can check a value in a set.

Integer[] arr = {1,4,9,11,13};
Set<Integer> set = new HashSet<>(Arrays.asList(arr));
...
else if (a==2 && b.equalsIgnoreCase("line")
     && (set.contains(c)) && d)

这篇关于如何检查int是否在值范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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