using System;
namespace xxx{
class Program{
staic void Main(){
//获取数据
Console.WriteLine("请输入年");
int year = int.Parse(Console.WriteLine());
Console.WriteLine("请输入月");
int month = int.Parse(Console.WriteLine());
Console.WriteLine("请输入日");
int day = int.Parse(Console.WriteLine());
//打印表头,显示星期一....日
string week = "";
for(int i<0;i<=7;i++){
week = "一二三四五六日";
Console.Write("星期" + week[i]);
}
//计算输入的月份有多少天
int theDay = 0;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 10:
case 12:
theDay = 31;
case 4:
case 6:
case 8:
case 10:
case 11:
theDay = 30;
//这里判断如果是闰年,2月为28天,平年为29天
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
theDay = 28;
}esle theDay = 29;
}
//计算每月1号是星期几,并对应空出格
//思路:如果12月1号是星期三,GetWeekByDay返回的值就是3,就空出两格,用for循环,循环条件就是GetWeekByDay得值
string dayBlack = "";
for(int z=0; z<GetWeekByDay(year,month,1); z++){
dayBlack += "\t";
}
Console.Write(dayBlack);
//写每个月的天数列表
for(int i=0; i<theDay; i++){
//每逢周日换行,周日在GetWeekByDay的值是0,而天数就用i
if(GetWeekByDay(year,month,i) == 0){
Console.Write(i + "号" + "\n");
}else Console.Write(i + "号" + "\t");
}
}
//输入年月日获取星期几的方法
private static int GetWeekByDay(int year,int month,int day){
DateTime dt = new DateTime(year,month,day);
return (int)dt.DayOfWeek;
}
}
}
输入年月日,在控制台输出对应的月历
最新推荐文章于 2024-07-09 14:14:16 发布