Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Edwin Torres
Edwin Torres

Posted on

     

Using a Switch for Days of the Week

Theswitch statement is a useful selection statement when there are many values that require different logic.

Here is a program that asks the user to enter a day number (1-7) and outputs the full name of that day of the week.

First, import the Scanner class (for user input), declare the class name, and declare the main method:

importjava.util.Scanner;publicclassDays{publicstaticvoidmain(String[]args){
Enter fullscreen modeExit fullscreen mode

Next, declare a Scanner variable and create the object. This object will retrieve user input later:

Scannerin=newScanner(System.in);
Enter fullscreen modeExit fullscreen mode

Declare a variable to store the user input:

intdayNum;
Enter fullscreen modeExit fullscreen mode

Ask the user to enter a number:

System.out.print("Enter a day number (1-7): ");
Enter fullscreen modeExit fullscreen mode

Use the Scanner object to retrieve the user input. Note that the program will wait here until the user types a value and presses Enter:

dayNum=in.nextInt();
Enter fullscreen modeExit fullscreen mode

Create aswitch statement that switches on the dayNum variable:

switch(dayNum){
Enter fullscreen modeExit fullscreen mode

Inside theswitch statement, add cases for each day. For example, the value1 will output Monday:

case1:System.out.println("Monday");break;
Enter fullscreen modeExit fullscreen mode

Here are the other cases. Note that case5 also outputs TGIF:

case2:System.out.println("Tuesday");break;case3:System.out.println("Wednesday");break;case4:System.out.println("Thursday");break;case5:System.out.println("Friday");System.out.println("TGIF!");break;case6:System.out.println("Saturday");break;case7:System.out.println("Sunday");break;
Enter fullscreen modeExit fullscreen mode

The last case is a default case. This case occurs when the dayNum value has a number outside the range 1-7:

default:System.out.println("Invalid day number.");break;
Enter fullscreen modeExit fullscreen mode

Finally, add the closing curly brackets for theswitch statement, main method, and class:

}}}
Enter fullscreen modeExit fullscreen mode

Here is the complete program:

importjava.util.Scanner;publicclassDays{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);intdayNum;System.out.print("Enter a day number (1-7): ");dayNum=in.nextInt();switch(dayNum){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");System.out.println("TGIF!");break;case6:System.out.println("Saturday");break;case7:System.out.println("Sunday");break;default:System.out.println("Invalid day number.");break;}}}
Enter fullscreen modeExit fullscreen mode

Thanks for reading. 😃

Follow me on Twitter@realEdwinTorres for more programming tips and help.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Department Chief Engineer @MITREcorp. CS professor @monmouthu. Proud husband and father ❤️. My tweets are my own.
  • Location
    NJ
  • Education
    Doctor of Engineering, George Washington University
  • Work
    Principal Software Engineer at MITRE
  • Joined

More fromEdwin Torres

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp