NameOfDay.java


Below is the syntax highlighted version of NameOfDay.java from §1.3 Conditionals and Loops.


/******************************************************************************
 *  Compilation:  javac NameOfDay.java
 *  Execution:    java NameOfDay day
 *
 *  Converts a day of the week (0 to 6) to the corresponding name (Sunday
 *  to Saturday).
 *
 *  % java NameOfDay 0
 *  Sunday
 *
 *  % java NameOfDay 2
 *  Tuesday
 *
 ******************************************************************************/

public class NameOfDay {
    public static void main(String[] args) {
        int day = Integer.parseInt(args[0]);
        switch (day) {
            case 0:  System.out.println("Sunday");      break;
            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;
            case 6:  System.out.println("Saturday");    break;
            default: System.out.println("invalid day"); break;
        }
    }
}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:12:31 EDT 2022.