How to add printing of number of days in a year also for leap years?

Viewed 42

I am having trouble adding it so that my program prints out the days in a certain year. What's messing me up is the leap years. Don't want to scrap it.

I am confused and was trying to put it together by myself and should have asked for help. wanted to know if there was a way to fix it or if i had to scrap it and start all over again. My problem so far is designating the days. since i'm not sure if i should just separate the days by the month over just have the total for each year and do that for the leap year as well.

`
    if (month == 1){
                  month1 = "January"; 
                        daysInMonth = 31;
                    }
                    else if (month == 2 ){
                        month1 = "February";
                        daysInMonth = 28;
                    }
                    else if (month == 3) {
                     month1 = "March";
                        daysInMonth = 31;
                    }
                    else if (month == 4){
                     month1 = "April";
                        daysInMonth = 30;
                    }
                    else if (month == 5){
                        month1 = "May";
                        daysInMonth = 31;
                    }
                    else if (month == 6){
                        month1 = "June";
                        daysInMonth = 30;
                    }
                    else if (month == 7){
                        month1 = "July";
                        daysInMonth = 31;
                    }
                    else if (month == 8){
                        month1 = "August";
                        daysInMonth = 30;
                    }
                    else if (month == 9 ){
                        month1 = "September";
                        daysInMonth = 30;
                    }
                    else if (month == 10){
                        month1 = "October";
                        daysInMonth = 31;
                    }
                    else if (month == 11){
                        month1 = "November";
                        daysInMonth = 30;
                    }
                    else if (month == 12){
                        month1 = "December";
                        daysInMonth = 31;
                    }
                    else if (month >= 13 ) {
                        System.out.println("this date does not exist please try again");
                    }
                    if (month <= 12) {
                        System.out.println("this is the year you put in " + year);
                        System.out.println("this is the month you entered " + month1);
                        System.out.println("this is how many days are in that year " + daysInMonth);
            
                    }
                }
            } `
1 Answers

use a calendar class would have worked and writing the same block of code typically means their is a better way to do it. also HashMap would have also sufficed.

Related