c#中实现控制台日历

  1. using System;  
  2. using System.Collections.Generic;  
  3. class Test  
  4. {  
  5.     public static void Main(String[] args)  
  6.     {  
  7.         while (true)  
  8.         {  
  9.             int year, month;//分别声明接收用户输入年月的变量  
  10.             while (true)  
  11.             {  
  12.                 Console.Write("请输入年份(1900-2100):");  
  13.                 year = int.Parse(Console.ReadLine());  
  14.                 if (year < 1900 || year > 2100)  
  15.                 {  
  16.                     Console.Write("输入的年份不在1900-2100之间,请按会车键重新输入!");  
  17.                     Console.ReadLine();  
  18.                     Console.Clear();  
  19.                 }  
  20.                 else  
  21.                 {  
  22.                     Console.Write("请输入月份(1-12):");  
  23.                     month = int.Parse(Console.ReadLine());  
  24.                     if (month < 1 || month > 12)  
  25.                     {  
  26.                         Console.Write("输入的月份不在1-12之间,请按回车键重新输入!");  
  27.                         Console.ReadLine();  
  28.                         Console.Clear();  
  29.                     }  
  30.                     else  
  31.                         break;  
  32.                 }  
  33.             }  
  34.   
  35.             List<string> dataes = new List<string>();  
  36.             //分别声明用户输入的年月与已知1900年1月1日相隔的整年天数和月份天数  
  37.             int crossDayToYear = 0, crossDayToMonth = 0;  
  38.             //1900年到year-1年相隔的天数  
  39.             for (int i = 1900; i < year; i++)  
  40.             {  
  41.                 if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)  
  42.                     crossDayToYear += 366;  
  43.                 else  
  44.                     crossDayToYear += 365;  
  45.             }  
  46.   
  47.             for (int i = 1; i < month; i++)  
  48.             {  
  49.                 if (i == 2)  
  50.                 {  
  51.                     if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)  
  52.                         crossDayToMonth += 29;  
  53.                     else  
  54.                         crossDayToMonth += 28;  
  55.                 }  
  56.                 else if (i <= 7 && i % 2 != 0 || i > 7 && i % 2 == 0)  
  57.                     crossDayToMonth += 31;  
  58.                 else  
  59.                     crossDayToMonth += 30;  
  60.             }  
  61.             int crossDay = crossDayToYear + crossDayToMonth;//相隔的总天数  
  62.             int dayOfWeek = crossDay % 7 + 1;//用户输入的月份第一天是星期几  
  63.             int space = dayOfWeek - 1;  
  64.             for (int i = 0; i < space; i++)  
  65.             {  
  66.                 dataes.Add("");  
  67.             }  
  68.   
  69.             int days;//用户输入的月份的天数  
  70.             if (month == 2)  
  71.             {  
  72.                 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)  
  73.                     days = 29;  
  74.                 else  
  75.                     days = 28;  
  76.             }  
  77.             else if (month <= 7 && month % 2 != 0 || month > 7 && month % 2 == 0)  
  78.                 days = 31;  
  79.             else  
  80.                 days = 30;  
  81.             for (int i = 1; i <= days; i++)  
  82.             {  
  83.                 dataes.Add(i.ToString());  
  84.             }  
  85.             Console.WriteLine("******************************************************");  
  86.             Console.Write("一\t二\t三\t四\t五\t六\t日");  
  87.             for (int i = 0; i < dataes.Count; i++)  
  88.             {  
  89.                 if (i % 7 == 0)  
  90.                     Console.WriteLine();  
  91.                 Console.Write(dataes[i] + "\t");  
  92.             }  
  93.             Console.WriteLine();  
  94.             Console.WriteLine("******************************************************");  
  95.             Console.Write("按回车键继续");  
  96.             Console.ReadLine();  
  97.             Console.Clear();  
  98.         }  
  99.     }  
  100. }  

猜你喜欢

转载自www.cnblogs.com/lecurs/p/9196162.html
今日推荐