C# - 程序显示当月的所有相同日期,用户输入

时间:2016-12-01 15:20:11

标签: c# date

我需要有关如何显示的指导,例如,基于用户输入,一个月内的所有星期一日期可以是任何日期和任何月份。 到目前为止我的代码在这里,但似乎并没有显示日期。我输入的指定日期,哪里出错了,任何帮助都非常感谢。

using System;
using System.Globalization;

namespace Calendar
{
   class Program
   {
        static int promptDay = new int();
        static int year = new int();
        static int month = new int();
        static int[,] calendar = new int[6, 7];

        static void Main(string[] args)
        {
            Console.Write("Enter the year? ");
            year = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the month (January = 1, etc): ");
            month = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the day you want to see the dates for (Mon = 0, etc): ");
            promptDay = Convert.ToInt32(Console.ReadLine());
            DrawHeader();
            FillCalendar();
            DrawCalendar();
            Console.ReadLine();
        }

        static void DrawHeader()
        {
            Console.Write("\n\n");
            //gives you the month and year at the top of the calendar
            Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month) + " " + year);
            Console.WriteLine("Mo Tu We Th Fr Sa Su");
        }

        static void FillCalendar()
        {
            int currentDay = 1;
            for (int i = 0; i < calendar.GetLength(0); i++)
            {
                for (int j = 0; j < calendar.GetLength(1) && currentDay <= totalDays; j++)
                {
                    if (i == 0 && day > j)
                    {
                        calendar[i, j] = 0;
                    }
                    else
                    {
                        if (j != promptDay)
                        {
                            calendar[i, j] = 0;
                        }
                        else
                        {
                            calendar[i, j] = currentDay;
                        }
                        currentDay++;
                    }
                }
            }
        }

        static void DrawCalendar()
        {
            for (int i = 0; i < calendar.GetLength(0); i++)
            {
                for (int j = 0; j < calendar.GetLength(1); j++)
                {
                    if (calendar[i, j] > 0)
                    {
                        if (calendar[i, j] < 10)
                        {
                            Console.Write(" " + calendar[i, j] + " ");
                        }
                        else
                        {
                            Console.Write(calendar[i, j] + " ");
                        }
                    }
                    else
                    {
                        Console.Write("   ");
                    }
                }
                Console.WriteLine("");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

以下代码打印出给定DayOfWeek的所有日期,是的,它可以改进,但它只是一个起点

<强>输出

What is the year? : 2016
What is the name of the month? : dicembre
Monday
5 12 19 26
Thursday
1 8 15 22 29
Wednesday
7 14 21 28
Tuesday
6 13 20 27
Friday
2 9 16 23 30
Saturday
3 10 17 24 31
Sunday
4 11 18 25

请注意dicembre为意大利语中的december

Dictionary<DayOfWeek, List<int>> daysCount = new Dictionary<DayOfWeek, List<int>>()
{
    { DayOfWeek.Monday, new List<int>() },
    { DayOfWeek.Thursday, new List<int>() },
    { DayOfWeek.Wednesday, new List<int>() },
    { DayOfWeek.Tuesday, new List<int>() },
    { DayOfWeek.Friday, new List<int>() },
    { DayOfWeek.Saturday, new List<int>() },
    { DayOfWeek.Sunday, new List<int>() },
};

Console.Write("What is the year? : ");
var year = Convert.ToInt32(Console.ReadLine());
Console.Write("What is the name of the month? : ");
var monthName = Console.ReadLine();

var monthNumber = DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture).Month;
var dayOfMonth = DateTime.DaysInMonth(year, monthNumber);

var date = new DateTime(year, monthNumber, 1);


for(int i = 0; i < dayOfMonth; i++, date = date.AddDays(1))
{

    if (daysCount[date.DayOfWeek] == null)
    {
        daysCount[date.DayOfWeek] = new List<int>();
    }

    daysCount[date.DayOfWeek].Add(date.Day);
}

foreach (var day in daysCount)
{
    Console.WriteLine(day.Key.ToString());
    foreach (var dayNumber in day.Value)
    {
        Console.Write(string.Format("{0} ", dayNumber));
    }
    Console.WriteLine();

}

Console.ReadLine();

Variant仅显示用户指定的日期

请注意Sunday = 0

var days = new List<int>();
Console.Write("What is the year? : ");
var year = Convert.ToInt32(Console.ReadLine());
Console.Write("What is the name of the month? : ");
var monthName = Console.ReadLine();
Console.Write("Enter the day you want to see the dates for (Mon = 0, etc): ");
var promptDay = Convert.ToInt32(Console.ReadLine());

var monthNumber = DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture).Month;
var dayOfMonth = DateTime.DaysInMonth(year, monthNumber);

var date = new DateTime(year, monthNumber, 1);


for(int i = 0; i < dayOfMonth; i++, date = date.AddDays(1))
{

    if ((int)date.DayOfWeek == promptDay)
        days.Add(date.Day);
}

foreach (var day in days)
{
    Console.Write(string.Format("{0} ", day));

}

Console.ReadLine();