Skip to content

Commit 6c88336

Browse files
committed
feat: Implement month name mapping using switch-case based on user input
🧠 Logic: - Program prompts the user to enter a number (1–12) to identify the corresponding month. - Utilizes a `switch` statement to match the input with the correct month abbreviation (e.g., 1 → Jan, 12 → December). - Includes a `default` case to handle invalid inputs outside the 1–12 range, enhancing robustness. - Demonstrates practical use of switch-case for value-to-string mapping in calendar-based logic. Signed-off-by: Somesh diwan <[email protected]>
1 parent 54c998e commit 6c88336

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

Section7ConditionalStatements/src/SwitchCasesAB2.java

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,73 +9,59 @@ public static void main(String[] args) {
99

1010
int Number = read.nextInt();
1111

12-
switch (Number)
13-
{
14-
case 1:
15-
{
12+
switch (Number) {
13+
case 1: {
1614
System.out.println("Jan");
1715
break;
1816
}
19-
case 2:
20-
{
17+
case 2: {
2118
System.out.println("Feb");
2219
break;
2320
}
24-
case 3:
25-
{
21+
case 3: {
2622
System.out.println("March");
2723
break;
2824
}
29-
case 4:
30-
{
25+
case 4: {
3126
System.out.println("April");
3227
break;
3328
}
34-
case 5:
35-
{
29+
case 5: {
3630
System.out.println("May");
3731
break;
3832
}
39-
case 6:
40-
{
33+
case 6: {
4134
System.out.println("June");
4235
break;
4336
}
44-
case 7:
45-
{
37+
case 7: {
4638
System.out.println("July");
4739
break;
4840
}
49-
case 8:
50-
{
41+
case 8: {
5142
System.out.println("Aug");
5243
break;
5344
}
54-
case 9:
55-
{
45+
case 9: {
5646
System.out.println("Sep");
5747
break;
5848
}
59-
case 10:
60-
{
49+
case 10: {
6150
System.out.println("Oct");
6251
break;
6352
}
64-
case 11:
65-
{
53+
case 11: {
6654
System.out.println("Nov");
6755
break;
6856
}
69-
case 12:
70-
{
57+
case 12: {
7158
System.out.println("December");
7259
break;
7360
}
74-
default:
75-
{
61+
default: {
7662
System.out.println("Month is not valid try another number to check month");
7763
break;
7864
}
7965
}
8066
}
81-
}
67+
}

0 commit comments

Comments
 (0)