program wypisujący liczbę dni w miesiącu

0

Mam zwrócić liczbę dni w miesiącu, ale program nie dochodzi do switcha, gdzies mam blad ktorego nie moge zlokalizowac.

BTW Jest jakis dzial dla very newbie, czy jesli postuje tutaj to jest ok?

public class NumberOfDaysInMonth {

    public static boolean isLeapYear(int year) {
        if (year < 1 || year > 9999) {
            return false;
        } else {
            return ((year % 4 == 0 && year != 100) || year % 400 == 0);
        }
    }

    public static int getDaysinMonth(int month, int year) {
        if (month < 1 || month > 12 || year < 1 || year > 9999) {
            return -1;
        } else {
            if (month == 2 && isLeapYear(year)) {
                return 29;
            } else {
                switch (month) {
                    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                        return 31;
                        break;
                    case 2:
                        return 28;
                    break;
                    case 4: case 6: case 9: case 11:
                        return 30;
                    break;
                }
            }
        }
    }
}
0

Niepotrzebne break. Brakuje Ci jednego return w metodzie getDaysinMonth(), ale proponowałbym po prostu zadeklarować wartość default w instrukcji switch

0
public class NumberOfDaysInMonth {

    public static boolean isLeapYear(int year) {
        if (year < 1 || year > 9999) {
            return false;
        } else {
            return ((year % 4 == 0 && year != 100) || year % 400 == 0);
        }
    }

    public static int getDaysinMonth(int month, int year) {
        if (month < 1 || month > 12 || year < 1 || year > 9999) {
            return -1;
        } else {
            if (month == 2 && isLeapYear(year)) {
                return 29;
            } else {
                switch (month) {
                    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                        return 31;
                    case 2:
                        return 28;
                    case 4: case 6: case 9: case 11:
                        return 30;
                    default :
                        return -1;
                }
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 1; i <= 12; i++) {
            System.err.println(getDaysinMonth(i, 2019));
        }
    }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1