用Java计算闰年



可能重复:
计算闰年的Java代码,这个代码正确吗?

这是家庭作业,我已经拿到了分数,但我没有在代码中实现闰年。这是一个简单的程序,可以根据用户输入显示一个月内的数字。唯一我想不出的是,在不写多个if语句的情况下,实现闰年的方法是2月有29天,而不是28天。肯定有更简单的方法吗?这是代码:

//Displays number of days in a month
package chapter_3;
import java.util.Scanner;

public class Chapter_3 {
    public static void main(String[] args) {
        System.out.println("This program will calculate n"
                + "the number of days in a month based upon n"
                + "your input, when prompted please enter n"
                + "the numerical value of the month you would liken"
                + "to view including the year. Example: if you would liken"
                + "to view March enter 3 2011 and so on.");
        Scanner input = new Scanner(System.in);
        //Prompt user for month and year
        System.out.print("Please enter the month and year: ");
        int month = input.nextInt();
        int year = input.nextInt();
        if (month == 1) {
            System.out.println("January " + year + " has 31 days");
        }   
        else if (month == 2) {
            System.out.println("February " + year + " has 28 days");
        }   
        else if (month == 3) {
            System.out.println("March " + year + " has 31 days");
        }
        else if (month == 4) {
            System.out.println("April " + year + " has 30 days");
        }
        else if (month == 5) {
            System.out.println("May " + year + " has 31 days");
        }
        else if (month == 6) {
            System.out.println("June " + year + " has 30 days");
        }
        else if (month == 7) {
            System.out.println("July " + year + " has 31 days");
        }
        else if (month == 8) {
            System.out.println("August " + year + " has 31 days");
        }
        else if (month == 9) {
            System.out.println("September " + year + " has 30 days");
         }
        else if (month == 10) {
            System.out.println("October " + year + " has 30 days");
        }
        else if (month == 11) {
            System.out.println("November " + year + " has 30 days");
        }
        else if (month == 12) {
            System.out.println("December " + year + " has 31 days");
         }
        else {
            System.out.println("Please enter the numerical value "
                + "of the month you would like to view");
        }
    }
}        

如果您使用GregorianCalendar,您可以执行以下

确定给定年份是否为闰年。如果给定年是闰年。要指定BC年份编号,1年编号必须被给予。例如,公元前4年被指定为-3年。

GregorianCalendar cal = new GregorianCalendar();
if(cal.isLeapYear(year))
{
    System.out.print("Given year is leap year.");
}
else
{ 
    System.out.print("Given year is not leap year.");
}

报价http://en.wikipedia.org/wiki/Leap_year

可被100整除的年份不是闰年,除非它们也可以被400整除,在这种情况下,它们是闰年。

因此,如果你想重新发明轮子:

boolean isLeapYear = year%4 == 0 && (year%100 != 0 || year%400 == 0)

但最优雅的解决方案(取自此处(可能是

public static boolean isLeapYear(int year) {
  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.YEAR, year);
  return cal.getActualMaximum(DAY_OF_YEAR) > 365;
}

您可以使用switch语句,在二月的情况下使用一些if语句:

switch(month)
{
case 1:
    System.out.println("January " + year + " has 31 days");
    break;
case 2:
    int n=28;
    if(year%4==0 && (year%100!=0 || year%400==0)
        n++;
    System.out.println("February " + year + " has "+n+" days");
    break;
case 3:
    System.out.println("March " + year + " has 31 days");
    break;
case 4:
    System.out.println("April " + year + " has 30 days");
    break;
case 5:
    System.out.println("May " + year + " has 31 days");
    break;
case 6:
    System.out.println("June " + year + " has 30 days");
    break;
case 7:
    System.out.println("July " + year + " has 31 days");
    break;
case 8:
    System.out.println("August " + year + " has 31 days");
    break;
case 9:
    System.out.println("September " + year + " has 30 days");
    break;
case 10:
    System.out.println("October " + year + " has 30 days");
    break;
case 11:
    System.out.println("November " + year + " has 30 days");
    break;
case 12:
    System.out.println("December " + year + " has 31 days");
    break;
default case:
    System.out.println("Please enter the numerical value of the month you would like to view");
}

参见http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html有关更多信息,

最新更新