Wed Oct 24, 2012 10:13 pm
switch(1)
{
}
int switchX = 1;
switch (switchX) {
case 1:
System.out.println("output is 1");
break;
case 2:
System.out.println("output is 2");
break;
case 3:
System.out.println("output is 3");
break;
case 4:
System.out.println("output is 4");
break;
case 5:
System.out.println("output is 5");
break;
default:
System.out.println(" no case found switchX");
}
output is 1
int switchX = 1;
switch (switchX) {
default:
System.out.println(" no case found switchX");
case 1:
System.out.println("output is 1");
break;
case 2:
System.out.println("output is 2");
break;
case 3:
System.out.println("output is 3");
break;
case 4:
System.out.println("output is 4");
break;
case 5:
System.out.println("output is 5");
break;
}
int switchX = 1;
switch (switchX) {
case 1:
System.out.println("output is 1");
case 2:
System.out.println("output is 2");
break;
case 3:
System.out.println("output is 3");
break;
case 4:
System.out.println("output is 4");
break;
case 5:
System.out.println("output is 5");
break;
default:
System.out.println(" no case found switchX");
}
output is 1
output is 2
byte byteA = 43;
switch (byteA) {
case 43:
System.out.println("output is 43");
case 44:
System.out.println("output is 44");
break;
default:
System.out.println(" no case found");
}
output is 43
output is 44
byte byteA = 43;
switch (byteA) {
case 443:
System.out.println("output is 443");
case 424:
System.out.println("output is 424");
break;
default:
System.out.println(" no case found");
}
float x = 3.4f;
switch ((int) x) {
case (int) 3.2f:
System.out.println("3.2 Grade");break;
case (int) 4.0f:
System.out.println("4 Grade");break;
}
3.2 Grade
// switch on integer
for (int x = 2; x <= 7; x++) {
switch (x) {
case 1:
System.out.println("X equals 1");
break;
case 2:
System.out.println("X equals 2");
break;
case 3:
System.out.println("X equals 3");
break;
case 4:
System.out.println("X equals 4");
break;
case 5:
case 6:
System.out.println("X equals 6");
break;
default:
System.out.println("No case of for number: " + x);
}
}
X equals 2
X equals 3
X equals 4
X equals 6
X equals 6
No case of for number: 7
public class SwitchExample {
public enum Colors {
RED, GREEN, BLUE;
}
public static void main(String[] args) {
// Switch on enum
Colors myColor = Colors.GREEN;
switch (myColor) {
case BLUE:
System.out.println("Enum value equal BLUE");
break;
case RED:
System.out.println("Enum value equal RED");
break;
case GREEN:
System.out.println("Enum value equal GREEN");
break;
}
}
}
Enum value equal GREEN
// switch on character
char color = 'R';
switch (color) {
case 'R':
System.out.println("This is Red");
break;
case 'G':
System.out.println("This is Green");
break;
case 'B':
System.out.println("This is Blue");
break;
}
This is Red
String postalCode = "11232";
switch (postalCode) {
case "11123":
System.out.print("Your city is x");
break;
case "33423":
System.out.print("Your city is y");
break;
case "11232":
System.out.print("Your city is z");
break;
default:
throw new IllegalArgumentException("Don't know your x");
}
Your city is z
Tue Jun 11, 2013 10:34 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.