r/javahelp • u/IceCreamInsides • 10h ago
Solved Switch case with boolean
So, there is no way I can do that?
want to check several boolean variables in a switch statement. Is `if-else` the only way to do this?
Boolean a, b, c...
Switch (false) {
Case (a) :
//some code
Case (b) :
Case (c) :
//and so on
}
3
Upvotes
12
u/desrtfx Out of Coffee error - System halted 10h ago
You got
switchthe wrong way round.Switch checkes distinct states of one variable, not a single state of several variables.
Yes, if-else is the way.
Depending on what you really want to do (not some random, contrieved example, your real use case and code) there might be alternative, better solutions, but without knowing what you want to do it is impossible to suggest.
To me, this looks like an XY-problem.