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.
import java.util.Scanner;
public class Switch_Demo
{
public static void main(String args[])
{
String season;
System.out.println("Enter any month(1 to 12)");
Scanner s = new Scanner(System.in);
int entry = s.nextInt();
switch (entry)
{
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
case 11:
season = "Autumn";
break;
default:
season = "Bogus Month";
}
System.out.println("The entered month is in the " + season + ".");
}
}
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
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Apply for Java Internship
- Check Java Books
- Apply for Computer Science Internship
- Practice Programming MCQs
- Practice BCA MCQs