Java Tutorial · Chapter 8 of 12
Java Switch
Select from fixed options.
Modern Java supports switch with arrow cases in many classroom environments.
Use switch for menus and discrete values.
Switch sample
Try it Yourself »
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Choose 1 or 2: ");
int choice = input.nextInt();
switch (choice) {
case 1 -> System.out.println("Addition selected");
case 2 -> System.out.println("Subtraction selected");
default -> System.out.println("Invalid choice");
}
}
}
Classroom Tips
- Include a default branch.