欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#实现输出本月的月历

程序员文章站 2024-02-14 10:30:28
格式要求:复制代码 代码如下:su mo tu we th fr sa         01...

格式要求:

复制代码 代码如下:

su mo tu we th fr sa
         01 02 03 04
05 06 07 08 09 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

代码:
复制代码 代码如下:

    class interview1
    {
        static void main()
        {
            printcalender(2011, 10);
        }
        public static void printcalender(int year, int month)
        {
            formatdate fd = new formatdate(year, month);
            string calender =
           @"su mo tu we th fr sa
            {0} {0} {0} {0} {0} {0} {0} {0}
            {0} {0} {0} {0} {0} {0} {0} {0}
            {0} {0} {0} {0} {0} {0} {0} {0}
            {0} {0} {0} {0} {0} {0} {0} {0}
            {0} {0} {0} {0} {0} {0} {0} {0}";

            calender = string.format(calender, fd).trimend();
            console.writeline(calender);
        }
    }
    class formatdate : iformattable
    {
        int num;
        int max;
        public formatdate(int year, int month)
        {
            datetime dt = new datetime(year, month, 1);
            num = (int)dt.dayofweek * -1;
            max = datetime.daysinmonth(year, month);
        }
        public string tostring(string format,iformatprovider formatprovider)
        {
            return num++ < 0 || num > max ? "  " : num.tostring("00");
        }
    }