码迷,mamicode.com
首页 > 其他好文 > 详细

练习.打印月历/年历

时间:2015-01-24 01:34:01      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:

/**程序原始功能:打印用户输入月份的月历
 * 修改需求:
 * 1.修改为打印整年的月历(已完成)
 * 2.修改为打印从输入月份至年底的月历(已完成)
 * 3.打印从输入月份至下一年(输入月份的上一个月)共12个月的月历(已完成)
 */
技术分享
import java.util.Scanner;

public class PrintCalendar {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter full year (e.g. 2010): ");
        int year = input.nextInt();
        System.out.print("Enter month in number between 1 and 12: ");
        int month = input.nextInt();

        // Print calendar for the month of the year
        // 打印输入月份的月历
        printMonth(year, month);
        
    }    
    
    // 打印月历(根据输入月份)的方法
    /* Print the calendar for a month in a year */
    public static void printMonth(int year, int month) {
        // Print the headings of the calendar
        printMonthTitle(year, month);

        // Print the body of the calendar
        printMonthBody(year, month);
    }

    /* Print the month title, e.g. May, 1999 */
    public static void printMonthTitle(int year, int month) {
        System.out.println("\n        " + getMonthName(month) + " " + year);
        System.out.println("-----------------------------");
        System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
    }

    /* Get the English name for the month */
    public static String getMonthName(int month) {
        String monthName = " ";
        switch (month) {
        case 1:
            monthName = "January";
            break;
        case 2:
            monthName = "February";
            break;
        case 3:
            monthName = "March";
            break;
        case 4:
            monthName = "April";
            break;
        case 5:
            monthName = "May";
            break;
        case 6:
            monthName = "June";
            break;
        case 7:
            monthName = "July";
            break;
        case 8:
            monthName = "August";
            break;
        case 9:
            monthName = "September";
            break;
        case 10:
            monthName = "October";
            break;
        case 11:
            monthName = "November";
            break;
        case 12:
            monthName = "December";
        }
        return monthName;
    }

    /* Print month body */
    public static void printMonthBody(int year, int month) {
        // Get start day of the week for the first date in the month
        // 为了打印日历的主体,需要知道这个月的第一天是星期几
        int startDay = getStartDay(year, month);

        // Get number of days in the month
        // 以及该月有多少天
        int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);

        // Pad space before the first day of the month
        int i = 0;
        for (i = 0; i < startDay; i++) {
            System.out.print("    ");
        }

        // 打印日期
        for (i = 1; i <= numberOfDaysInMonth; i++) {
            System.out.printf("%4d", i);

            // 满7天换行
            if ((i + startDay) % 7 == 0) {
                System.out.println();
            }
        }
        System.out.println();
    }

    /* Get the start day of month/1/year */
    public static int getStartDay(int year, int month) {
        // 1800年1月1日的第一天为星期三
        final int START_DAY_FOR_JAN_1_1800 = 3;
        // Get total number of days from 1/1/1800 to month/1/year
        int totalNumberOfDays = getTotalNumberOfDays(year, month);

        // Return the start day for month/1/year
        return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
    }

    /* Get the total number of days since January 1, 1800 */
    public static int getTotalNumberOfDays(int year, int month) {
        int total = 0;
        // Get the total days from 1800 to 1/1/year
        for (int i = 1800; i < year; i++) {
            if (isLeapYear(i)) {
                total += 366;
            } else {
                total += 365;
            }
        }
        // Add days from Jan to the month prior to the calendar month
        for (int i = 1; i < month; i++) {
            total += getNumberOfDaysInMonth(year, i);
        }
        return total;
    }

    /* Get the number of days in a month */
    public static int getNumberOfDaysInMonth(int year, int month) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
                || month == 10 || month == 12) {
            return 31;
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        }
        if (month == 2) {
            return isLeapYear(year) ? 29 : 28;
        }
        // If month is incorrect
        return 0;
    }

    /* Determine if it is a leap year */
    public static boolean isLeapYear(int year) {
        return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
    }
}
打印月历
技术分享
import java.util.Scanner;

