Consider the following table:
State a java program that use Switch structure to print one of the above statements depending on the
selected day. Write comments to explain your code.
day statement
Monday " Monday is the first day of the week !"
Tuesday " Tuesday is the second day of the week !"
Wednesday "Wednesday is the third day of the week !"
Thursday "Thursday is the fourth day of the week !"
Friday “Friday is the fourth day of the week !"
Saturday “Saturday is the fifth day of the week !"
Sunday “Sunday is the sixth day of the week !"
Any other day “ Unknown day !"
import java.util.Scanner; //import Class Scanner
public class WeekDays {
public static void main(String[] args) {
Scanner scan= new Scanner(System.in); //Create object scanner
System.out.print("Choose one day of the week: "); //Print massage
String weekDay= scan.nextLine(); //variable that will get word from user
switch(weekDay){
case "Monday": //first case
System.out.println("Monday is the first day of the week !");//Print massage if variable weekDay=Monday
break;
case "Tuesday": //second case
System.out.println("Tuesday is the second day of the week !");
break;
case "Wednesday":
System.out.println("Wednesday is the third day of the week !");
break;
case "Thursday":
System.out.println("Thursday is the fourth day of the week");
break;
case "Friday":
System.out.println("Friday is the fifth day of the week !");
break;
case "Saturday":
System.out.println("Saturday is the fifth day of the week !");
break;
case "Sunday":
System.out.println("Sunday is the sixth day of the week !");
break;
default: //Case when no option is suitable
System.out.println("Unknown day !");
}
}
}
Comments
Leave a comment