【Unity 10】 C# 万年历 例子

PS:本系列笔记将会记录我此次在北京学习Unity开发的总体过程,方便后期写总结,笔记为日更。
笔记内容均为 自己理解,不保证每个都对。
C#笔记未按照难度排列

闰年:能被4整除不能被100整除,或者能被400整除
1990年1月1日为星期一,所以相差天数模7 可得当前星期几
样例输出:
【Unity 10】 C# 万年历 例子_第1张图片
总代码

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

namespace FREE_TEST2_2019_08_02
{
    class Program
    {
        static bool JudgeYear(int year) //判断闰年
        {
            if (year % 4 == 0 && year % 100 != 0)   //能被4整除,不能被100整除
                return true;
            else
                if (year % 400 == 0)    //能被400整除
                return true;
            else
                return false;
        }

        static int MonthDay(int year, int month)  //判断天数
        {
            switch (month)  //1 3 5 7 8 10 12为大月31天    4 6 9 11为小月30天  2月特殊处理 闰年29天 平年28天
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:return 31;

                case 4:
                case 6:
                case 9:
                case 11:return 30;
                case 2:
                    {
                        if (JudgeYear(year) == true)
                            return 29;
                        else
                            return 28;
                    };
                default:return 0;

            }

        }

        static int TotalNumber(int Year, int Month, int Day)
        {
            int result = 0;
            for (int i = 1990; i <= Year - 1; i++)  //计算年份相差的天数
            {
                if (JudgeYear(i) == true)
                    result = result + 366;
                else
                    result = result + 365;
            }
            result = result + ThisNumber(Year, Month, Day); //年份相差天数 + 今年相差天数 = 总相差天数
            return result;
        }

        static int ThisNumber(int Year, int Month, int Day) //计算日期 距离今年1月1日的相差天数
        {
            int result = 0;
            if (Month > 1)
            {
                for (int i = 1; i < Month; i++)
                {
                    result = result + MonthDay(Year, i);
                }
            }

                result = result + Day - 1;

            return result;
        }


        static void Main(string[] args)
        {

            int Year, Month, Day;
            Console.Write("请输入年份:");
            Year = int.Parse(Console.ReadLine());
            Console.Write("请输入月份:");
            Month = int.Parse(Console.ReadLine());
            Console.Write("请输入日期:");
            Day = int.Parse(Console.ReadLine());





            if (JudgeYear(Year) == true)
                Console.WriteLine("输入的{0}年是闰年", Year);
            else
                Console.WriteLine("输入的{0}年不是闰年", Year);

            int TotalResult;
            int ThisResult;
            TotalResult = TotalNumber(Year, Month, Day);
            ThisResult = ThisNumber(Year, Month, Day);


            Console.WriteLine("输入的{0}年{1}月{2}日距离1990年01月01日的天数为{3}天", Year, Month, Day, TotalResult);
            Console.WriteLine("输入的{0}年{1}月{2}日距离{3}年01月01日的天数为{4}天", Year, Month, Day, Year, ThisResult);

            string[] Week = { "Monday", "Tuesday", "Wednesday", "Thursday", "Firday", "Saturday", "Sunday" };

            for(int i = 0; i < Week.Length; i++)
            {
                Console.Write(Week[i].PadRight(15, ' '));
            }   //日历格式化输出,均为长度均为15,未满15右侧空格补齐

            Console.WriteLine();

            Day = 1;
            int tmpNumber = TotalNumber(Year, Month, Day);  //获得当前月1号距离1990年1月1日的天数

            int tmpWeek = tmpNumber % 7;    //获得当前月1号是星期几






            int Number = MonthDay(Year, Month); //计算当前月有多少天

            for (int i = 6; i > 6 - tmpWeek; i--)   //不是当前月份的日期空格代替
            {
                String temp = " ";

                Console.Write(temp.PadRight(15, ' '));
            }

            for (int i = 1; i <= Number; i++)   //输出日历
            {
                string temp = i.ToString();
                Console.Write(temp.PadRight(15, ' '));
                tmpWeek++;
                if (tmpWeek == 7)   //每输出7次,进行一次换行
                {
                    tmpWeek = 0;
                    Console.WriteLine();
                }
            }

            Console.WriteLine();
            Console.ReadLine();

        }
    }
}

你可能感兴趣的:(C#,Unity)