Tuesday, 18 July 2017

3. Implement a Java program which prints the season name respective to its month number using if else and switch statements

import java.util.*;   
  public class IfElseDemo   
  {   
    public static void main(String args[])   
    {   
       System.out.print("Which Seasons: ");   
       Scanner in = new Scanner(System.in);   
       int month = in.nextInt();   
       String M=" ";   
       if(month==12||month==1||month==2)   
       M="Winter";   
       else if(month==3||month==4||month==5)   
       M="Spring";   
       else if(month==6||month==7||month==8)   
       M="Summer";   
       else if(month==9||month==10||month==11)   
       M="Autumn";   
       else if(month>12 ||month <1)   
       {   
         System.out.print("Invalid Entry");   
         return;   
       }   
               
             switch(M)  
             {       
                     case "Winter":  
                     System.out.print("Season: Winter");  
                     break;  
                       
                     case "Spring":  
                     System.out.print("Season: Spring");  
                     break;  
                       
                     case "Summer":  
                     System.out.print("Season: Summer");  
                     break;  
                       
                     case "Autumn":  
                     System.out.print("Season: Autumn");  
                     break;  
                       
             }   
    }   
  }   
   


Output:


No comments:

Post a Comment