- A+
所属分类:.NET技术
1 /*编写日期2021/8/16 2 **开发者:市井道士 3 **为方便同样在此学习的各位同学,在此放出自己的答案供大家参考学习,仅供参考,随意转载*/ 4 using System; 5 6 namespace d03 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 Calendar calendar = new Calendar(); 13 calendar.Printdate(DateTime.Now.Year); 14 Console.WriteLine("编写日期2021/8/16"); 15 Console.WriteLine("开发者:市井道士"); 16 Console.WriteLine("为方便同样在此学习的各位同学,在此放出自己的答案供大家参考学习,仅供参考,随意转载"); 17 Console.ReadLine(); 18 } 19 } 20 class Calendar 21 { 22 /// <summary> 23 /// 输入年月日,得到这天是星期几 24 /// </summary> 25 /// <param name="year">年</param> 26 /// <param name="month">月</param> 27 /// <param name="day">日</param> 28 /// <returns>星期几</returns> 29 private static int GetWeekByDay(int year, int month, int day) 30 { 31 DateTime dt = new DateTime(year, month, day); 32 return (int)dt.DayOfWeek; 33 } 34 35 /// <summary> 36 /// 获取某个月的天数,输入(int)年份,月份,返回天数(int) 37 /// </summary> 38 /// <param name="year">年</param> 39 /// <param name="month">月</param> 40 /// <returns>天数</returns> 41 private static int GetMonthDay(int year, int month) 42 { 43 int thismonthdays = DateTime.DaysInMonth(year, month); 44 return thismonthdays; 45 } 46 /// <summary> 47 /// 打印年历 48 /// </summary> 49 /// <param name="year"></param> 50 public void Printdate(int year) 51 { 52 int nextlinecount;//使用一个计数器没过一天就加1,逢7换行 53 for (int month = 1; month <= 12; month++) 54 { 55 nextlinecount = 0;//计数器每个月开始需要进行初始化 56 Console.WriteLine("{0}年{1}月",year, month); 57 Console.WriteLine("星期天t 星期一t 星期二t 星期三t 星期四t 星期五t 星期六t"); 58 //获取每个月第一天是星期几然后输出对应次数的空格 59 for (int count=1;count <= GetWeekByDay(year,month,1);count++ ){ 60 Console.Write(" t "); 61 nextlinecount++;//计数器增加,这里的空的是上个月的日子 62 } 63 for (int day = 1; day <= GetMonthDay(year, month); day++) 64 { 65 if (nextlinecount % 7 == 0)//每次打印日期前先判断是否为周六,逢7换行 66 Console.WriteLine(); 67 Console.Write(day+"t "); 68 nextlinecount++; 69 } 70 Console.WriteLine(); 71 Console.WriteLine(); 72 Console.WriteLine("========================================================================="); 73 Console.WriteLine(); 74 } 75 } 76 } 77 }