万年历

打印万年历(输入年份,月份,输出该月的日历,已知1900年1月1日是星期一),要求:

(1)编写一个方法判断闰年;

(2)编写一个方法判断某年某月有多少天;

(3)编写一个方法计算某年某月前距离1900年1月1日的总天数;(4)编写一个输出某年某月日历的方法;

(5)编写一个测试方法。

 1 package experiment;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Date_2_2 {
 6     public static void main(String args[]) {
 7         Scanner sc = new Scanner(System.in);
 8         System.out.println("请输入要查询日历的年份和月份:");
 9         int year = sc.nextInt();
10         int month = sc.nextInt();
11         output_cal(year, month);
12     }
13     
14     static int nonleap_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
15     static int leap_month[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
16     
17     //判断是否是闰年
18     public static boolean judge_leap(int year) {
19         if(year % 4 == 0 && year % 100 != 0) {
20             return true;
21         }
22         else if(year % 400 == 0) {
23             return true;
24         }
25         else {
26             return false;
27         }
28     }
29     
30     public static void output_leap(int year) {
31         if(judge_leap(year)) {
32             System.out.println(year + "年是闰年。");
33         }
34         else {
35             System.out.println(year + "年不是闰年。");
36         }
37     }
38     
39     //判断某年某月有多少天
40     public static int count_month(int year, int month) {
41         int day;
42         if(judge_leap(year)) {
43             day = leap_month[month - 1];
44         }
45         else
46         {
47             day = nonleap_month[month - 1];
48         }
49         return day;
50     }
51     //某年某月前距离1900年1月1日的总天数
52     public static int count_day(int year, int month) {
53         int day = 0;
54         for(int i = 1900; i < year; i++) {
55             if(judge_leap(i)) {
56                 day += 366;
57             }
58             else {
59                 day += 365;
60             }
61         }
62         for(int i = 0; i < month - 1; i++)
63         {
64             if(judge_leap(year)) {
65                 day += leap_month[i];
66             }
67             else {
68                 day += nonleap_month[i];
69             }
70         }
71         return day;
72     }
73     //输出某年某月日历
74     public static void output_cal(int year, int month) {
75         System.out.println("一\t二\t三\t四\t五\t六\t日");
76         int day;
77         int week;
78         int n_month;
79         int i = 0;
80         day = count_day(year, month);
81         week = day % 7 + 1;
82         n_month = count_month(year, month);
83         for(i = 1; i < week; i++)
84             System.out.print(" \t");
85         for(; i < n_month + week; i++) {
86             System.out.print(i - week + 1);
87             System.out.print("\t");
88             if(i % 7 == 0)
89                 System.out.print("\n");
90         }
91     }
92 }

 

你可能感兴趣的:(万年历)