-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathSwitchExpressionDemo.java
More file actions
50 lines (42 loc) · 1.44 KB
/
SwitchExpressionDemo.java
File metadata and controls
50 lines (42 loc) · 1.44 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
public class SwitchExpressionDemo {
public static void main(String[] args) {
String fruitName = "Banana";
switch (fruitName) {
case "Banana", "Apple" -> System.out.println("$ 1.0 charged");
case "Grapes" -> System.out.println("$ 2.0 charged");
case "Pineapple" -> System.out.println("$ 2.5 charged");
case "Mango" -> System.out.println("$ 3.0 charged");
default -> System.out.println("Pick a valid fruit");
}
String output = switch (fruitName) {
case "Banana", "Apple" -> "$ 1.0 charged";
case "Grapes" -> "$ 2.0 charged";
case "Pineapple" -> "$ 2.5 charged";
case "Mango" -> "$ 3.0 charged";
default -> "Pick a valid fruit";
};
String day = "FRIDAY";
int numLetters = switch (day) {
case "MONDAY", "FRIDAY", "SUNDAY" -> {
System.out.println(6);
yield 6;
}
case "TUESDAY" -> {
System.out.println(7);
yield 7;
}
case "THURSDAY", "SATURDAY" -> {
System.out.println(8);
yield 8;
}
case "WEDNESDAY" -> {
System.out.println(9);
yield 9;
}
default -> {
System.out.println("Invalid Day");
yield 0;
}
};
}
}