Середній рівень Java
Jim White
Java Developer
if (score >= 90) {
System.out.println("Excellent!");
}
if (score >= 70) {
System.out.println("Good effort.");
}
if (score < 70) {
System.out.println("Keep trying!");
}
Якщо score >= 70, буде надруковано і Excellent!, і Good effort.
➡ Кілька окремих if швидко ускладнюють код
int score = 80;
if (score >= 90) {
System.out.println("Excellent!");
} else if (score >= 70) {
// Виконається лише цей код
System.out.println("Good effort.");
} else {
System.out.println("Keep trying!");
}
Good effort.
int score = 60;
if (score >= 90) {
System.out.println("Excellent!");
} else if (score >= 70) {
System.out.println("Good effort.");
} else { // Виконається лише цей код
System.out.println("Keep trying!");
}
Keep trying!
int score = 95;
if (score >= 90) { // Виконається лише цей код
System.out.println("Excellent!");
} else if (score >= 70) {
System.out.println("Good effort.");
} else {
System.out.println("Keep trying!");
}
Excellent!
// Скільки завгодно
if (score >= 95) {
System.out.println("Excellent job, well done!");
} else if (score >= 90) {
System.out.println("Excellent!");
} else if (score >= 80) {
System.out.println("Great job.");
} else if (score >= 70) {
System.out.println("Good effort.");
} else {
System.out.println("Keep trying!");
}
// Стежте за порядком
int score = 95;
if (score > 70) { // Виконується цей код
System.out.println("Good effort.");
} else if (score >= 90) {
// Це теж коректно
System.out.println("Excellent!");
} else {
System.out.println("Keep trying!");
}
Good effort.
int age = 33;
if (age < 20) {
System.out.println("You are a teenager");
} else if (age < 30) {
System.out.println("You are in your twenties");
} else if (age < 40) { // Виконається лише цей блок коду
System.out.println("You are in your thirties");
} else {
System.out.println("You are older than 39");
}
You are in your thirties
Середній рівень Java