首页 > 编程知识 正文

Java求闰年,java怎么求闰年

时间:2023-05-03 23:04:17 阅读:212513 作者:369

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

{

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。