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

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

Hoping for something more elegant than

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

    if you are using Spring data you can also use the Range object from Spring.

    range = new org.springframework.data.domain.Range(3, 8); range.contains(5) will return true.

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

    You could add spacing ;)

    if (i > 0 && i < 100) 
    
    0 讨论(0)
  • 2020-11-27 04:28
    if ( 0 < i && i < 100)  
    
    if ( 'a' <= c && c <= 'z' )
    
    0 讨论(0)
  • 2020-11-27 04:28

    if(i <= 0 || i >=100)

    It will work.

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

    I think

    if (0 < i && i < 100) 
    

    is more elegant. Looks like maths equation.

    If you are looking for something special you can try:

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

    at least it uses library.

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

    If you're looking for something more original than

    if (i > 0 && i < 100)
    

    you can try this

    import static java.lang.Integer.compare;
    ...
    if(compare(i, 0) > compare(i, 100))
    
    0 讨论(0)
提交回复
热议问题