This was homework, and I have already received my grade, but I did not implement leap years in my code. This is a simple program that displays the numbers in a month based on user input. The only thing is I can't figure out is a way to implement a leap year that would get 29 days for February rather than 28, without writing multiple if statements. Surely there is an easier way? Here is the code:
//Displays number of days in a month
package chapter_3;
import java.util.Scanner;
public class Chapter_3 {
public static void main(String迷你的狗 args) {
Scanner input = new Scanner(System.in); //Prompt user for month and year int month = input.nextInt(); int year = input.nextInt(); if (month == 1) { } else if (month == 2) { } else if (month == 3) { } else if (month == 4) { } else if (month == 5) { } else if (month == 6) { } else if (month == 7) { } else if (month == 8) { } else if (month == 9) { } else if (month == 10) { } else if (month == 11) { } else if (month == 12) { } else { } } } If you use the GregorianCalendar, you could do as below Determines if the given year is a leap year. Returns true if the given year is a leap year. To specify BC year numbers, 1 - year number must be given. For example, year BC 4 is specified as -3. GregorianCalendar cal = new GregorianCalendar(); if(cal.isLeapYear(year)) { } else { }