c# 输入年份输出一年的日历

YQ.yang YQ.yang 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Day01
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine("请输入年份");
int year = int.Parse(Console.ReadLine());
bool Estimate=LeapYear(year);//判断是否闰年
for(int month = 1; month < 13; 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, day);//计算空格
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==a)||(i==(a+7))|| (i == (a + 14)) || (i == (a + 21)) || (i == (a + 28)) || (i == (a + 35)) || (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;
if (Estimate == true)
{
if(month==1||month==3||month==5||month==7||month==8||month==12) day = 31;
else if (month==4||month==6||month==9||month==10||month==11) day=30 ;
else day=29;
}
else
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 12) day = 31;
else if (month == 4 || month == 6 || month == 9 || month == 10 || month == 11) day = 30;
else day = 28;
}
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;
}


}
}
...全文
评论 9
收藏 已收藏
分享
返回顶部
YQ.yang
C# 111,129 成员 642,538 内容