Respuesta :
Answer:
The java program for the given scenario is shown below.
import java.util.Scanner;
import java.util.*;
public class Test
{
//declaration and initialization of array`
static String[] days = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
static int n;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
//loop runs till valid user input received
do{
System.out.print("Enter any number from 1 to 7: ");
n=sc.nextInt();
if(n<1 || n>7)
System.out.println("Invalid number.");
}while(n<1 || n>7);
//print message depending on user input
switch(n)
{
case 1: System.out.println("The day you chose is Sunday and it is a weekend."); break;
case 2: System.out.println("The day you chose is Monday and it is not a weekend."); break;
case 3: System.out.println("The day you chose is Tuesday and it is not a weekend."); break;
case 4: System.out.println("The day you chose is Wednesday and it is not a weekend."); break;
case 5: System.out.println("The day you chose is Thursday and it is not a weekend."); break;
case 6: System.out.println("The day you chose is Friday and it is not a weekend."); break;
case 7: System.out.println("The day you chose is Saturday and it is a weekend."); break;
default: break;
}
}
}
Explanation:
1. An array of size seven is declared and initialized with all the seven days in a week.
2. An integer variable is declared to store user input.
3. An object of Scanner class is created to enable user input.
4. All the above are declared outside main() and hence, declared as static.
5. Inside main, inside a do-while loop, user input is taken. The loop executes until the user enters a valid input.
6. Once a valid user input is obtained, switch case begins.
7. Inside switch case, each number from 1 to 7 is associated with a weekday.
8. The message is displayed to the user based on the corresponding user inputted number.
9. The switch case also includes default case.
10. All the code is written inside class named Test.
11. The program can be tested for any size of the array and the corresponding elements of the array.
12. The output is attached in an image.
