1. 程式人生 > >自己用C#做的一個萬年曆

自己用C#做的一個萬年曆

剛開始接觸C#不久,自己嘗試做了一個日曆,說是說萬年曆哈其實只從1900開始算起哈,因為題目就是這樣的。先貼一下題目;

   實現傳入一個年份後,輸出改年份每個月的資訊,星期和日期;

       提示:1900.1.1剛好是星期一
 

下面是程式碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _007_test6_萬年曆_
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("請輸入你要查詢的年份:");
            int currentYear = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();
            int x, y;
            for (int i = 1; i <= 12; i++)//遍歷每個月
            {
                 x = WhatDay(currentYear, i);//得出某年某月的第一天是星期幾
                 y = EveryMonthDays(currentYear, i);//得出某年某月的天數
                int index;//計數器
                Console.WriteLine("            2018年{0}月\n",i);
                Console.WriteLine("一   二   三   四   五   六   日");
                Console.WriteLine("---------------------------------");
               
            
                 index = x;
                int monthIndex2 = 1;

                while (monthIndex2<=y)
                {
                    for (int j = 1; j < index; j++)
                    {
                        Console.Write("  "+"   ");
                    }
                    for (int k = index; k <=7; k++)
                    {
                        
                        if (monthIndex2>y)
                        {
                            break;
                        }

                        if (monthIndex2<=9)
                        {
                            Console.Write(" "+monthIndex2+ "   ");
                        }
                        else
                        {
                            Console.Write(monthIndex2 + "   ");
                        }
                        
                        monthIndex2++;
                    }
                    Console.WriteLine();
                    index = 1;
                }
            Console.WriteLine("\n\n");
            
            }
            Console.ReadKey();

        }
        static bool IsLeapYear(int year)//判斷某年是不是閏年
        {
            if (year%4==0&&year%100!=0||year%400==0)
            {
                 //   Console.WriteLine("shi 閏年");
                return true;
            }
            else
            {
             //   Console.WriteLine("不是 閏年");
                return false;
            }
        }

        static int WhatDay(int currentYear,int month)//判斷從某年某月第一天是星期幾
        {
            int num;
            int totalDays=0;
            for (int i = 1900; i < currentYear; i++) 
            {
                if (IsLeapYear(i))
                {
                    totalDays += 366;
                }
                else
                {
                    totalDays += 365;
                }

            }
            for (int j = 1; j < month; j++)
            {
                totalDays += EveryMonthDays(currentYear,j);
            }

            num = totalDays % 7;
            return num+1;
        }
        static int EveryMonthDays(int year, int month)//判斷某年每個月的天數
        {
            int i = month;
            int monthDay;
            if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
            {
                monthDay = 31;
            }

            else if (i == 4 || i == 6 || i == 9||i==11)
            {
                monthDay = 30;
            }

           else if (i==2&&IsLeapYear(year)==true)
            {
                monthDay = 29;
            }
            else
            {
                monthDay = 28;
            }
            return monthDay;
        }
    }
}