Enhanced Switch Statement in Java
The syntax of enhanced statement is slightly different from normal switch.
Source code given below :-
package com.CodeWithRK;Note :- "break" statement is not required in this syntax.
import java.util.Scanner;
public class DaysOfWeek {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the day number : ");
int dayNum = scan.nextInt();
String day = switch (dayNum) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6 -> "Saturday";
case 7 -> "Sunday";
default -> "Not found due to wrong input!";
};
System.out.println("The day is "+day);
}
}
Output :-

Comments
Post a Comment