Computer Science, asked by ankitdhara276, 5 months ago

write a Java Program which will accept one number between 1 to 7 and print using switch case the corresponding day of the week for invalid input print invalid input​

Answers

Answered by Ishu060
3

Answer:

(i am writing the program with blueJ friendly envirnoment)

//program starts

import java.util.*;

class DaysOfWeeks

{

public static void main();

{

Scanner sc=new Scanner(System.in);

int n;

System.out.println("Enter the number between 1-7");

n = sc.nextInt();

switch(n)

{

case 1: System.out.println("Day corresponding to 1 is Sunday");

break;

case 2: System.out.println(" Day corresponding to 2 is Monday");

break;

case 3: System.out.println(" Day corresponding to 3 is Tuesday");

break;

case 4: System.out.println(" Day corresponding to 4 is Wednesday");

break;

case 5: System.out.println(" Day corresponding to 5 is Thursday");

break;

case 6: System.out.println(" Day corresponding to 6 is Friday");

break;

case 7: System.out.println(" Day corresponding to 8 is Saturday");

break;

default: System.out.println("Invalid");

}

}

}

Explanation:

in the first line i have imported the Scanner class.

After the main i have created an object for Scanner Class.

Then I have initialized a variable and told the user to input the value between 1-7.

Then I have started the switch command which works on the input given by user.

If user gave input as 1 the case 1 will work

If user gave input as 2 then case 2 will work

Is user gave input as 3 then case 3 will work

and so on...

if user gave any other input other than 1,2,3,4,5,6,7 then the default case will work which is at the last.

Break command is necessary between every case because if not given the program will execute all commands below it.

For example if break is not given and user input value as 3 then case 3 will run and all the cases below it will also run.

So break is necessary

Hope it helps

Answered by TheUntrustworthy
116

import java.util.*;

class Hello

{

public static void main (string args [])

{

Scanner sc=new Scanner(System .in);

int d;

System.out.println("Enter a day no.");

d=sc.nextInt();

switch=(d)

{

Case1: System.out.println("Monday");

break;

Case2: System.out.println("Tuesday");

break;

Case3: System.out.println("Wednesday");

break;

Case4: System.out.println("Thursday");

break;

Case5: System.out.println("Friday");

break;

Case6: System.out.println("Saturday");

break;

Case7: System.out.println("Sunday");

break;

default: System.out.println("Wrong Day");

}

}

}

Attachments:
Similar questions