久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用Java計算閏年

Calculate leap year in Java(用Java計算閏年)
本文介紹了用Java計算閏年的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

可能重復:
計算閏年的Java代碼,這段代碼正確嗎?

這是作業(yè),我已經(jīng)收到了成績,但我沒有在我的代碼中實現(xiàn)閏年.這是一個簡單的程序,可以根據(jù)用戶輸入顯示一個月內(nèi)的數(shù)字.唯一的事情是我想不出一種方法來實現(xiàn)閏年,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.");
}

這篇關(guān)于用Java計算閏年的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 午夜性视频 | 久久精品16 | 天堂av中文在线 | 天天操天天天干 | 不卡视频在线 | 欧美三级电影在线播放 | 国产成人在线播放 | 欧美一级一区 | 黄色大片在线免费观看 | 日韩精品一区二区三区视频播放 | 在线看av网址| 国产成人精品a视频一区www | 日韩在线免费播放 | 福利视频网 | 欧美一区二区成人 | 影音先锋男| 国产精品久久 | 欧美日韩高清一区二区三区 | 正在播放国产精品 | 亚洲在线 | 国产片淫级awww| 碰碰视频 | 鸳鸯谱在线观看高清 | 久久综合伊人 | 国产精品久久久久久久久久东京 | 97成人免费 | 国产一区二区三区在线 | 亚洲成人免费av | 美女毛片免费看 | 午夜一区 | 久久久成人一区二区免费影院 | 一级黄色网页 | 99久久国产综合精品麻豆 | 麻豆亚洲| 亚洲精品久久久久中文字幕欢迎你 | 91亚洲精品在线观看 | 嫩草研究影院 | 人人性人人性碰国产 | 亚洲一区二区三区四区五区午夜 | 亚洲免费在线观看 | 99免费在线视频 |