LoginSignup
0
1

More than 3 years have passed since last update.

C#でコンソール画面でのカレンダー作ってみた

Last updated at Posted at 2020-03-28
Test.cs
using System.Collections.Generic;
using static System.Linq.Enumerable;
using static System.Console;
class Sub : object
{
    const int WEEK_MAX = 7;//マジックナンバーは禁止

    public Sub()
    {
    }

    public int Week()
    {
        WriteLine("1/1の曜日を入力");
        switch (ReadLine())
        {
            case "日":
            case "日曜日":
                return 1;
            case "月":
            case "月曜日":
                return 2;
            case "火":
            case "火曜日":
                return 3;
            case "水":
            case "水曜日":
                return 4;
            case "木":
            case "木曜日":
                return 5;
            case "金":
            case "金曜日":
                return 6;
            case "土":
            case "土曜日":
                return 7;
            default:
                return 0;
        }
    }

    public string Calendar(int year,int you)
    {
        if (year <= 0 || you == 0)
        {
            return null;
        }
        WriteLine($"{year}年カレンダー\n");
        var cld = "";
        int max;
        foreach (var y in Range(1, 12))
        {
            cld += $"{y.ToString()}月のカレンダー\n\n"; 
            if (y == 4 || y == 6 || y == 9 || y == 11)
            {
                max = 30;
            }
            else if (y == 2)
            {
                if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
                {
                    max = 29;
                }
                else
                {
                    max = 28;
                }
            }
            else
            {
                max = 31;
            }
            cld += "日月火水木金土\n";
            foreach (var x in Range(1, max))
            {
                if (x < 10)
                {
                    cld += " ";
                }
                if (x == 1)
                {
                    if (you == 0)
                    {
                        you += 1;
                    }
                    foreach(var z in Range(1, you-1))
                    {
                        cld += "  ";
                    }
                }
                if (you % WEEK_MAX == 0)
                {
                    you %= WEEK_MAX;
                    cld += x.ToString() + "\n";
                }
                else
                {
                    cld += x.ToString();
                }
                you++;
            }
            cld += "\n\n";
        }
        return cld;
    }

    public void End()
    {
        WriteLine("何かを入力してください");
        ReadLine().ToString();
    }

}

class Test
{
    static void Main(string[] args)
    {
        var sub = new Sub();
        Write("調べたい年を入力");
        var year = int.Parse(ReadLine());
        var cld = sub.Calendar(year,sub.Week());
        WriteLine(cld is null? "\n存在しないです\n":cld);
        sub.End();
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1