ADVERTISEMENT
ADVERTISEMENT

Java - Switch Case Statement Example Code - Print Weekdays.


IncludeHelp 14 July 2016

In this code example we will learn how to print weekday’s name according to given weekday’s number. In this program we will read weekday’s number between 0 to 6 and print weekday’s name.

For example - User input is 0 then program will print Sunday, if 1 then program will print Monday and so on.

Java Code Snippet - Switch Case Statement Example Code to Print Weekdays

//Java - Switch Case Statement Example Code - Print Weekdays.
import java.util.Scanner;

public class SwitchCaseExample{	
	  public static void main(String args[]){
		  int day;
		  Scanner SC=new Scanner(System.in);
		  
		  System.out.print("Enter a weekday number (0 to 6): ");
		  day=SC.nextInt();
		  
		  if(day<0 || day>6){
			  System.out.println("Invalid weekday number.");
			  System.exit(0);
		  }
		  
		  //print weekday name according to give value
		  switch(day){
		  		case 0:	
		  			System.out.println("Sunday");
		  			break;
		  		case 1:	
		  			System.out.println("Monday");
		  			break;
		  		case 2:	
		  			System.out.println("Tuesday");
		  			break;
		  		case 3:	
		  			System.out.println("Wednesday");
		  			break;
		  		case 4:	
		  			System.out.println("Thursday");
		  			break;
		  		case 5:	
		  			System.out.println("Friday");
		  			break;
		  		case 6:	
		  			System.out.println("Saturday");
		  			break;		  			
		  		default:
		  			System.out.println("Invalid");
		  			break;		  			
		  }
		  
	  }
}
    First Run:
    Enter a weekday number (0 to 6): 8
    Invalid weekday number.

    Second Run:
    Enter a weekday number (0 to 6): 5
    Friday

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.