0

I want to do boolean check like this.This doesn't work. But my idea is like this

if(num==(1,2,3)){
        println (num)
    }

or

if(num==(1|2|3)){
        println (num)
    }

How can I do this?

Tonechas
  • 13,398
  • 16
  • 46
  • 80
  • 4
    Bad tags, lack of basic knowledge. If you are aiming for java, do check official Java SE documentation and tutorials. – ForInfinity Mar 17 '17 at 11:38
  • Possible duplicate of [Javascript: The prettiest way to compare one value against multiple values](http://stackoverflow.com/questions/9121395/javascript-the-prettiest-way-to-compare-one-value-against-multiple-values) – Tom Mar 17 '17 at 11:38
  • The dupe is Javascript, but since it shows the general approach and you tagged spammed your question, it should be fine. – Tom Mar 17 '17 at 11:39
  • You shouldn't tag Python and Scala in your question if you are looking for java code. – Lexi Mar 17 '17 at 11:40

2 Answers2

1

num==(1,2,3) is not a valid expression in Java. (Although in C and C++ it is equivalent to num == 3).

You need to write if (num == 1 || num == 2 || num == 3).

If num is an integral type, you could use if (num >= 1 && num <= 3).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0
if ((1 to 3) contains num) {
  print(num)
}