boolean condition verification

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?

Upvotes: 0

Views: 75

Answers (2)

Meghana Viswanath
Meghana Viswanath

Reputation: 43

if ((1 to 3) contains num) {
  print(num)
}

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234715

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).

Upvotes: 1

Related Questions