カレンダーの表示 (paizaランク B 相当)

[問題文](https://paiza.jp/works/mondai/dateset/display_calendar?language_uid=c-sharp&t=3359a58c49106e9e3e1cebf6badc309b)
#####解説を視る前に書いたコード(エラーになるが原因は分かっていない)
```c#

    class Program
    {
        static int[] NextDay(int year,int month, int day)
        {
            day++;
            if(day > LastDay(year, month))
            {
                day = 1;
                month++;
            }
            if(month > 12)
            {
                month = 1;
                year++;
            }
            return new int[] { year, month, day };
        }
        //１８００年１月１日からの日数を計算するメソッド
        static int CountDay (int year,int month,int day)
        {
            int y = 1800, m = 1, d = 1;
            int count = 0;
            //400年分の日数
            int daysOf400years = 365 * 400 + (400 / 4) - (400 / 100) + (400 + 400);
            //400年の倍数飛ぶ
            count += ((year - 1800) / 400) * daysOf400years;
            y = year - (year - 1800) % 400;
            while(!(y == year && m == month && d == day))
            {
                count++;
                int[] nextDay = NextDay(y, m, d);
                y = nextDay[0];
                m = nextDay[1];
                d = nextDay[2];
            }
            return count;
        }
        static int LastDay(int year, int month)
        {
            switch (month)
            {
                case 2:
                    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
                    {
                        return 29;
                    }
                    else
                    {
                        return 28;
                    }
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                default:
                    return 31;
            }
        }
        static int WhatDay(int year, int month, int day)
        {
            int dayOfweek = 3; //1800年１月１日は水曜日
            int count = CountDay(year, month, day);
            return (dayOfweek + count) % 7;
        }
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Trim().Split(' ');
            int y = int.Parse(input[0]);
            int m = int.Parse(input[1]);
            //y年m月1日の曜日を求める
            int dayOfWeek = WhatDay(y, m,1);
            //日→０、月→1、・・・・　土→6
            //y年m月は何日か
            int lastDay = LastDay(y, m);
            for(int i = 0; i < dayOfWeek; i++)
            {
                Console.Write("   ");
            }

            for(int i = 1; i < 10; i++)
            {
                Console.Write(" {0}", i);
                //日から金なら後ろに半角スペース、土なら改行
                if(dayOfWeek != 6)
                {
                    Console.Write(" ");
                }
                else
                {
                    Console.WriteLine();
                }
                dayOfWeek = (dayOfWeek + 1) % 7;
            }
            for(int i = 10;i < lastDay+1; i++)
            {
                Console.Write("{0}", i);
                //日から金なら後ろに半角スペース、土なら改行
                if (dayOfWeek != 6)
                {
                    Console.Write(" ");
                }
                else
                {
                    Console.WriteLine();
                }
                dayOfWeek = (dayOfWeek + 1) % 7;
            }
            for(int i = dayOfWeek; i < 7; i++)
            {
                Console.Write("  ");
                if (i != 6)
                {
                    Console.Write(" ");
                }
                else
                {
                    Console.WriteLine();
                }
            }
        }
    }
`````

#####[YouTubeの解説動画](https://www.youtube.com/watch?v=bSHd6FOqKP4)を視て書いたもの

####メモ　string.Formatメソッドで桁数を指定する方法[
[参考ページ](https://araramistudio.jimdo.com/2018/08/17/c-%E3%81%AEstring-format%E3%81%A7%E6%A1%81%E6%95%B0%E3%82%84%E6%9B%B8%E5%BC%8F%E3%82%92%E6%8C%87%E5%AE%9A%E3%81%99%E3%82%8B/)

```c#

string.Format("値は{0,5}です。", 10);

string.Format("値は{0,-5}です。", "ABC");
````
{}の中の変数を指定する番号の後に、カンマで区切って指定する。

***プラスで右寄せ***、***マイナスで左寄せ***。

```c#

    class Program
    {
        static int[] NextDay(int year, int month, int day)
        {
            day++;
            if (day > LastDay(year, month))
            {
                day = 1;
                month++;
            }
            if (month > 12)
            {
                month = 1;
                year++;
            }
            return new int[] { year, month, day };
        }
        
        static int LastDay(int year, int month)
        {
            switch (month)
            {
                case 2:
                    if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
                    {
                        return 29;
                    }
                    else
                    {
                        return 28;
                    }
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                default:
                    return 31;
            }
        }
        static int DayOfWeek(int year, int month, int day)
        {
            int y = 1800, m = 1, d = 1, dow = 3;
            while(!(y == year && m == month && d == day))
            {
                int[] date = NextDay(y, m, d);
                y = date[0];
                m = date[1];
                d = date[2];
                dow = (dow + 1) % 7;
            }
            return dow;
        }
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Trim().Split(' ');
            int y = int.Parse(input[0]);
            int m = int.Parse(input[1]);
            string[][] cal = new string[6][];
            for(int i = 0; i < 6; i++)
            {
                cal[i] = new string[7];
                for(int j = 0; j < 7; j++)
                {
                    cal[i][j] = "  ";
                }
            }
            //calを更新する
            int line = 0, column = DayOfWeek(y, m, 1);
            int day = 1;
            while(day <= LastDay(y, m))
            {
                cal[line][column] = string.Format("{0,2}", day); //プラスで右寄せ、マイナスで左寄せ
                day++;
                line += (column + 1) / 7; 
                column = (column + 1) % 7;
            }
            DisplayCalendar(cal);
        }
        static void DisplayCalendar(string[][] cal)
        {
            for(int i = 0;i < cal.Length;i++)
            {
                Console.Write(cal[i][0]);
                for(int j = 1; j < cal[i].Length; j++)
                {
                    Console.Write(" {0}", cal[i][j]);
                }
                Console.WriteLine();
            }
        }
    }
``````