打印日历
来源:互联网 发布:罗马帝国 知乎 编辑:程序博客网 时间:2024/05/22 13:54
这篇的算法是打印日历,可能对许多小伙伴来说有点困难,但是,笔者还是想让大家硬着头皮看完,毕竟,学会这个算法之后,小伙伴们就不用买日历了~~~好了,一起来看代码
package printdate;import java.util.Scanner;/** * 编写一个程序,显示给定年月的日历。 * * Created by 郑文华 on 2017/6/1. */public class PrintCalendar { public static void main(String[] args){ Scanner input = new Scanner(System.in); //输入年份 System.out.println("please enter full year(eg.2000)"); int year = input.nextInt(); //输入月份 System.out.println("enter mouth between 1 and 12"); int month = input.nextInt(); //调用函数,打印日历 printCalender(year,month); } /** * 打印指定年份月份的日历 * @param year 年份 * @param month 月份 */ public static void printCalender(int year,int month){ //打印日历的头部 printTitle(year,month); //打印日历的主体部分 printBody(year,month); } /** * 打印日历的头部 * @param year * @param month */ public static void printTitle(int year, int month){ System.out.println(" " + getMonth(month) + " " + year); System.out.println("-------------------------------"); System.out.println(" Sun Mon Tue Wed Thu Fri Sat "); } /** * 获得日历的英文名称 * @param mouth * @return */ public static String getMonth(int mouth){ String monthName = " "; switch (mouth){ 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 = "Octomber";break; case 11: monthName = "November";break; case 12: monthName = "December"; } return monthName; } /** * 打印日历的主体部分 * @param year * @param month */ public static void printBody(int year,int month){ //计算该月的第一天是星期几 int startDay = getStartDays(year,month); //计算该月总共的天数 int numberOfDaysInMonth = getNumberOfDaysInMonth(year,month); //计算起始位置需要留出的空白地方 int i ; for( i = 0; i < startDay; i++) { System.out.print(" "); } for( i = 1; i <= numberOfDaysInMonth; i++){ System.out.printf("%5d",i); //是否需要换行 if((i + startDay) % 7 == 0){ System.out.println(); } } } /** * 获得该月的起始位置 * @param year * @param month */ public static int getStartDays(int year, int month){ //以1800年1月1日星期三为基准的日历 final int START_DAY_FOR_JAN_1_1800 = 3; //计算现在的月份和基准时间的相差天数 int totalNumberOfDays = getTotalNumberOfDays(year,month); //返回起始的星期 return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7; } /** * 计算和基准时间的相差天数 * @param year * @param month * @return */ public static int getTotalNumberOfDays(int year,int month){ int total = 0; //获取该年距离基准时间总共的天数 for(int i = 1800; i < year; i++){ //判断是否为闰年 if(isLeapYear(i)){ total = total + 366; } else { total = total + 365; } } //获得该年份至要打印月份的总共天数 for(int i = 1; i < month; i++){ total = total + getNumberOfDaysInMonth(year,i); } return total; } /** * 获得每月的天数 * @param year * @param month * @return */ public static int getNumberOfDaysInMonth(int year,int month){ if(month == 1 || month == 3 || month == 5 || month == 7 || month == 7 || month == 8 || month == 10 || month == 13 ){ return 31; } if(month == 4 || month == 6 || month == 9 || month == 9 || month == 11){ return 30; } if(month == 2){ return isLeapYear(year) ? 29 : 28; } return 0; } public static boolean isLeapYear(int year){ return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); }}</font>
阅读全文
1 0
- 打印日历
- 打印日历
- 打印日历
- 打印日历
- 打印日历
- 打印日历
- 打印日历
- 打印日历
- 打印日历
- 打印日历
- 打印日历
- 打印日历
- 打印日历
- 打印日历
- 日历打印
- 小程序--打印日历
- [原创]PHP打印日历
- 打印日历程序
- 自定义View交集触摸事件
- 1021. 个位数统计 (15)
- NOIP 2010 普及组 复赛 sanguo 三国游戏
- 自定义实现消息转换器
- helloworld
- 打印日历
- 玲珑杯oj-1126咸鱼旅行
- 剑指offer-25-js-复杂链表的复制
- 第二十五天 面试总结
- Cows and Cars UVA
- C++ 中 std::vector存储const型对象报错
- Wooden Sticks HDU 1051
- MyBatis拦截器原理探究
- Sudoku Solver