Check Season by Month
This program prints the season name based on month number using a switch statement.
Problem Statement
Write a Java program that prints the season name for a given month number (1-12).
Source Code
| 1 | public class SeasonSwitch { |
| 2 | public static void main(String[] args) { |
| 3 | int month = 4; |
| 4 | switch(month) { |
| 5 | case 12: case 1: case 2: |
| 6 | System.out.println("Winter - Eduinq"); |
| 7 | break; |
| 8 | case 3: case 4: case 5: |
| 9 | System.out.println("Spring - Eduinq"); |
| 10 | break; |
| 11 | case 6: case 7: case 8: |
| 12 | System.out.println("Summer - Eduinq"); |
| 13 | break; |
| 14 | case 9: case 10: case 11: |
| 15 | System.out.println("Autumn - Eduinq"); |
| 16 | break; |
| 17 | default: |
| 18 | System.out.println("Invalid month - Eduinq"); |
| 19 | } |
| 20 | } |
| 21 | } |
Program Output
Spring - Eduinq
Explanation
The program declares an integer variable month with value 4. The switch statement evaluates this variable and matches it against grouped cases. Months 12, 1, and 2 correspond to Winter; 3, 4, and 5 correspond to Spring; 6, 7, and 8 correspond to Summer; and 9, 10, and 11 correspond to Autumn. Grouping cases allows multiple values to execute the same block, making the code concise. Since month equals 4, the program prints Spring. The default case ensures that invalid inputs outside 1-12 are handled gracefully. Shows how switch can classify months into seasons using grouped cases.