根據(jù)年月算當(dāng)月所有的日期
//萬(wàn)年歷輸入年份月份查找當(dāng)月的日歷
#region
Console.WriteLine("==========================萬(wàn)年歷=========================\n");
int[] daysArr = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Console.Write("請(qǐng)輸入 1900 至 2099 年之間的年份:");
int inputYear = Convert.ToInt32(Console.ReadLine());
Console.Write("請(qǐng)輸入查詢的月份:");
int inputMonth = Convert.ToInt32(Console.ReadLine());
int yearDays = 0;
for (int year = 1900; year <= inputYear - 1; year++)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
yearDays += 366;
}
else
{
yearDays += 365;
}
}
//Console.WriteLine(yearDays);
int monthDays = 0;
Console.WriteLine("---------------------第" + inputYear + "年------------第" + inputMonth + "月份--------------------\n");
for (int month = 0; month < inputMonth - 1; month++)
{
if ((inputYear % 4 == 0 && inputYear % 100 != 0) || inputYear % 400 == 0)
{
daysArr[1] = 29;
monthDays += daysArr[month];
}
else
{
daysArr[1] = 28;
monthDays += daysArr[month];
}
}
//Console.Write(monthDays);
yearDays += monthDays; //計(jì)算出輸入的前一月的天數(shù)總和
////這是將知道的每月的第一天星期幾翎碑,放在相對(duì)應(yīng)的周幾
int dayOfWeek = yearDays % 7 + 1; //通過(guò)總天數(shù)計(jì)算周數(shù)日杈,加1為了計(jì)算下一月的第一天星期幾
//Console.WriteLine("本月第一天是星期" + dayOfWeek);
int[,] weeks = new int[(daysArr[inputMonth - 1] / 7) + 1, 7];//二位數(shù)組,加載周
Console.WriteLine("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期日\(chéng)t");
int cols = 0;
int m = 1;
switch (dayOfWeek)
{
case 1: cols = 0; break;
case 2: cols = 1; break;
case 3: cols = 2; break;
case 4: cols = 3; break;
case 5: cols = 4; break;//通過(guò)列cols承載星期幾
case 6: cols = 5; break;
case 7: cols = 6; break;
default: break;
}
for (int row = 0; row < (daysArr[inputMonth - 1] / 7) + 1; row++)
{
if (cols != 0)
{
for (int i = 0; i < cols; i++)
{
weeks[row, i] = 0;//如果新月份的第一天不是星期一酿炸,用空填充星期
Console.Write("\t");
}
}
for (; cols < 7; cols++)//通過(guò)cols給每一列賦值填硕,也就是天數(shù)
{
if (m <= daysArr[inputMonth - 1])//小于沒(méi)月份的天數(shù)
{
weeks[row, cols] = m;
Console.Write("\0\0" + m + "\t");
++m;
}
}
cols = 0;//循環(huán)給零鹿鳖,不能放上面翅帜,因?yàn)榫筒荒苎h(huán)為零
Console.WriteLine();
}
#endregion