Question #3526

5) Write a program to print the days in a week if 0=Sunday and 1=Monday and so on till Saturday by using switch-case.

Expert's answer

Create the file "DayOfWeek.java" with the following
contents:

//////////////////////////////
import
java.util.Scanner;

public class DayOfWeek
{
public static void
main (String[] args)
{
Scanner input = new Scanner(
System.in);
int day;
boolean flag;


System.out.print("Please enter a day number (0-6): ");
day =
input.nextInt();

switch (day) {
case
0:
System.out.printf("Sunday\n");
break;

case
1:
System.out.printf("Monday\n");
break;

case
2:
System.out.printf("Tuesday\n");
break;

case
3:
System.out.printf("Wednesday\n");
break;

case
4:
System.out.printf("Thursday\n");
break;

case
5:
System.out.printf("Friday\n");
break;

case
6:
System.out.printf("Saturday\n");
break;

default:
System.out.printf("Incorrect
number!\n");
break;

};

}
}
//////////////////////////



Compile it with


javac DayOfWeek.java

and run with

java DayOfWeek
LATEST TUTORIALS
APPROVED BY CLIENTS