AI智能
改变未来

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

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, 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==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;elsereturn false;}private static int GetWeekByDay(int year, int month, int day)//根据年月日计算星期数{DateTime dt = new DateTime(year, month, day);return (int)dt.DayOfWeek;}

}
}

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » c# 输入年份输出一年的日历