0
Can a case for a switch statement be a condition?
For example if there's an int variable named "X" can the case of a switch statement for X be "Case X>2"?
2 Respuestas
+ 1
As in
https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.11
"The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type (§8.9), or a compile-time error occurs."
so no, it's a pretty nice idea, but switching boolean wouldn't work.
0
no, but ..
int x, y; String st="";
y = 38;
x = y<10?10: y<20?20: y<30?30: y<40?40: y<50?50: -1;
switch( x ) {
case 10: st="Poor"; break;
case 20: st="Fair"; break;
case 30: st="Good"; break;
case 40: st="Very Good"; break;
case 50: st="Excellent"; break;
}
System.out.println(st);