public class PrintCalendar {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter full year (e.g. 2010): ");
        int year = input.nextInt();
        System.out.print("Enter month in number between 1 and 12: ");
        int month = input.nextInt();
        
        // 打印从输入月份至年底的月历
        printInputMonthToDecember(year, month);
    }
    
    // 打印从输入月份至年底的月历
    public static void printInputMonthToDecember(int year, int month) {
        int temp = month;
        for (; month <= 12; month++) {
            printMonth(year, month);
            if (month == 12) {
                // 使用break则只打印从输入月份至年底的月历
                break;
            }
        }
    }
    
    // 打印月历(根据输入月份)的方法
    /* Print the calendar for a month in a year */
    public static void printMonth(int year, int month) {
        // Print the headings of the calendar
        printMonthTitle(year, month);

        // Print the body of the calendar
        printMonthBody(year, month);
    }

    /* Print the month title, e.g. May, 1999 */
    public static void printMonthTitle(int year, int month) {
        System.out.println("\n        " + getMonthName(month) + " " + year);
        System.out.println("-----------------------------");
        System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
    }

    /* Get the English name for the month */
    public static String getMonthName(int month) {
        String monthName = " ";
        switch (month) {
        case 1:
            monthName = "January";
            break;
        case 2:
            monthName = "February";
            break;
        case 3:
            monthName = "March";
            break;
        case 4:
            monthName = "April";
            break;
        case 5:
            monthName = "May";
            break;
        case 6:
            monthName = "June";
            break;
        case 7:
            monthName = "July";
            break;
        case 8:
            monthName = "August";
            break;
        case 9:
            monthName = "September";
            break;
        case 10:
            monthName = "October";
            break;
        case 11:
            monthName = "November";
            break;
        case 12:
            monthName = "December";
        }
        return monthName;
    }

    /* Print month body */
    public static void printMonthBody(int year, int month) {
        // Get start day of the week for the first date in the month
        // 为了打印日历的主体,需要知道这个月的第一天是星期几
        int startDay = getStartDay(year, month);

        // Get number of days in the month
        // 以及该月有多少天
        int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);

        // Pad space before the first day of the month
        int i = 0;
        for (i = 0; i < startDay; i++) {
            System.out.print("    ");
        }

        // 打印日期
        for (i = 1; i <= numberOfDaysInMonth; i++) {
            System.out.printf("%4d", i);

            // 满7天换行
            if ((i + startDay) % 7 == 0) {
                System.out.println();
            }
        }
        System.out.println();
    }

    /* Get the start day of month/1/year */
    public static int getStartDay(int year, int month) {
        // 1800年1月1日的第一天为星期三
        final int START_DAY_FOR_JAN_1_1800 = 3;
        // Get total number of days from 1/1/1800 to month/1/year
        int totalNumberOfDays = getTotalNumberOfDays(year, month);

        // Return the start day for month/1/year
        return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
    }

    /* Get the total number of days since January 1, 1800 */
    public static int getTotalNumberOfDays(int year, int month) {
        int total = 0;
        // Get the total days from 1800 to 1/1/year
        for (int i = 1800; i < year; i++) {
            if (isLeapYear(i)) {
                total += 366;
            } else {
                total += 365;
            }
        }
        // Add days from Jan to the month prior to the calendar month
        for (int i = 1; i < month; i++) {
            total += getNumberOfDaysInMonth(year, i);
        }
        return total;
    }

    /* Get the number of days in a month */
    public static int getNumberOfDaysInMonth(int year, int month) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
                || month == 10 || month == 12) {
            return 31;
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        }
        if (month == 2) {
            return isLeapYear(year) ? 29 : 28;
        }
        // If month is incorrect
        return 0;
    }

    /* Determine if it is a leap year */
    public static boolean isLeapYear(int year) {
        return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
    }
}
打印从输入月份至年底的月历
技术分享
import java.util.Scanner;

public class PrintCalendar {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter full year (e.g. 2010): ");
        int year = input.nextInt();
        System.out.print("Enter month in number between 1 and 12: ");
        int month = input.nextInt();
        
