How to check whether an int is within a range of values

喜夏-厌秋 提交于 2019-12-24 19:12:36

问题


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)

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?


回答1:


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

Full sample

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

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)



回答2:


Surprise surprise, it is c >= low && c <= high

To answer to the update, you'll need to employ a set

Set<Integer> validValues = new HashSet<Integer>();
validValues.add(1);
validValues.add(4);
validValues.add(9);
validValues.add(10);
validValues.add(19);

if (validValues.contains(currentVal)) {
    // do stuff
}

To curb java's verbosity you may use Guava's immutable set:

Set<Integer> validValues = ImmutableSet.of(1, 4, 9, 10, 19);



回答3:


Try like this, range1 and range2 are ranges from which you need to check b.

(c < range1 && c >  range2) ? d=true : d=false;



回答4:


You simply need to add:

c > 1 && c < 8

If you ever feel that your conditionals are getting too complicated I'd suggest creating Boolean methods that simplify them down a little. This is a very moderate case, but for example this range problem could be represented by:

public boolean inRange(int num)
{
    return (num > 1 && num < 8);
}



回答5:


Unfortunately, the only way to do it would be using if else statements (as far as I know). A switch statement does not accept conditional operators, and so would not work for that. As suggested by some others, you could use seperate methods, as the methods, would allow you to check if other values were in range as well. Instead of just for this value, however, you could go for something like

public Boolean inRange(int num, int lowBound, int topBound)
{
    return (num > lowBound && num < topBound);

}

and just use it in your code like this

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

It does not look too messy, and will work (you'll need to decide whether it is inclusive or exclusive bounds though).

The reason for the changed method is that it could be used in other places as well, making it useful for range checking.



来源:https://stackoverflow.com/questions/19827999/how-to-check-whether-an-int-is-within-a-range-of-values

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