In this lab, you will learn how to use switch
statements in Java. The switch
statement provides an alternative to the if-else
structure when dealing with multiple possible values of a variable. This control structure can make your code more readable when you have a set of predefined values to check. You will write a program that utilizes the switch
statement to make decisions based on user input.
By the end of this lab, you will be able to:
switch
statement in Java.switch
statement to handle multiple cases in a program.switch
statement to evaluate user input.switch
statements.cd ~
SwitchExample.java
:nano SwitchExample.java
import java.util.Scanner;
public class SwitchExample {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a number between 1 and 7
System.out.print("Enter a number (1-7) to select a day of the week: ");
int day = scanner.nextInt();
// Switch statement to determine the day of the week
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid input! Please enter a number between 1 and 7.");
break;
}
}
}
CTRL + O
, then hit Enter
to confirm the filename.CTRL + X
.javac
command:javac SwitchExample.java
SwitchExample.class
. Verify this by running:ls
java
command:java SwitchExample
Enter a number (1-7) to select a day of the week: 3
Tuesday
SwitchExample.java
file again using Nano:nano SwitchExample.java
switch
statement:System.out.print("Enter a number (1-12) to select a month of the year: ");
int month = scanner.nextInt();
switch (month) {
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Invalid input! Please enter a number between 1 and 12.");
break;
}
javac SwitchExample.java
java SwitchExample
switch
statement to prompt the user to first choose between the two categories, and then proceed to either the day or month selection:System.out.print("Enter 1 for day selection, 2 for month selection: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
// Ask for day
System.out.print("Enter a number (1-7) to select a day of the week: ");
int day = scanner.nextInt();
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid input! Please enter a number between 1 and 7.");
break;
}
break;
case 2:
// Ask for month
System.out.print("Enter a number (1-12) to select a month of the year: ");
int month = scanner.nextInt();
switch (month) {
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Invalid input! Please enter a number between 1 and 12.");
break;
}
break;
default:
System.out.println("Invalid input! Please choose 1 or 2.");
break;
}
.class
files:rm *.class
In this lab, you learned how to use the switch
statement to handle multiple conditions in Java. You explored how switch
is used as an alternative to if-else
for checking multiple values. You implemented a program that allowed users to choose a day of the week or a month of the year, and expanded it to include more user input options. The switch
statement is a powerful tool for simplifying decision-making logic when you have multiple discrete values to evaluate.