【问题标题】:C# - program to display all of the same days dates in the month, user inputC# - 显示月份中所有相同日期的程序,用户输入
【发布时间】:2016-12-02 00:19:18
【问题描述】:

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

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("");
            }
        }
    }
}

【问题讨论】:

  • 你为什么要这样声明你的ints?
  • yh 我试过了,但它不要求用户输入?我怎么能得到那个?
  • 我编辑了我之前尝试过的新代码,
  • 请检查一下,看看我哪里出错了

标签: c# date


【解决方案1】:

以下代码打印出给定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();

仅显示用户指定日期的变体

注意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();

【讨论】:

  • 我怎样才能改变它,让它只显示用户输入的一天,所以只是星期一,等等......
  • 另外我在 list 位和字典位也遇到了错误
  • 找不到类型或命名空间字典,是否缺少参考
  • 列表相同
  • @J.Doe 我已经更新了答案,以解决您在顶部插入 using System.Collections.Generic; 的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多