111,098
社区成员




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Day01
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("请输入年份");
int year = int.Parse(Console.ReadLine());
bool Estimate=LeapYear(year);//判断是否闰年
for(int month = 1; month <= 12; month++)
{
int j;
Console.WriteLine("______________________________________________________");
Console.WriteLine("\n");
Console.WriteLine("\t\t\t"+ month+"月");
Console.WriteLine("\n");
int day = DayOfMonth(Estimate, month);//判断月份的天数
int blank = GetWeekByDay(year, month, 1);//计算空格
Console.Write("日\t一\t二\t三\t四\t五\t六\n");
for (j=1; j<=blank; j++)//添加空格
{
Console.Write("\t");
}
for(int i=1; i<=day; i++)
{
int a = 8 - j;
if ((i+7-a)%7==0 || i==day) {
Console.Write(i + "\n");
} else {
Console.Write(i + "\t");
}
}
Console.WriteLine( "\n\n");
}
Console.ReadLine();
}
private static int DayOfMonth(bool Estimate, int month)//判断指定月份的天数
{
int day;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
if (Estimate)
day = 29;
else
day = 28;
break;
default:
day = 0;
break;
}
return day;
}
//判断是否为闰年
private static bool LeapYear(int year)
{
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
return true;
else
return false;
}
//根据年月日计算星期数
private static int GetWeekByDay(int year, int month, int day)
{
DateTime dt = new DateTime(year, month, day);
return (int)dt.DayOfWeek;
}
}
}