Match more than one condition in a junior

Asked

Viewed 325 times

1

Is it possible to combine more than one condition in the first one? Example:

boolean existe = pessoa tem 23 ou pessoa tem 22 ? true : false ;

I’m trying but I can’t.

2 answers

8


Would that be?

boolean existe = (idade == 22 || idade == 23);
  • Thank you very much, that’s right.

  • 1

    Taking advantage if what you want to return is boolean do not need the if ternary just write the condition. I use if ternary when I want to return some value that is not boolean type: (idade == 22 || idade == 23) ? "Sim": "Não";

-2

It is possible yes:

If you necessarily want to use the ternary operator (as asked):

boolean existe = pessoa.getIdade() == 23? true : pessoa.getIdade() == 22? true : false ;

But in this example it would not be necessary to use this operator:

boolean existe = pessoa.getIdade() == 23 || pessoa.getIdade() == 22;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.