标题:新手初学C#求大神解答,输出日历的方法要怎样写,我这样写该怎么改
只看楼主
勿忘心安、
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-7-27
结帖率:100%
已结贴  问题点数:5 回复次数:1 
新手初学C#求大神解答,输出日历的方法要怎样写,我这样写该怎么改
     public void  Calendar()
        {
            switch (_month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:

                    for (int i = 1; i <= 31; i++)
                    {
                        Console.Write(i + " ");
                        if (i % 7 == 0)
                        {
                            Console.WriteLine();
                        }
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    for (int i = 1; i <= 30; i++)
                    {
                        Console.Write(i + " ");
                        if (i % 7 == 0)
                        {
                            Console.WriteLine();
                        }
                    }
                    break;
                case 2:
                    if (_year % 4 == 0 && _year % 100 != 0 || _year % 400 == 0)
                    {
                        for (int i = 1; i <= 29; i++)
                        {
                            Console.Write(i + " ");
                            if (i % 7 == 0)
                            {
                                Console.WriteLine();
                            }
                        }
                    }
                    else
                    {
                        for (int i = 1; i <= 28; i++)
                        {
                            Console.Write(i + " ");
                            if (i % 7 == 0)
                            {
                                Console.WriteLine();
                            }
                        }
                    }
                    break;
            }
        }
搜索更多相关主题的帖子: public 日历 
2016-07-27 20:34
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:4 
这样写可以输出答案啊
当然也可以这样写
namespace ConsoleApplication1
{
    class program
    {
        private static int _month = 4;
        private static int _year = 2016;

        public static void Main(string[] args)
        {
            DateTime dt = new DateTime(_year, _month, 1);
            DateTime dt1 = dt.AddMonths(1);
            while ((dt1) < 0)
            {
                Console.Write(dt.Day + " ");
                if (dt.Day % 7 == 0)
                {
                    Console.WriteLine();
                }
                dt = dt.AddDays(1);  
            }
            Console.Read();
        }
    }
}
2016-07-30 18:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-467301-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016215 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved