用Java计算闰年

Calculate leap year in Java(用Java计算闰年)
本文介绍了用Java计算闰年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

这是作业,我已经收到了成绩,但我没有在我的代码中实现闰年.这是一个简单的程序,可以根据用户输入显示一个月内的数字.唯一的事情是我想不出一种方法来实现闰年,2 月将获得 29 天而不是 28 天,而无需编写多个 if 语句.当然有更简单的方法吗?代码如下:

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) {
        System.out.println("This program will calculate 
"
                + "the number of days in a month based upon 
"
                + "your input, when prompted please enter 
"
                + "the numerical value of the month you would like
"
                + "to view including the year. Example: if you would like
"
                + "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,你可以这样做

If you use the GregorianCalendar, you could do as below

确定给定年份是否为闰年.如果给定,则返回 true年是闰年.要指定 BC 年编号,必须使用 1 年编号被给予.例如,公元前 4 年指定为 -3.

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))
{
    System.out.print("Given year is leap year.");
}
else
{ 
    System.out.print("Given year is not leap year.");
}

这篇关于用Java计算闰年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Month is not printed from a date - Java DateFormat(月份不是从日期打印的 - Java DateFormat)
java get week of year for given a date(java获取给定日期的一年中的一周)
Get the number of days, weeks, and months, since Epoch in Java(获取自 Java 中的 Epoch 以来的天数、周数和月数)
Is there a good way to get the date of the coming Wednesday?(有什么好方法可以得到即将到来的星期三的日期吗?)
Calendar view for Android GingerBread and before (APIlt;11)(Android GingerBread 及之前的日历视图 (API11))
How to save and retrieve Date in SharedPreferences(如何在 SharedPreferences 中保存和检索日期)