static void Main(string[] args)
{
Console.WriteLine(" |----------------------------------------------------------------|");
Console.WriteLine(" | 这是一个“输入一个日期,求该日期是这一年中的第几天 ”的程序 |");
Console.WriteLine(" |----------------------------------------------------------------|");
Console.WriteLine();
Console.WriteLine("您需要输入一个日期...");
Console.WriteLine();
Console.Write("您需要输入年份:");
int year = int.Parse(Console.ReadLine());
Console.WriteLine();
Console.Write("您需要输入月份:");
int month = int.Parse(Console.ReadLine());
Console.WriteLine();
Console.Write("您需要输入具体日期(几号?):");
int date = int.Parse(Console.ReadLine());
Console.WriteLine();
Console.Write("{0}年{1}月{2}日是一年中的第",year,month,date);
bool b = Leap_Common_year(year);//判断平年还是闰年
switch(month) //判断月份
{
case 1:month_1(date);break;//根据日期计算出是哪一天
case 2:month_2(date);break;
case 3:month_3(date,b);break;
case 4:month_4(date,b);break;
case 5:month_5(date,b);break;
case 6:month_6(date,b);break;
case 7:month_7(date,b);break;
case 8:month_8(date,b);break;
case 9:month_9(date,b);break;
case 10:month_10(date,b);break;
case 11:month_11(date,b);break;
case 12:month_12(date,b);break;
default: Console.WriteLine("您输入的月份有误..."); break;
}
Console.WriteLine(Program.week(year,month,date));
Console.ReadKey();
}
static bool Leap_Common_year(int year)//判断平年还是闰年
{
bool b;
if (year % 4 == 0 && year % 100 != 0)
{
b = false;
}
else
{
b = true;
}
return b;
}
static void month_1(int date)
{
Console.Write("{0}天。",date);
}
static void month_2(int date)
{
int d;
d = 31 + date;
Console.Write("{0}天",d);
}
static void month_3(int date, bool b)
{
int d;
if (b)
{
d = 31 + 28 + date;
}
else
{
d = 31 + 29 + date;
}
Console.Write("{0}天", d);
}
static void month_4(int date, bool b)
{
int d;
if (b)
{
d = 31 + 28 + 31 + date;
}
else
{
d = 31 + 29 + 31 + date;
}
Console.Write("{0}天", d);
}
static void month_5(int date, bool b)
{
int d;
if (b)
{
d = 31 + 28 + 31 + 30 + date;
}
else
{
d = 31 + 29 + 31 + 30 + date;
}
Console.Write("{0}天", d);
}
static void month_6(int date, bool b)
{
int d;
if (b)
{
d = 31 + 28 + 31 + 30 + 31 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + date;
}
Console.Write("{0}天", d);
}
static void month_7(int date, bool b)
{
int d;
if (b)
{
d = 31 + 28 + 31 + 30 + 31 + 30 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + 30 + date;
}
Console.Write("{0}天", d);
}
static void month_8(int date, bool b)
{
int d;
if (b)
{
d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + date;
}
Console.Write("{0}天", d);
}
static void month_9(int date, bool b)
{
int d;
if (b)
{
d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + date;
}
Console.Write("{0}天", d);
}
static void month_10(int date, bool b)
{
int d;
if (b)
{
d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date;
}
Console.Write("{0}天", d);
}
static void month_11(int date, bool b)
{
int d;
if (b)
{
d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date;
}
Console.Write("{0}天", d);
}
static void month_12(int date, bool b)
{
int d;
if (b)
{
d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date;
}
else
{
d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date;
}
Console.Write("{0}天", d);
}
//Y+L+M+D(除7的余数)
static string week(int year,int month,int date)
{
if (month == 1 || month == 2)
{
month += 12;
year--;
//把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
}
int week = (date + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
string weekstr = "";
switch (week)
{
case 0: weekstr = "星期一";
break;
case 1: weekstr = "星期二";
break;
case 2: weekstr = "星期三";
break;
case 3: weekstr = "星期四";
break;
case 4: weekstr = "星期五";
break;
case 5: weekstr = "星期六";
break;
case 6: weekstr = "星期七";
break;
}
return weekstr;
}
}
} |
|