⇒ Write a Java program to input a character from the user and display the days of the week according to the users choice. (For Sunday ,let the character be S )
Answers
Answer:
public static void main(String s[])
{
int day = 2;
switch(day)
{
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;
default:
System.out.println("Weekend");
break;
}
}
}
Program:
import java.util.*;
public class MyClass
{
public static void main(String args[])
{
Scanner Sc = new Scanner(System.in);
char ch;
System.out.print("Enter S for Sunday, M for Monday, T for Tuesday, W for Wednesday, H for Thursday, F for Friday, and A for Saturday\nPlease enter your choice : ");
ch = Sc.next().charAt(0);
switch(ch)
{
case 'S':
System.out.print("Sunday");
break;
case 'M':
System.out.print("Monday");
break;
case 'T':
System.out.print("Tuesday");
break;
case 'W':
System.out.print("Wednesday");
break;
case 'H':
System.out.print("Thursday");
break;
case 'F':
System.out.print("Friday");
break;
case 'A':
System.out.print("Saturday");
break;
default:
System.out.print("Invalid Input");
}
}
}
Output:
Enter S for Sunday, M for Monday, T for Tuesday, W for Wednesday, H for Thursday, F for Friday, and A for Saturday
Please enter your choice : S
Sunday