简体   繁体   中英

Print day of week for program when input is given?

I am using switch case statements, I have to print day of the week after some given inputs in the format of year/month/day. Here is the class that is done so far and what I have done for the printDayOfWeek() method in the Date class:

class Date {

    int year, month, day;
    Date(int y, int m, int d) {
        year = y;
        month = m;
        day = d;
    }


    public int getDayOfWeek() {

        int y0 = year - (14 - month) / 12;
        int x = y0 + y0 / 4 - y0 / 100 + y0 / 400;
        int m0 = month + 12 * ((14 - month) / 12) - 2;
        int d0 = (day + x + (31 * m0) / 12) % 7;

        return d0;
    }

    public int getDaysInMonth(int month) {

        int daysInMonth = (int) (28 + (Math.floor(month / 8.0) + month) % 2 + 2 % month + 2 * Math.floor(1.0 / month));

        if (month == 2 && isLeapYear()) {
            daysInMonth += 1;
        }
        return daysInMonth;
    }


    public boolean isLeapYear() {


        boolean isLeapYear = true;

         if (year % 4 != 0) {
        isLeapYear = false;
    }
    else {  
        if (year % 100 != 0) {
            isLeapYear = true;
        }
        else if (year % 400 != 0) {
            isLeapYear = false;
        }
    else { 
            isLeapYear = true;
         }
    }
        // Task I.1

        return isLeapYear;
    }

    public void printDaysInMonth() {

        // Task I.2
    for (int i = 1; i <= 12; i++) {
        int m = getDaysInMonth(i);

        System.out.println( i + "         " + m);
    }
}

    public void printDaysInYear() {

        // Task II

    }

    public void printDayOfWeek() {

        // Task III
         int d0 = getDayOfWeek();

         switch(d0) {
             case 1:
                 System.out.println("Monday");
             case 2:
                 System.out.println("Tuesday");
             case 3:
                 System.out.println("Wednesday");
             case 4:
                 System.out.println("Thursday");
             case 5:
                 System.out.println("Friday");
             case 6:
                 System.out.println("Saturday");
             case 7:
                 System.out.println("Sunday");
         }

    }


}

What is wrong with my Prinddayofweek method? dont really know what to do at that point since am new to switch cases.

This is how your printDayOfWeek() function should look like:- as you are doing %7 Sundays will have value 0. In addition to that put break; at end of each sys out to stop the flow of execution.

public void printDayOfWeek() {

        // Task III
         int d0 = getDayOfWeek();

         switch(d0) {
             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;
         }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM