c#简单日历

2018-4-13 有趣的心 博主

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

namespace ConsoleApplication18
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入年份");
            int inyear = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入月份");
            int inmonth = int.Parse(Console.ReadLine());
            int sum = 0;
            int tianshu = 0;
            if (inmonth == 2)
            {
                if (inyear % 4 == 0 && inyear % 100 != 0 || inyear % 400 == 0)
                {
                    tianshu= 29;
                }
                else
                {
                    tianshu = 28;
                }
            }
            else
            {
                if (inmonth == 4 || inmonth == 6 || inmonth == 9 || inmonth == 11)
                {
                    tianshu = 30;
                }
                else
                {
                    tianshu = 31;
                }
            }
            for (int year = 1900; year <= inyear-1; year++)
            {
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                {
                    sum += 366;
                }
                else
                {
                    sum += 365;
                }
            }
            for (int month = 1; month <= inmonth-1; month++)
            {
                if (month == 2)
                {
                    if (inyear % 4 == 0 && inyear % 100 != 0 || inyear % 400 == 0)
                    {
                        sum += 29;
                    }
                    else
                    {
                        sum += 28;
                    }
                }
                else
                {
                    if (month == 4 || month == 6 || month == 9 || month == 11)
                    {
                        sum += 30;
                    }
                    else
                    {
                        sum += 31;
                    }
                }
            }
            sum += 1;
            int wekday = sum % 7;
            Console.WriteLine ("日\t一\t二\t三\t四\t五\t六");
            for (int i = 1; i <= wekday; i++)
            {
                Console.Write("\t");
            }
          
 

            for (int i = 1; i <= tianshu; i++)
            {
                if (sum % 7 == 6)
                {
                    Console.Write(i + "\n");
                }
                else
                {
                    Console.Write(i + "\t");
                }
                sum++;
            }
            Console.Read();
        }
    }
}

标签: c#

评论:

有趣的心
2019-09-05 08:45
本文仿照我要自学网java中的日历例子转换而成
有趣的心
2019-03-21 22:17
666
有趣的心
2018-05-15 02:25
不错哦

发表评论:

Powered by emlog