        // 打印从输入月份至下一年(输入月份的上一个月)共12个月的月历
        printInputMonthToNextYear(year, month);
    }
    
    // 打印从输入月份至下一年(输入月份的上一个月)共12个月的月历
    public static void printInputMonthToNextYear(int year, int month) {
        int temp = month;
        for (; month <= 12; month++) {
            printMonth(year, month);
        }
        for (int i = 1; i <= temp - 1; i++ ) {
            printMonth(year + 1, i);
        }
    }
    
    
    // 打印月历(根据输入月份)的方法
    /* Print the calendar for a month in a year */
    public static void printMonth(int year, int month) {
        // Print the headings of the calendar
        printMonthTitle(year, month);

        // Print the body of the calendar
        printMonthBody(year, month);
    }

    /* Print the month title, e.g. May, 1999 */
    public static void printMonthTitle(int year, int month) {
        System.out.println("\n        " + getMonthName(month) + " " + year);
        System.out.println("-----------------------------");
        System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
    }

    /* Get the English name for the month */
    public static String getMonthName(int month) {
        String monthName = " ";
        switch (month) {
        case 1:
            monthName = "January";
            break;
        case 2:
            monthName = "February";
            break;
        case 3:
            monthName = "March";
            break;
        case 4:
            monthName = "April";
            break;
        case 5:
            monthName = "May";
            break;
        case 6:
            monthName = "June";
            break;
        case 7:
            monthName = "July";
            break;
        case 8:
            monthName = "August";
            break;
        case 9:
            monthName = "September";
            break;
        case 10:
            monthName = "October";
            break;
        case 11:
            monthName = "November";
            break;
        case 12:
            monthName = "December";
        }
        return monthName;
    }

    /* Print month body */
    public static void printMonthBody(int year, int month) {
        // Get start day of the week for the first date in the month
        // 为了打印日历的主体,需要知道这个月的第一天是星期几
        int startDay = getStartDay(year, month);

        // Get number of days in the month
        // 以及该月有多少天
        int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);

        // Pad space before the first day of the month
        int i = 0;
        for (i = 0; i < startDay; i++) {
            System.out.print("    ");
        }

        // 打印日期
        for (i = 1; i <= numberOfDaysInMonth; i++) {
            System.out.printf("%4d", i);

            // 满7天换行
            if ((i + startDay) % 7 == 0) {
                System.out.println();
            }
        }
        System.out.println();
    }

    /* Get the start day of month/1/year */
    public static int getStartDay(int year, int month) {
        // 1800年1月1日的第一天为星期三
        final int START_DAY_FOR_JAN_1_1800 = 3;
        // Get total number of days from 1/1/1800 to month/1/year
        int totalNumberOfDays = getTotalNumberOfDays(year, month);

        // Return the start day for month/1/year
        return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
    }

    /* Get the total number of days since January 1, 1800 */
    public static int getTotalNumberOfDays(int year, int month) {
        int total = 0;
        // Get the total days from 1800 to 1/1/year
        for (int i = 1800; i < year; i++) {
            if (isLeapYear(i)) {
                total += 366;
            } else {
                total += 365;
            }
        }
        // Add days from Jan to the month prior to the calendar month
        for (int i = 1; i < month; i++) {
            total += getNumberOfDaysInMonth(year, i);
        }
        return total;
    }

    /* Get the number of days in a month */
    public static int getNumberOfDaysInMonth(int year, int month) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
                || month == 10 || month == 12) {
            return 31;
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        }
        if (month == 2) {
            return isLeapYear(year) ? 29 : 28;
        }
        // If month is incorrect
        return 0;
    }

    /* Determine if it is a leap year */
    public static boolean isLeapYear(int year) {
        return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
    }
}
打印从输入月份至下一年(输入月份的上一个月)共12个月的月历
技术分享
  1 import java.util.Scanner;
  2 
  3 public class PrintCalendar {
  4     public static void main(String[] args) {
  5         Scanner input = new Scanner(System.in);
  6 
  7         System.out.print("Enter full year (e.g. 2010): ");
  8         int year = input.nextInt();
  9         
 10         // 打印整年的月历
 11         printYear(year);
 12         
 13     }
 14     
 15     // 打印整年月历的方法
 16     public static void printYear(int year) {
 17         for (int i = 1; i <= 12; i++) {
 18             printMonth(year, i);
 19         }
 20     }    
 21     
 22     // 打印月历(根据输入月份)的方法
 23     /* Print the calendar for a month in a year */
 24     public static void printMonth(int year, int month) {
 25         // Print the headings of the calendar
 26         printMonthTitle(year, month);
 27 
 28         // Print the body of the calendar
 29         printMonthBody(year, month);
 30     }
 31 
 32     /* Print the month title, e.g. May, 1999 */
 33     public static void printMonthTitle(int year, int month) {
 34         System.out.println("\n        " + getMonthName(month) + " " + year);
 35         System.out.println("-----------------------------");
 36         System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
 37     }
 38 
 39     /* Get the English name for the month */
 40     public static String getMonthName(int month) {
 41         String monthName = " ";
 42         switch (month) {
 43         case 1:
 44             monthName = "January";
 45             break;
 46         case 2:
 47             monthName = "February";
 48             break;
 49         case 3:
 50             monthName = "March";
 51             break;
 52         case 4:
 53             monthName = "April";
 54             break;
 55         case 5:
 56             monthName = "May";
 57             break;
 58         case 6:
 59             monthName = "June";
 60             break;
 61         case 7:
 62             monthName = "July";
 63             break;
 64         case 8:
 65             monthName = "August";
 66             break;
 67         case 9:
 68             monthName = "September";
 69             break;
 70         case 10:
 71             monthName = "October";
 72             break;
 73         case 11:
 74             monthName = "November";
 75             break;
 76         case 12:
 77             monthName = "December";
 78         }
 79         return monthName;
 80     }
 81 
 82     /* Print month body */
 83     public static void printMonthBody(int year, int month) {
 84         // Get start day of the week for the first date in the month
 85         // 为了打印日历的主体,需要知道这个月的第一天是星期几
 86         int startDay = getStartDay(year, month);
 87 
 88         // Get number of days in the month
 89         // 以及该月有多少天
 90         int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
 91 
 92         // Pad space before the first day of the month
 93         int i = 0;
 94         for (i = 0; i < startDay; i++) {
 95             System.out.print("    ");
 96         }
 97 
 98         // 打印日期
 99         for (i = 1; i <= numberOfDaysInMonth; i++) {
100             System.out.printf("%4d", i);
101 
102             // 满7天换行
103             if ((i + startDay) % 7 == 0) {
104                 System.out.println();
105             }
106         }
107         System.out.println();
108     }
109 
110     /* Get the start day of month/1/year */
111     public static int getStartDay(int year, int month) {
112         // 1800年1月1日的第一天为星期三
113         final int START_DAY_FOR_JAN_1_1800 = 3;
114         // Get total number of days from 1/1/1800 to month/1/year
115         int totalNumberOfDays = getTotalNumberOfDays(year, month);
116 
117         // Return the start day for month/1/year
118         return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
119     }
120 
121     /* Get the total number of days since January 1, 1800 */
122     public static int getTotalNumberOfDays(int year, int month) {
123         int total = 0;
124         // Get the total days from 1800 to 1/1/year
125         for (int i = 1800; i < year; i++) {
126             if (isLeapYear(i)) {
127                 total += 366;
128             } else {
129                 total += 365;
130             }
131         }
132         // Add days from Jan to the month prior to the calendar month
133         for (int i = 1; i < month; i++) {
134             total += getNumberOfDaysInMonth(year, i);
135         }
136         return total;
137     }
138 
139     /* Get the number of days in a month */
140     public static int getNumberOfDaysInMonth(int year, int month) {
141         if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
142                 || month == 10 || month == 12) {
143             return 31;
144         }
145         if (month == 4 || month == 6 || month == 9 || month == 11) {
146             return 30;
147         }
148         if (month == 2) {
149             return isLeapYear(year) ? 29 : 28;
150         }
151         // If month is incorrect
152         return 0;
153     }
154 
155     /* Determine if it is a leap year */
156     public static boolean isLeapYear(int year) {
157         return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
158     }
159 }
打印整年的月历

 

练习.打印月历/年历

标签:

原文地址:http://www.cnblogs.com/fatoland/p/4245327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!