How to check if an integer is in a given range?

前端 未结 18 1238
遥遥无期
遥遥无期 2020-11-27 03:58

Hoping for something more elegant than

if (i>0 && i<100) 
相关标签:
18条回答
  • 2020-11-27 04:36

    I think its already elegant way for comparing range. But, this approach cause you to write extra unit tests to satisfy all && cases.

    So, you can use any of the below approach to avoid writing extra unit tests.

    Using Java 8 Streams:

    if(IntStream.rangeClosed(0,100).boxed().collect(Collectors.toList()).contains(i))
    

    Using Math class:

    if(Math.max(0, i) == Math.min(i, 100))
    

    Personally I recommend the second approach because it won't end up creating an Array of the size equal to the range you want to check.

    0 讨论(0)
  • 2020-11-27 04:39

    If you are confident that numbers are stored in 2's complement form:

    return  ((x-low) <= (high-low));
    

    A more general and safer solution would be:

    return ((x-high)*(x-low) <= 0);
    
    0 讨论(0)
  • 2020-11-27 04:39

    Google's Java Library Guava also implements Range:

    import com.google.common.collect.Range;
    
    Range<Integer> open = Range.open(1, 5);
    System.out.println(open.contains(1)); // false
    System.out.println(open.contains(3)); // true
    System.out.println(open.contains(5)); // false
    
    Range<Integer> closed = Range.closed(1, 5);
    System.out.println(closed.contains(1)); // true
    System.out.println(closed.contains(3)); // true
    System.out.println(closed.contains(5)); // true
    
    Range<Integer> openClosed = Range.openClosed(1, 5);
    System.out.println(openClosed.contains(1)); // false
    System.out.println(openClosed.contains(3)); // true
    System.out.println(openClosed.contains(5)); // true
    
    0 讨论(0)
  • 2020-11-27 04:39
        val = val < MIN ? MIN : ( val > MAX ? MAX : val);
    
    0 讨论(0)
  • 2020-11-27 04:41
    ValueRange range = java.time.temporal.ValueRange.of(minValue, maxValue);
    range.isValidIntValue(x);
    

    it returns true if minValue <= x <= MaxValue - i.e within the range

    it returns false if x < minValue or x > maxValue - i.e outofrange

    Use with if condition as shown below:

    int value = 10;
    if(ValueRange.of(0, 100).isValidIntValue(value)) {
        System.out.println("Value is with in the Range.");
    } else {
        System.out.println("Value is out of the Range.");
    }
    

    below program checks, if any of the passed integer value in the hasTeen method is within the range of 13(inclusive) to 19(Inclusive)


    import java.time.temporal.ValueRange;    
    public class TeenNumberChecker {
    
        public static void main(String[] args) {
            System.out.println(hasTeen(9, 99, 19));
            System.out.println(hasTeen(23, 15, 42));
            System.out.println(hasTeen(22, 23, 34));
    
        }
    
        public static boolean hasTeen(int firstNumber, int secondNumber, int thirdNumber) {
    
            ValueRange range = ValueRange.of(13, 19);
            System.out.println("*********Int validation Start ***********");
            System.out.println(range.isIntValue());
            System.out.println(range.isValidIntValue(firstNumber));
            System.out.println(range.isValidIntValue(secondNumber));
            System.out.println(range.isValidIntValue(thirdNumber));
            System.out.println(range.isValidValue(thirdNumber));
            System.out.println("**********Int validation End**************");
    
            if (range.isValidIntValue(firstNumber) || range.isValidIntValue(secondNumber) || range.isValidIntValue(thirdNumber)) {
                return true;
            } else
                return false;
        }
    }
    

    ******OUTPUT******

    true as 19 is part of range

    true as 15 is part of range

    false as all three value passed out of range

    0 讨论(0)
  • 2020-11-27 04:41

    Try:

    if (i>0 && i<100) {} 
    

    it will work at least ;)

    0 讨论(0)
提交回复
热议问题