Java Program to Find Season Using Switch Statement

This is a Java Program to Find Season Using Switch Statement. The switch statement is Java’s multi way branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression.

We take the month as an input. The value of the input is compared with each of the literal values in the case statements. If a match is found, the code sequence following that case statement is executed. If none of the constants matches the value of the expression, then the default statement is executed.

Here is the source code of the Java Program to Find Season Using Switch Statement. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Switch_Demo
  3. {
  4.  public static void main(String args[])
  5.  {
  6.     String season;
  7.     System.out.println("Enter any month(1 to 12)");
  8.     Scanner s = new Scanner(System.in);
  9.     int entry = s.nextInt();
  10.       switch (entry)
  11.         {
  12.             case 12:
  13.             case 1:
  14.             case 2:
  15.             season = "Winter";
  16.             break;
  17.             case 3:
  18.             case 4:
  19.             case 5:
  20.             season = "Spring";
  21.             break;
  22.             case 6:
  23.             case 7:
  24.             case 8:
  25.             season = "Summer";
  26.             break;
  27.             case 9:
  28.             case 10:
  29.             case 11:
  30.             season = "Autumn";
  31.             break;
  32.             default:
  33.             season = "Bogus Month";
  34.        }
  35.         System.out.println("The entered month is in the " + season + ".");
  36.    }
  37. }

Output:

$ javac Switch_Demo.java
$ java Switch_Demo
 
Enter any month(1 to 12)
3
The entered month is in the Spring.

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.