-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitch.java
More file actions
70 lines (65 loc) · 1.94 KB
/
Switch.java
File metadata and controls
70 lines (65 loc) · 1.94 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;
class Switch{
public static void main(String[] args){
Scanner obj = new Scanner(System.in);
int day = obj.nextInt();
String dayString;
String dayType;
switch(day){
case 1:
dayString = "Monday";
break;
case 2:
dayString = "Tuesday";
break;
case 3:
dayString = "Wednesday";
break;
case 4:
dayString = "Thursday";
break;
case 5:
dayString = "Friday";
break;
case 6:
dayString = "Saturday";
break;
case 7:
dayString = "Sunday";
break;
default:
dayString="Invalid day";
}
switch(day){
case 1:
case 2:
case 3:
case 4:
case 5:
dayType = "Weekday";
break;
case 6:
case 7:
dayType = "Weekend";
break;
default:
dayType = "Invalid day";
}
System.out.println(dayString+" is a "+dayType);
//switch expressions and case labels as "expressions"
int x = 2;
switch(x+1){ // variables can be used in switch expressions
case 1:
System.out.println(1);
break;
case 1+1: // variables should not be used in case labels
System.out.println(2); // only constants and literals
break;
case 2+1:
System.out.println(3);
break;
default:
System.out.println("Default");
}
}
}