C#控制台打印日历

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace chapter7

{

    class Program

    {

        static void Main(string[] args)

        {

            //打印日历

            Console.WriteLine("请输入要打印日历的年份:");

            int year = Convert.ToInt32(Console.ReadLine());

            while (year<1900)

            {

                Console.WriteLine("请输入大于1900年份的整数:");

                year = Convert.ToInt32(Console.ReadLine());

            }

            for (int i = 1; i < 13; i++)

            {

                int days = DateTime.DaysInMonth(year, i);//得到当前年/月的天数

                DateTime currday=new DateTime(year,i,1);//当月第一天

                DateTime lastday = currday.AddMonths(1);//下月第一天

                Console.WriteLine();

                switch (i)//判断月份,显示不同的前景色

                {

                    case   1:

                        Console.ForegroundColor = ConsoleColor.Red;

                        break;

                    case 2:

                        Console.ForegroundColor = ConsoleColor.Blue;

                        break;

                    case 3:

                        Console.ForegroundColor = ConsoleColor.Cyan;

                        break;

                    case 4:

                        Console.ForegroundColor = ConsoleColor.DarkBlue;

                        break;

                    case 5:

                        Console.ForegroundColor = ConsoleColor.DarkCyan;

                        break;

                    case 6:

                        Console.ForegroundColor = ConsoleColor.DarkGreen;

                        break;

                    case 7:

                        Console.ForegroundColor = ConsoleColor.DarkMagenta;

                        break;

                    case 8:

                        Console.ForegroundColor = ConsoleColor.Yellow;

                        break;

                    case 9:

                        Console.ForegroundColor = ConsoleColor.DarkYellow;

                        break;

                    case 10:

                        Console.ForegroundColor = ConsoleColor.Green;

                        break;

                    case 11:

                        Console.ForegroundColor = ConsoleColor.Magenta;

                        break;

                    case 12:

                        Console.ForegroundColor = ConsoleColor.White;

                        break;

                }

                Console.WriteLine("{0}年{1}月",year,i);

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

                while (currday<lastday)

                {

                    int weekday =Convert.ToInt32(currday.DayOfWeek);//得到日期是星期几

                    if (currday.Day == 1)//判断该月第一天

                    {

                        StringBuilder tmpSpace = new StringBuilder();

                        for (int j = 0; j < weekday; j++)//输出制表符

                        {

                            tmpSpace.Append("  \t");

                        }

                        Console.Write("{0} {1}", tmpSpace.ToString(), currday.Day);

                    }

                    else if (currday.DayOfWeek == DayOfWeek.Sunday)//如果是礼拜天

                    {

                        Console.Write(Environment.NewLine);

                        Console.Write(" {0}", currday.Day);

                    }

                    else

                    {

                        Console.Write(" \t {0}", currday.Day);

                    }

                    currday = currday.AddDays(1);

                }

            }

            Console.Read();

        }

    }

}

显示结果:
在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一叶知秋~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值