请高手帮忙改进一下 一个显示日期的小程序 自己写的感觉有点繁琐
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 日期显示v1._2加强版
{
public struct Date //声明结构体
{
public string year;
public string month;
public string day;
}
class Program
{
static void Main(string[] args)
{
Date a;
int b, c, d;
bool flag, leap = false;
do //输入年份并判断是否正确及平闰年
{
Console.WriteLine("请输入年份:");
a.year = Console.ReadLine();
b = Int32.Parse(a.year);
if (b <= 0)
{
Console.WriteLine("\a\a年份输入错误!请重新输入!");
flag = true;
}
else
{
flag = false;
if (b % 4 == 0) //看是判断平闰年
{
if (b % 100 == 0)
{
if (b % 400 == 0)
leap = true;
else
leap = false;
}
else
leap = true;
}
else
leap = false;
}
}
while (flag);
if (leap) //闰年的情况
{
do
{
Console.WriteLine("请输入月份:");
a.month = Console.ReadLine();
c = Int32.Parse(a.month);
if (c <= 0 || c >= 12)
{
Console.WriteLine("\a\a月份输入错误!请重新输入!*.*(一年就十二个月呀!)*.*");
flag = true;
}
else flag = false;
}
while (flag);
loop: Console.WriteLine("请输入日期:");
a.day = Console.ReadLine();
d = Int32.Parse(a.day);
switch (c) //判断输入月份、天数是否正
{
case 2:
{
if (d <= 0 || d >= 30)
{
Console.WriteLine("\a\a日期输入错误!{0}是闰年,二月只有29天!请重新输入!", b);
goto loop;
}
}; break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
if (d <= 0 || d >= 32)
{
Console.WriteLine("\a\a日期输入错误!{0}月只有31天!请重新输入!", c);
goto loop;
}
}; break;
case 4:
case 6:
case 9:
case 11:
{
if (d <= 0 || d >= 31)
{
Console.WriteLine("\a\a日期输入错误!{0}月只有30天!请重新输入!", c);
goto loop;
}
}; break;
default: ; break;
}
}
else //平年的情况
{
do
{
Console.WriteLine("请输入月份:");
a.month = Console.ReadLine();
c = Int32.Parse(a.month);
if (c <= 0 || c >= 12)
{
Console.WriteLine("\a\a月份输入错误!请重新输入!*.*(一年就十二个月呀!)*.*");
flag = true;
}
else flag = false;
}
while (flag);
loop: Console.WriteLine("请输入日期:");
a.day = Console.ReadLine();
d = Int32.Parse(a.day);
switch (c)
{
case 2:
{
if (d <= 0 || d >= 30)
{
Console.WriteLine("\a\a日期输入错误!{0}年是平年,二月只有28天!请重新输入!", b);
goto loop;
}
}; break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
if (d <= 0 || d >= 32)
{
Console.WriteLine("\a\a日期输入错误!{0}月只有31天!请重新输入!", c);
goto loop;
}
}; break;
case 4:
case 6:
case 9:
case 11:
{
if (d <= 0 || d >= 31)
{
Console.WriteLine("\a\a日期输入错误!{0}月只有30天!请重新输入!", c);
goto loop;
}
}; break;
default: ; break;
}
}
Console.WriteLine("您输入的日期是:{0}年{1}月{2}日", a.year, a.month, a.day); //显示用户输入的日期
Console.ReadLine();
}
}
}
请各位高手帮忙改进一下,谢了