java程序
使用方法实现月历的打印
由于静态变量的内存空间在程序退出之后才释放内存空间,为避免多次输入,此处需定义两个静态的全局变量year和month和一个全局数组dayOfMonth
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import java.util.Scanner;
public class printCalendar {
/*year接收用户输入的年份*/
public static int year = Integer.MIN_VALUE;
/*month接收用户输入的月份*/
public static int month = Integer.MIN_VALUE;
/*数组dayOfMonth的元素下标和内容表示几月有几天
例如,下标为1的内容是31表示1月有31天
*/
private static int[] dayOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static void main(String[] args) {
PrintCalendar();
}
}打印月历的核心算法(假设从1900开始)
- 第一步用户输入年份和月份
- 第二步计算1900-1-1到用户输入年份月份的总天数(year = 2017,month = 7 2017-1-1 判断用户输入的7月第一天是周几)
- 计算各年的天数
- 计算各月的天数之和
- 第三步打印出年份和月份
- 第四步打印月份的标题(星期一到星期日)
- 第五步根据某月1日是星期几打印月历的内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16private static void PrintCalendar() {
//1.让用户输入年份和月份
InputYearAndMonth();
//2.计算1900-1-1到用户输入年份月份的总天数(year = 2017,month = 7 2017-1-1)
//2-1.计算各年的总天数
//2-2.计算各月的天数之和
int sum = getSumDayOfYears();
sum += getSumDayOfMonth();
sum++;
System.out.println(sum % 7);
//3.打印年份和月份(英文)
PrintMonthTitlre();
//4.打印月份的标题(星期一到星期日)
//5.根据某月1日是星期几打印月历的内容
PrintCalendarContent(sum % 7);
}根据当月1号是周几打印月历的内容
注意:PrintCalenderContent(int dayOfWeek)方法参数dayOfWeek用数组存储下标从0开始,所以取值范围为0-6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20private static void PrintCalendarContent(int dayOfWeek) {
//注意:dayOfWeek取值范围是0-6
int sepCount = dayOfWeek - 1;//\t的数量
if(dayOfWeek == 0) {
sepCount = 6;
}else {
sepCount = dayOfWeek - 1;
}
for (int i = 0; i < dayOfWeek - 1; i++) {
System.out.print("\t");
}
for(int i = 0; i < dayOfMonth[month - 1]; i++) {
System.out.print(i + 1);
if((dayOfWeek + i) % 7 == 0){
System.out.println();
}else{
System.out.print("\t");
}
}
}- 确定月历排列顺序
month - 1:数组下标从0开始
1
2
3
4
5
6
7
8
9
10private static void PrintMonthTitlre() {
String[] monthNames = {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月",
"九月", "十月", "十一月", "十二月"};
System.out.println("\t\t" + year + '\t' + monthNames[month - 1]);
String[] weekdays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
for (int i = 0; i < weekdays.length; i++) {
System.out.print(weekdays[i] + "\t");
}
System.out.println();
}获得1900到year年的总天数
需要判断是否输入年份,若是没有输入或者输入错误给出错误提示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15private static int getSumDayOfYears() {
//需要判断用户是否已经输入了年份
if(year == Integer.MIN_VALUE) {
System.out.println("年份错误!请重新输入年份和月份!");
InputYearAndMonth();
}
int sum = 0;
for(int i = 1900; i < year; i++) {
sum += 365;//每一年累加365天
if(isLeapYear(i)) {
sum++;//闰年多加一天
}
}
return sum;
}得到year年1月1号到year年month-1月最后一天的总天数
1
2
3
4
5
6
7
8
9
10
11private static int getSumDayOfMonth() {
int sum = 0;
for(int i = 0; i < month - 1; i++) {
sum += dayOfMonth[i];
}
//如果year年是闰年,并且month>=3
if(isLeapYear(year) && month >= 3) {
sum++;
}
return sum;
}用来判断传入的year变量是不是闰年
1
2
3private static boolean isLeapYear(int year) {
return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}接收用户输入的年份和月份
1
2
3
4
5
6
7
8
9private static void InputYearAndMonth() {
Scanner input = new Scanner(System.in);
System.out.print("请输入年份:");
year = input.nextInt();
System.out.print("请输入月份:");
month = input.nextInt();
input.close();
input = null;
}
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.