程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#實現小小的日歷

C#實現小小的日歷

編輯:C#入門知識

 

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace CalendarConsoleApp

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("請輸入年月");

                  int year = int.Parse(Console.ReadLine());

                  int month = int.Parse(Console.ReadLine());

                  int day = 0;

                  int sum = 0;

             

                  for(int i=1900;i<year;i++)

                  {

                         if(i%4==0&&i%100!=0||i%400==0)

                         {

                                sum+=366;

                         }

                         else

                         {

                                sum+=365;

                         }

                  }

             

                  switch(month)

                  {

                      case 12:

                             day = 31;

                             break;

                      case 11:

                             day = 30;

                             break;

                      case 10:

                             day = 31;

                             break;

                      case 9:

                             day = 30;

                             break;

                      case 8:

                             day = 31;

                             break;

                      case 7:

                             day = 31;

                             break;

                      case 6:

                             day = 30;

                             break;

                      case 5:

                             day = 31;

                             break;

                      case 4:

                             day = 30;

                             break;

                      case 3:

                             day = 31;

                             break;

                      case 2:

                             if(year%4==0&&year%100!=0||year%400==0)

                                    day = 29;

                             else

                                    day = 28;

                             break;

                      case 1:

                             day = 31;

                             break;

                  }

                  int leap;

            /*先計算某月以前月份的總天數*/

            switch (month)

            {

                case 1: sum += 0; break;

                case 2: sum += 31; break;

                case 3: sum += 59; break;

                case 4: sum += 90; break;

                case 5: sum += 120; break;

                case 6: sum += 151; break;

                case 7: sum += 181; break;

                case 8: sum += 212; break;

                case 9: sum += 243; break;

                case 10: sum += 273; break;

                case 11: sum += 304; break;

                case 12: sum += 334; break;

            }

            /*判斷是不是閏年*/

            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))

                leap = 1;

            else

                leap = 0;

            /*如果是閏年且月份大於2,總天數應該加一天*/

            if (leap == 1 && month > 2)

                sum++;

 

                  int space = (sum+1)%7;

             

                  Console.WriteLine("日\t一\t二\t三\t四\t五\t六\t");

             

                  for(int i=1;i<=space+day;i++)

                  {

                         if(i<=space)

                                Console.Write("\t");

                         else

                                Console.Write(i-space+"\t");

                    

                         if(i%7 == 0)

                    Console.WriteLine();

                  }

            Console.WriteLine();

        }

    }

}

 

 

 

       else

        Console.Write(i-space+"\t");

  

       if(i%7 == 0)

                    Console.WriteLine();

      }

            Console.WriteLine();

        }

    }

}

 

 

作者lin_lujian

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved