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.
public class Switch_Demo
public static void main(String args)
System.out.println("Enter any month(1 to 12)");
Scanner s = new Scanner(System.in);
int entry = s.nextInt();
season = "Winter";
season = "Spring";
season = "Summer";
season = "Autumn";
season = "Bogus Month";
System.out.println("The entered month is in the " + season + ".");
$ 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.