欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)簡(jiǎn)單的萬(wàn)年歷

 更新時(shí)間:2021年04月20日 10:41:02   作者:&小小白&  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單的萬(wàn)年歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java實(shí)現(xiàn)簡(jiǎn)單萬(wàn)年歷的具體代碼,供大家參考,具體內(nèi)容如下

1 要求

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

2 思路

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

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

3、計(jì)算該月第一天是星期幾;

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

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

3 源代碼

import java.util.Scanner;

public class index {
    //每個(gè)月的天數(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("\n**********"+year+"年"+month+"月的日歷為**********");
                System.out.println("一\t二\t三\t四\t五\t六\t日\(chéng)t");
                int week = 1+totaldays%7;
                //根據(jù)1日為周幾輸出空格
                for(int i=1;i<week;i++){
                    System.out.print(" \t");
                }
                //輸入具體日期
                for(int i=1;i<=monthday(month,year);i++){
                    System.out.print(i+"\t");
                    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("***********************歡迎使用萬(wàn)年歷**************************");
        System.out.println("*********請(qǐng)選擇你需要進(jìn)行的操作(輸入進(jìn)行操作之前的數(shù)字)**********");
        System.out.println("********************1.查詢某年某月的日歷************************");
        System.out.println("********************2.結(jié)束操作*********************************");
        System.out.print("\n請(qǐng)選擇你需要進(jìn)行的操作:");
        int a = scanner.nextInt();
        for (int i=0;i>=0;i++) {
            switch (a) {
                case 1:
                    System.out.print("請(qǐng)選擇年份:");
                    int year = scanner.nextInt();
                    System.out.print("請(qǐng)選擇月份:");
                    int month = scanner.nextInt();
                    printCalendar(month, year);
                    System.out.print("\n請(qǐng)選擇你需要進(jìn)行的操作:");
                    a = scanner.nextInt();
                    break;
                case 2:
                    System.out.println("退出程序成功!");
                    return;
            }
        }
    }
}

4 結(jié)果截圖

注意:我的周日是在最后一欄

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 關(guān)于SpringBoot在有Ajax時(shí)候不跳轉(zhuǎn)的問(wèn)題解決

    關(guān)于SpringBoot在有Ajax時(shí)候不跳轉(zhuǎn)的問(wèn)題解決

    最近在使用Ajax來(lái)發(fā)送一些數(shù)據(jù)給后臺(tái)一個(gè)Controller,但是遇到些問(wèn)題,所以下面這篇文章主要給大家介紹了關(guān)于SpringBoot在有Ajax時(shí)候不跳轉(zhuǎn)問(wèn)題的解決辦法,需要的朋友可以參考下
    2022-05-05
  • Spring中的spring.factories文件用法(Spring如何加載第三方Bean)

    Spring中的spring.factories文件用法(Spring如何加載第三方Bean)

    這篇文章主要介紹了Spring中的spring.factories文件用法(Spring如何加載第三方Bean),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 解決Properties屬性文件中的值有等號(hào)和換行的小問(wèn)題

    解決Properties屬性文件中的值有等號(hào)和換行的小問(wèn)題

    這篇文章主要介紹了解決Properties屬性文件中的值有等號(hào)有換行的小問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot的@GetMapping路徑匹配規(guī)則、國(guó)際化詳細(xì)教程

    SpringBoot的@GetMapping路徑匹配規(guī)則、國(guó)際化詳細(xì)教程

    這篇文章主要介紹了SpringBoot的@GetMapping路徑匹配規(guī)則、國(guó)際化,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-11-11
  • Java中Scanner用法實(shí)例解析

    Java中Scanner用法實(shí)例解析

    Scanner?指的是java.util包下的Scanner類,可以接收控制臺(tái)輸入的數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Java中Scanner用法實(shí)例的相關(guān)資料,文中通過(guò)實(shí)例代碼以及圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • springboot如何使用@ConfigurationProperties封裝配置文件

    springboot如何使用@ConfigurationProperties封裝配置文件

    springboot如何使用@ConfigurationProperties封裝配置文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java中的WeakHashMap、LinkedHashMap、TreeMap與Set詳解

    Java中的WeakHashMap、LinkedHashMap、TreeMap與Set詳解

    這篇文章主要介紹了Java中的WeakHashMap、LinkedHashMap、TreeMap與Set詳解,在JVM中,一個(gè)對(duì)象如果不再被使用就會(huì)被當(dāng)做垃圾給回收掉,判斷一個(gè)對(duì)象是否是垃圾,我們的WeakHashMap就是基于弱引用,需要的朋友可以參考下
    2023-09-09
  • SpringCloud連接不上遠(yuǎn)程N(yùn)acos問(wèn)題排查

    SpringCloud連接不上遠(yuǎn)程N(yùn)acos問(wèn)題排查

    本文主要介紹了SpringCloud連接不上遠(yuǎn)程N(yùn)acos問(wèn)題排查,可能是因?yàn)槲撮_(kāi)放端口,或集群內(nèi)部通信異常等,下面就來(lái)介紹一下問(wèn)題解決,感興趣的可以了解一下
    2024-06-06
  • 詳解Java中Comparable和Comparator接口的區(qū)別

    詳解Java中Comparable和Comparator接口的區(qū)別

    這篇文章主要介紹了詳解Java中Comparable和Comparator接口的區(qū)別的相關(guān)資料,希望通過(guò)本文大家能徹底掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • Java wait和notify虛假喚醒原理

    Java wait和notify虛假喚醒原理

    這篇文章主要介紹了Java wait和notify虛假喚醒,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04

最新評(píng)論