Середній рівень Java
Jim White
Java Developer
if-elseif (bonus == 10000) {
System.out.println("Let's buy a car!");
} else if (bonus == 5000) {
System.out.println("Let's take a trip!");
} else if (bonus == 1000) {
System.out.println("Let's save!");
}
switch (expression) {
case value1: // if (expression == value1)
// statements
case value2: // if (expression == value2)
// statements
// use as many case statements as needed
}
char direction = 'N';
switch (direction) {
case 'E':
System.out.println("We are headed east.");
case 'S':
System.out.println("We are headed south.");
case 'W':
System.out.println("We are headed west.");
case 'N':
System.out.println("We are headed north."); // Це буде виведено
}
char direction = 'W';
switch (direction) {
case 'E':
System.out.println("We are headed east.");
case 'S':
System.out.println("We are headed south.");
case 'W':
System.out.println("We are headed west."); // Це буде виведено
case 'N':
System.out.println("We are headed north."); // Це теж буде виведено
}
break дає змогу обʼєднувати кілька випадків:
char direction = 'N';
switch (direction) {
case 'E':
case 'W':
System.out.println("We are headed east or west.");
break;
case 'S':
case 'N':
System.out.println("We are headed south or north.");
break;
}
We are headed south or north.
default можна додати в кінці switchdefault має бути останнім у switchdefault — це випадок «усі інші значення»char direction = 'z';
switch (direction) {
case 'E':
case 'W':
System.out.println("To east or west.");
break;
case 'S':
case 'N':
System.out.println("To south or north.");
break;
default:
System.out.println("We are lost.");
}
We are lost.
char character = 'A';
if (character == 'A') {
System.out.println("It's A");
} else if (direction =='B') {
System.out.println("It's B");
} else {
System.out.println("Unknown");
}
int, byte, short, char або Stringchar character = 'A';
switch (character) {
case 'A':
System.out.println("It's A");
break;
case 'B':
System.out.println("It's B");
break;
default:
System.out.println("Unknown");
}
Середній рівень Java