溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用Java代碼來寫一個日歷

發(fā)布時間:2022-02-24 14:21:47 來源:億速云 閱讀:251 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“怎么用Java代碼來寫一個日歷”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“怎么用Java代碼來寫一個日歷”這篇文章吧。

1 要求

1、輸入年份;
2、輸入月份;
3、輸出某年某月的日歷。

2 思路

1、實(shí)現(xiàn)從控制臺接收年和月,判斷是否是閏年(判斷是否是閏年:能被4整除但不能被100整除;或者能被400整除);

2、計算輸入月份的天數(shù);

3、計算該月第一天是星期幾;

3.1 計算輸入年份距離1900年1月1日的天數(shù);
3.2 計算輸入月份之前的天數(shù)(從當(dāng)年年初開始);
3.3 將以上兩組數(shù)據(jù)進(jìn)行求和;
3.4 已知該月之前的天數(shù),計算輸入月份的第一天是星期幾(從1900年1月1日(星期一)開始推算: 星期幾 = 1 + 天數(shù)差 % 7 )。

4、按格式輸出該月日歷 。

3 源代碼

import java.util.Scanner;

public class index {
    //每個月的天數(shù)
    public static int monthday(int month, int year) {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            int[] day = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            return day[month];
        } else {
            int[] day = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            return day[month];
        }
    }

    //月份總天數(shù)
    public static int monthdays(int month, int year) {
        int totaldays = 0;
        for (int i = 1; i < month; i++) {
            totaldays = totaldays + monthday(i, year);
        }
        return totaldays;
    }

    //距離1900年的年份總天數(shù)
    public static int yeardays(int year){
        int yeardays = 0;
        for (int i = 1900;i<year;i++){
            if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
                yeardays = yeardays+366;
            } else {
                yeardays = yeardays+365;
            }
        }
        System.out.println(year+"年距離1900年的總天數(shù)"+yeardays);
        return yeardays;
    }

    //輸出日歷
    public static void printCalendar(int month,int year){
        int totaldays = 0;
        if (year > 0) {
            if (month > 0 && month < 13) {
                //距離1900年1月1日總天數(shù)
                totaldays = monthdays(month,year)+yeardays(year);
                System.out.println(year+"年"+month+"月1日距離1900年的總天數(shù):"+totaldays);
                System.out.println("
**********"+year+"年"+month+"月的日歷為**********");
                System.out.println("一	二	三	四	五	六	日	");
                int week = 1+totaldays%7;
                //根據(jù)1日為周幾輸出空格
                for(int i=1;i<week;i++){
                    System.out.print(" 	");
                }
                //輸入具體日期
                for(int i=1;i<=monthday(month,year);i++){
                    System.out.print(i+"	");
                    if(week==7){
                        week = 1;//重置為星期一
                        System.out.println();
                    }else{
                        week++;
                    }
                }
            } else {
                System.out.println("輸入的月份不合法!");
            }
        } else {
            System.out.println("輸入的年份不合法!");
        }
    }

    //主函數(shù)
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("***********************歡迎使用萬年歷**************************");
        System.out.println("*********請選擇你需要進(jìn)行的操作(輸入進(jìn)行操作之前的數(shù)字)**********");
        System.out.println("********************1.查詢某年某月的日歷************************");
        System.out.println("********************2.結(jié)束操作*********************************");
        System.out.print("
請選擇你需要進(jìn)行的操作:");
        int a = scanner.nextInt();
        for (int i=0;i>=0;i++) {
            switch (a) {
                case 1:
                    System.out.print("請選擇年份:");
                    int year = scanner.nextInt();
                    System.out.print("請選擇月份:");
                    int month = scanner.nextInt();
                    printCalendar(month, year);
                    System.out.print("
請選擇你需要進(jìn)行的操作:");
                    a = scanner.nextInt();
                    break;
                case 2:
                    System.out.println("退出程序成功!");
                    return;
            }
        }
    }
}

以上是“怎么用Java代碼來寫一個日歷”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI