java 中怎么打印一个日历_日历打印用java实现

本文介绍了一个简单的Java程序,用于打印1900年以后任意指定年月的日历。程序通过用户输入年份和月份,计算并显示对应月份的日历布局。

1 importjava.util.Scanner;2

3 /**

4 *打印1900年之后的日历5 *@author:Archer-LCY6 *@date:2018年1月19日上午10:20:397 */

8 public classCalendal {9 //注意全局变量声明的位置

10 /**用户输入的年份*/

11 public static int year =Integer.MAX_VALUE;12 /**用户输入的月份*/

13 public static int month =Integer.MAX_VALUE;14 /**每月的天数*/

15 public static int [] dayofmonth= {31,28,31,30,31,30,31,31,30,31,30,31};16 public static voidmain(String[] args) {17

18 PrintCalendal();19 }20

21 public static voidPrintCalendal() {22 //TODO Auto-generated method stub23 //1、让用户输入年月份

24 InputYearandMonth();25 //2、计算1900-1-1到用户输入年份的天数26 //2-1计算各年的总天数27 //2-2计算各月的天数之和

28 int sum=GetsumdayofYear();29 sum+=GetsumdayofMonth();30 //计算每月的一号是星期几

31 int dayofweek=sum%7+1;32

33 //3、打印年月份(英文),打印月份的标题(星期一到星期日)

34 PrintMonthTitle();35 //4、根据某年某月一号星期几,打印月历功能

36 PrintCalendalContent(dayofweek);37 }38 /**

39 * 根据当月一号打印月历内容40 *@paramdayofweek 当月一号星期几41 */

42 private static void PrintCalendalContent(intdayofweek) {43 //TODO Auto-generated method stub

44 for(int i=0;i

52 }else

53 System.out.print("\t");54 }55 }56

57 /**

58 * 3、打印年月份(英文),打印月份的标题(星期一到星期日)59 */

60 private static voidPrintMonthTitle() {61 //TODO Auto-generated method stub

62 String[] monthofname= {"January","February","March","April","May","June","July","August","September","October","November","December"};63 System.out.println(year+"\t"+monthofname[month-1]);64 String[] weekdays= {"Mon","Tue","Wed","Thu","Fir","Sta","Sun"};65 for(int i=0;i< weekdays.length;i++) {66 System.out.print(weekdays[i]+"\t");67 }68 System.out.println();69 }70

71 /**

72 * 计算1900-year整年的总天数73 *@returnsum总天数74 */

75 private static intGetsumdayofYear() {76 //TODO Auto-generated method stub77 //判断是否输入年份

78 if(year==Integer.MAX_VALUE) {//用户没有输入,重新调用

79 System.out.println("输入错误请从新输入!");80 InputYearandMonth();81 }82 int sum=0;83 for(int i=1900;i

86 if(isLeapYear(i)) {87 sum++;//闰年多加一年

88 }89 }90 returnsum;91 }92

93 /**

94 * 判断是不是闰年95 *@paramyear 要判断的年份96 *@return是闰年返回true97 */

98 private static boolean isLeapYear(int year ) {//此处一定要加参数year,表示的是传入的参数值而不是全局变量中的year值

99 return year%400==0||year%4==0&&year%100!=0;//先算与再算或

100 }101

102 /**

103 * 计算月份天数104 *@returnsum返回year这一年月份的总天数105 */

106 private static intGetsumdayofMonth() {107 //TODO Auto-generated method stub

108

109 int sum=0;110 for(int i=0;i=3) {113 sum++;114 }115 }116 returnsum;117 }118

119 publicCalendal() {120 //TODO Auto-generated constructor stub

121 }122

123 /**

124 * 用户输入年份和月份125 */

126 private static voidInputYearandMonth() {127 //TODO Auto-generated method stub

128 Scanner input =newScanner(System.in);129 System.out.print("请输入年份:");130 //int year=input.nextInt();//局部变量

131 year=input.nextInt();132 System.out.print("请输入月份:");133 month =input.nextInt();134 //节省资源

135 input.close();136 input=null;137 }138 }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值