• 万年历


    首先位java的结构图

    然后是第一种代码(第一种完全在主函数中编写):

    复制代码
      1 package com.cal;
      2 public class MyCalendar01 {
      3     public static void main(String[] args) {
      4         int year = 2017;
      5         //显示某一年的日历
      6         System.out.println(year + "年的日历");
      7         for (int i = 1; i < 13; i++) {
      8             int month = i;
      9             //显示某一月的开头
     10             System.out.println(year + "年" + month + "月的日历");
     11             System.out.println("日	一	二	三	四	五	六");
     12             
     13             int monthDays = 0;
     14             //判断此月有多少天
     15             switch (month) {
     16             case 1:
     17             case 3:
     18             case 5:
     19             case 7:
     20             case 8:
     21             case 10:
     22             case 12:
     23                 monthDays = 31;
     24                 break;
     25             case 4:
     26             case 6:
     27             case 9:
     28             case 11:
     29                 monthDays = 30;
     30                 break;
     31             case 2:
     32                 //判断是否为闰年
     33                 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
     34                     monthDays = 29;
     35                 } else {
     36                     monthDays = 28;
     37                 }
     38                 break;
     39             default:
     40                 break;
     41             }
     42             //计算每月为星期几,weekday是某月星期几
     43             int weekDay = 0;
     44             //计算1900年到该年之间一共有多少天
     45             int days = 0;
     46             for (int j = 1900; j < year; j++) {
     47                 int currentYear = j;
     48                 if (currentYear % 4 == 0 && currentYear % 100 != 0
     49                         || currentYear % 400 == 0) {
     50                     days = days + 366;
     51                 } else {
     52                     days = days + 365;
     53                 }
     54             }
     55             //算出该年某月之前有多少天
     56             for (int j = 1; j < month; j++) {
     57                 int currentmonth = j;
     58                 switch (currentmonth) {
     59                 case 1:
     60                 case 3:
     61                 case 5:
     62                 case 7:
     63                 case 8:
     64                 case 10:
     65                 case 12:
     66                     days = days + 31;
     67                     break;
     68                 case 4:
     69                 case 6:
     70                 case 9:
     71                 case 11:
     72                     days = days + 30;
     73                     break;
     74                 case 2:
     75                     if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
     76                         days = days + 29;
     77                     } else {
     78                         days = days + 28;
     79                     }
     80                     break;
     81                 default:
     82                     break;
     83                 }
     84             }
     85             weekDay = (days % 7 + 1) % 7;
     86             //显示某月主题部分
     87             for (int j = 0; j < weekDay; j++) {
     88                 System.out.print("	");
     89             }
     90             for (int j = 1; j < monthDays + 1; j++) {
     91                 int day = j;
     92                 System.out.print(day + "	");
     93                 if ((day + weekDay) % 7 == 0) {
     94                     System.out.println();
     95                 }
     96             }
     97             System.out.println();
     98 
     99         }
    100     }
    101 }
    复制代码

    第二种代码将某些功能写成一块一块的函数:

    复制代码
    package com.Calendar;
    
    public class MyCalendar {
        public static void main(String[] args) {
            int year = 2017;
            // System.out.println();
            //printCalendarofMonth(year, 2);
            printCalendar(year);
        }
    
        // 显示每个月的开头
        static void printHeader(int year, int month) {
            System.out.println(year + "年" + month + "月");
            System.out.println("日	一	二	三	四	五	六");
        }
    
        // 某年某月是否为闰月
        static boolean isYear(int year) {
            boolean result;
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                result = true;
            } else {
                result = false;
            }
            return result;
        }
    
        // 获得某年的天数
        static int getDayofYear(int year) {
            int days = 0;
            if (isYear(year) == true) {
                days = 366;
            } else {
                days = 365;
            }
            return days;
        }
    
        // 获得某年某月的天数
        static int getDaysofMonth(int year, int month) {
            int days = 0;
            switch (month) {
            case 1:
            case 3:
            case 7:
            case 8:
            case 10:
            case 12:
            case 5:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                if (isYear(year) == true) {
                    days = 29;
                } else {
                    days = 28;
                }
                break;
            default:
                days = 0;
                break;
            }
            return days;
        }
    
        // 获得某年某月的1号是星期几
        static int getWeekDayofMonth(int year, int month) {
            int days = 0;
            int weekday;
            // 累计每年天数
            for (int i = 1900; i < year; i++) {
                int currentyear = i;
                if (isYear(currentyear) == true) {
                    days += 366;
                } else {
                    days += 365;
                }
            }
            // 累计该年前几月的天数
            for (int j = 1; j < month; j++) {
                int currentmonth = j;
                days += getDaysofMonth(year, currentmonth);
            }
            weekday = (days % 7 + 1) % 7;
            return weekday;
        }
    
        // 显示某月的日历主题部分
        static void printCalendarofMonth(int year, int month) {
            int days = 0;
            printHeader(year, month);
            int weekday = getWeekDayofMonth(year, month);
            for (int i = 0; i < weekday; i++) {
                System.out.print("	");
            }
            days = getDaysofMonth(year, month);
            for (int i = 0; i < days; i++) {
                int day = i + 1;
                System.out.print(day + "	");
                if ((weekday + day) % 7 == 0) {
                    System.out.println();
                }
            }
            System.out.println();
        }
        //显示某年日历
        static void printCalendar(int year){
            for (int i = 1; i <13; i++) {
                int month=i;
                printCalendarofMonth(year, month);
            }
        }
    
    }
    复制代码

    第三种为偷懒的办法,从主函数开始写分支函数,然后用ctl+1来建立分支函数,可以更快点:

    复制代码
    package com.cal;
    
    public class MyCalendar {
        public static void main(String[] args) {
            int year = 2017;
            //System.out.println(getDays(year,2));
            //打印某一年的日历
            printYearCalendar(year);
        }
    
        private static void printYearCalendar(int year) {
            System.out.println(year + "年的日历");
            for (int i = 0; i < 12; i++) {
                int month = i + 1;
                System.out.println(year + "年" + month + "月");
                System.out.println("日	一	二	三	四	五	六");
                printMonthofHeader(year, month);
            }
        }
    
        private static void printMonthofHeader(int year, int month) {
            // 某月的开始为星期几
            int weekday = getFirstDayofMonth(year, month);
            for (int i = 0; i < weekday; i++) {
                System.out.print("	");
            }
            //根据年份和月份得到某个月共有几天
            int days = getDays(year, month);
            for (int i = 0; i < days; i++) {
                int day = i + 1;
                System.out.print(day + "	");
                if ((weekday + day) % 7 == 0) {
                    System.out.println();
                }
            }
            System.out.println();
    
        }
    
        //获得某年某个月的1号是星期几
        private static int getFirstDayofMonth(int year, int month) {
            int weekday;
            int days=0;
            // 计算从1900年1月1日以来的整年天数
            for (int i = 1900; i <year; i++) {
                int currentyear=i;
                days+=getDaysofYear(currentyear);            
            }
            // 计算当前年份当前月份之前整月的天数
            for (int i = 1; i < month; i++) {
                int currentmonth=i;
                days+=getDays(year, currentmonth);
            }
            // 根据总天数计算这一天是星期几
            weekday=(days%7+1)%7;
            return weekday;
        }
        //获得某年的天数
        private static int getDaysofYear(int year) {
            if(isLeapYear(year)==true){
                return 366;
            }else{
                return 365;
            }
        }
    
        //根据年份和月份得到某个月共有几天
        private static int getDays(int year, int month) {
            int days = 0;
            switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days=30;
                break;
            case 2:
                if(isLeapYear(year)==true){
                    days=29;
                }else{
                    days=28;
                }
                break;
            default:
                days = 0;
                break;
            }
            return days;
        }
        //判断某个年份是否为闰年
        private static boolean isLeapYear(int year) {
            boolean result;
            if(year%4==0&&year%100!=0||year%400==0){
                result= true;
            }else{
                result= false;
            }
            return result;
        }
    }
    复制代码
  • 相关阅读:
    判断是否声明了某个特性
    如何月份英文缩写字符串转日期
    要捕捉System.Exception异常吗
    jenkins发布到nuget
    C# – Record, Class, Struct
    Google Maps Embed API & JavaScript API
    图片综合知识
    ASP.NET Core – ADO.NET
    CSS – 管理
    JavaScript Library – PhotoSwipe
  • 原文地址:https://www.cnblogs.com/Jansens520/p/6496778.html
Copyright © 2020-2023  润新知