我正在研究基于控制台的日历作为教育任务(学校)。它需要显示用户输入的特定月份的日历表。我快到了,但我注意到现在所有月份都是星期一开始的。我用一个stringbuilder创建了这个工作表,并且我试图填充该月的第一天实际上带有零的日子,但它还没有工作。 for循环是我的尝试。有人有任何想法吗?我还在学习。以下是构建工作表的代码,除了该问题之外,它可以正常工作:
DateTime date = DateTime.Now;
List<DateTime> dateList = new List<DateTime>();
DateTime dateCopyForModification = date;
int inputMonth = date.Month;
while (dateCopyForModification.Month == inputMonth)
{
dateList.Add(dateCopyForModification);
dateCopyForModification = dateCopyForModification.AddDays(1);
}
Console.WriteLine("\t---------\n\t{0}, {1}\n\t---------", date.ToString("MMMM"), date.Year);
int dayCounter = 0;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("\nMo\t|\tDi\t|\tMi\t|\tDo\t|\tFr\t|\tSa\t|\tSo\t|\n-----------------------------------------------------------------------------------------------------\n");
foreach (DateTime dateTime in dateList)
{
//the part that doesnt work
int day = (int)dateTime.DayOfWeek;
if (dateTime == new DateTime(dateTime.Year, dateTime.Month, 1))
{
for (day = 1; day == (int)dateTime.DayOfWeek; day++)
{
stringBuilder.Append("0");
dayCounter++;
}
}
//until here
else
{
stringBuilder.Append(dateTime.Day);
dayCounter++;
}
if (dateTime.Day == 28)
{
stringBuilder.Append(" |");
}
else
{
stringBuilder.Append("\t|\t");
}
if (dayCounter == 7)
{
stringBuilder.Append("\n");
dayCounter = 0;
}
}
Console.Write(stringBuilder);
Console.ReadKey();
答案 0 :(得分:0)
我为你的问题创建了一个小解决方案。但是它不使用stringbuilder
//This class will store the given month information
class MonthInfo
{
public DayOfWeek DayName { get; set; }
public DateTime DayDate { get; set; }
}
//This class will store the cursor position, just to make it look like a calendar
class CursorPosition
{
public string DayName { get; set; }
public DayOfWeek DayWeek { get; set; }
public int locationx { get; set; }
}
这是代码的其余部分
static void Main(string[] args)
{
int monthdigit = 0;
DateTime _date;
Console.WriteLine("Enter Month");
string monthName= Console.ReadLine(); //should be in format "Jan","Feb"
if (DateTime.TryParseExact(monthName, "MMM", CultureInfo.InvariantCulture, DateTimeStyles.None, out _date))
{
monthdigit = (int)_date.Month;
}
else
{
Console.WriteLine("invalid..programm will exit");
return;
}
List<MonthInfo> GetMyDate = GetDates(2016, monthdigit);
//stores the cursor position to align the days accordingly
List<CursorPosition> CurorList = new List<CursorPosition>();
CurorList.Add(new CursorPosition() { DayName = "Sun", DayWeek=DayOfWeek.Sunday });
CurorList.Add(new CursorPosition() { DayName = "Mon", DayWeek = DayOfWeek.Monday });
CurorList.Add(new CursorPosition() { DayName = "Tue", DayWeek = DayOfWeek.Tuesday });
CurorList.Add(new CursorPosition() { DayName = "Wed", DayWeek = DayOfWeek.Wednesday });
CurorList.Add(new CursorPosition() { DayName = "Thu", DayWeek = DayOfWeek.Thursday });
CurorList.Add(new CursorPosition() { DayName = "Fri", DayWeek = DayOfWeek.Friday });
CurorList.Add(new CursorPosition() { DayName = "Sat", DayWeek = DayOfWeek.Saturday });
//print all the days name
foreach (CursorPosition _activeCursor in CurorList)
{
Console.Write("\t{0}",_activeCursor.DayName);
_activeCursor.locationx = Console.CursorLeft;
}
//retreive the cursor position and display your day index by adjusting the rownumber accordingly.
int _dayIndex = 1;
int rownumber = 2;
foreach (MonthInfo _month in GetMyDate)
{
Console.WriteLine();
int positionx = (from p in CurorList
where p.DayWeek == _month.DayName
select p.locationx).Single();
Console.SetCursorPosition(positionx, rownumber + 1);
Console.Write(_dayIndex++.ToString());
if ((int)_month.DayName== 6)
{
rownumber++;
Console.WriteLine();
}
}
Console.ReadKey();
}
public static List<MonthInfo> GetDates(int year, int month)
{
var query=from date in Enumerable.Range(1, DateTime.DaysInMonth(year, month)) // Days: 1, 2 ... 31 etc.
select new MonthInfo() { DayName = new DateTime(year, month, date).DayOfWeek, DayDate = new DateTime(year, month, date) };
return query.ToList();
}