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);
}
}
CodePudding user response:
You would get several advantages if you used java.time
for this:
- it will be easier to check for invalid input
- you can easily find out if a year is a leap year or not
- you can easily print month names and even abbreviations depending on a locale, that means you can switch languages and standards
Here's a small example for a method:
public static void printInformationAbout(int month, int year) {
// create an instance of month and year from the arguments
Year y = Year.of(year);
Month m = Month.of(month);
// collect information about the year
boolean isLeap = y.isLeap();
int days = y.length();
// and print that information
if (isLeap) {
System.out.println(year " is a leap year and has " days " days");
} else {
System.out.println(year " is a regular year and has " days " days");
}
// print month info (directly this time)
System.out.println(String.format("%s (month no. %d) has %d days in %d",
m.getDisplayName(TextStyle.FULL, Locale.ENGLISH),
m.getValue(), // number of the month
m.length(isLeap), // length of month in days
year));
}
If you used this in a main
, let's say like this:
public static void main(String[] args) {
printInformationAbout(5, 2020);
}
You'd get the following output:
2020 is a leap year and has 366 days
May (month no. 5) has 31 days in 2020
CodePudding user response:
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.