-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheEnhancedSwitchStatement.java
More file actions
70 lines (60 loc) · 2.6 KB
/
TheEnhancedSwitchStatement.java
File metadata and controls
70 lines (60 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.Scanner;
public class TheEnhancedSwitchStatement {
public static void main(String[] args) {
// enhanced switch statement= used to replace many else if statement
// it's a java 14 feature
/* String day = "Sunday";
if(day.equals("Monday")){
System.out.println("Today is a weekday");
}else if (day.equals("Tuesday")){
System.out.println("Today is a weekday");
}
else if(day.equals("Wednesday")){
System.out.println("Today is a weekday");
}
else if (day.equals("Thursday")){
System.out.println("Today is a weekday");
} else if (day.equals("Friday")) {
System.out.println("Today is a weekday");
} else if (day.equals("Saturday")) {
System.out.println("it is the Weekend");
} else if (day.equals("Sunday")) {
System.out.println("it is the Weekend");
}
else{
System.out.println(day+"is not a valid day");
}
THIS ALL CODE CAN BE REPLACED WITH THE ENHANCED SWITCH STATEMENT
*/
// ENHANCED SWITCH STATEMENT
System.out.print("enter the day of the Week:");
Scanner scanner= new Scanner(System.in);
String day=scanner.nextLine();
scanner.close();
switch(day){
case "Monday"-> System.out.println("it is a weekday");
case "Tuesday"-> System.out.println("its a weekday");
case "Wednesday"-> System.out.println("its a weekday");
case "Thursday"-> System.out.println("its a weekday");
case "Friday"-> {
// curly braces allow you to enter
System.out.println("its a weekday");
}
case "Saturday"-> System.out.println("its a weekday");
case "Sunday"-> System.out.println("its a weekday");
default ->System.out.println(day+" is not a day");
}
// the default block is the same as the else statement
// the above program can be written as
switch(day){
case "Monday","Tuesday","Wednesday","Thursday","Friday"->
System.out.println("out put of the second advanced Switch satement: \n" +
"its a weekday");
case "Saturday","Sunday"->
System.out.println("out put of the second advanced Switch satement: \n" +
"its a weekday");
default ->
System.out.println(day+" is not a day");
}
}
}