年月日判断并输出第多少天
方法一:
while (true) { Console.Write("输入年:"); int year = int.Parse(Console.ReadLine()); Console.Write("输入月:"); int month = int.Parse(Console.ReadLine()); Console.Write("输入日:"); int day = int.Parse(Console.ReadLine()); bool isok = false;//用来记录日期是否正确 bool isrun = false; if (year >= 1759 && year < 9999) { if (month >= 1 && month <= 12) { if (month == 2) { if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { if (day >= 1 && day <= 29) { isok = true; isrun = true; } else { Console.WriteLine("应在29天范围之内"); } } else { if (day >= 1 && day <= 28) { isok = true; } else { Console.WriteLine("应在28天范围之内"); } } } if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if (day >= 1 && day <= 31) { isok = true; } else { Console.WriteLine("日期应在31天之内"); } } if (month == 4 || month == 6 || month == 9 || month == 11) { if (day >= 1 && day <= 30) { isok = true; } else { Console.WriteLine("日期应在30天之内"); } } } else { Console.WriteLine("月份不正确"); } } else { Console.WriteLine("年份不正确"); } int sumday = 0;//第多少天 if (isok)//日期正确,计算天数 { if (isrun)//是闰年 { if (month == 1) { sumday = day; } if (month == 2) { sumday = 31 + day; } if (month == 3) { sumday = 31 + 29 + day; } if (month == 4) { sumday = 31 + 29 + 31 + day; } if (month == 5) { sumday = 31 + 29 + 31 + 30 + day; } if (month == 6) { sumday = 31 + 29 + 31 + 30 + 31 + day; } if (month == 7) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + day; } if (month == 8) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day; } if (month == 9) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day; } if (month == 10) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; } if (month == 11) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; } if (month == 12) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; } } else { if (month == 1) { sumday = day; } if (month == 2) { sumday = 31 + day; } if (month == 3) { sumday = 31 + 28 + day; } if (month == 4) { sumday = 31 + 28 + 31 + day; } if (month == 5) { sumday = 31 + 28 + 31 + 30 + day; } if (month == 6) { sumday = 31 + 28 + 31 + 30 + 31 + day; } if (month == 7) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + day; } if (month == 8) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day; } if (month == 9) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day; } if (month == 10) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; } if (month == 11) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; } if (month == 12) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; } } Console.WriteLine("第{0}天", sumday); } else { Console.WriteLine("已经日期不正确,算不出来!"); } Console.ReadLine(); } }
方法二:
while (true) //输入年月日判断是该年的第几天
{
int year, mooth, day;
Console.Write("请输入年份:");
year=int.Parse( Console.ReadLine());
if ((year > 1759 && year <= 9999) && (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)))
{
Console.Write("请输入月份;");
mooth = int.Parse(Console.ReadLine());
if (mooth >= 1 && mooth <= 12)
{
Console.Write("请输入日期:");
day = int.Parse(Console.ReadLine());
int m1=31, m2=28,m3=31, m4=30, m5=31, m6=30, m7=31, m8=31, m9=30, m10=31, m11=30;
if(mooth==1&&(day>=1&&day<=31))
{
Console.WriteLine("这是"+year+"第"+day+"天");
}
else if (mooth == 2 && (day >= 1 && day <= 28))
{
Console.WriteLine("这是" + year + "第" +(m1+day) + "天");
}
else if (mooth == 3 && (day >= 1 && day <= 31))
{
Console.WriteLine("这是" + year + "第" + (m1+m2+ day) + "天");
}
else if (mooth == 4 && (day >= 1 && day <= 30))
{
Console.WriteLine("这是" + year + "第" + (m1+m2+m3+ day) + "天");
}
else if (mooth == 5 && (day >= 1 && day <= 31))
{
Console.WriteLine("这是" + year + "第" + (m1+m2+m3+m4 + day) + "天");
}
else if (mooth == 6 && (day >= 1 && day <= 30))
{
Console.WriteLine("这是" + year + "第" + (m1+m2+m3+m4+m5 + day) + "天");
}
else if (mooth == 7 && (day >= 1 && day <= 31))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + m5+m6 + day) + "天");
}
else if (mooth == 8 && (day >= 1 && day <= 31))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + m5 + m6+m7 + day) + "天");
}
else if (mooth == 9 && (day >= 1 && day <= 30))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 +m8+ day) + "天");
}
else if (mooth == 10 && (day >= 1 && day <= 31))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8+m9 + day) + "天");
}
else if (mooth == 11 && (day >= 1 && day <= 30))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9+m10 + day) + "天");
}
else if (mooth == 12 && (day >= 1 && day <= 31))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10+m11 + day) + "天");
}
else
{
Console.WriteLine("输入日期有误");
}
}
else
{
Console.WriteLine("输入月份有误");
}
}
else if ((year > 1759 && year <= 9999) && !((year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))))
{
Console.Write("请输入月份;");
mooth = int.Parse(Console.ReadLine());
if (mooth >= 1 && mooth <= 12)
{
Console.Write("请输入日期:");
day = int.Parse(Console.ReadLine());
int m1 = 31, m2 = 29, m3 = 31, m4 = 30, m5 = 31, m6 = 30, m7 = 31, m8 = 31, m9 = 30, m10 = 31, m11 = 30;
if (mooth == 1 && (day >= 1 && day <= 31))
{
Console.WriteLine("这是" + year + "第" + day + "天");
}
else if (mooth == 2 && (day >= 1 && day <= 28))
{
Console.WriteLine("这是" + year + "第" + (m1 + day) + "天");
}
else if (mooth == 3 && (day >= 1 && day <= 31))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + day) + "天");
}
else if (mooth == 4 && (day >= 1 && day <= 30))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + day) + "天");
}
else if (mooth == 5 && (day >= 1 && day <= 31))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + day) + "天");
}
else if (mooth == 6 && (day >= 1 && day <= 30))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + m5 + day) + "天");
}
else if (mooth == 7 && (day >= 1 && day <= 31))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + m5 + m6 + day) + "天");
}
else if (mooth == 8 && (day >= 1 && day <= 31))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + day) + "天");
}
else if (mooth == 9 && (day >= 1 && day <= 30))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + day) + "天");
}
else if (mooth == 10 && (day >= 1 && day <= 31))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + day) + "天");
}
else if (mooth == 11 && (day >= 1 && day <= 30))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + day) + "天");
}
else if (mooth == 12 && (day >= 1 && day <= 31))
{
Console.WriteLine("这是" + year + "第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + day) + "天");
}
else
{
Console.WriteLine("输入日期有误");
}
}
else
{
Console.WriteLine("输入月份有误");
}
}
else
{
Console.WriteLine("输入年份有误");
}
}
}