| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 473 人关注过本帖
标题:请高手帮忙改进一下 一个显示日期的小程序 自己写的感觉有点繁琐
取消只看楼主 加入收藏
心若定水
Rank: 1
来 自:河北联合大学轻工学
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-3-10
结帖率:0
收藏
已结贴  问题点数:20 回复次数:0 
请高手帮忙改进一下 一个显示日期的小程序 自己写的感觉有点繁琐
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();
        }
    }
}



请各位高手帮忙改进一下,谢了
搜索更多相关主题的帖子: class void public 结构体 false 
2012-03-11 13:37
快速回复:请高手帮忙改进一下 一个显示日期的小程序 自己写的感觉有点繁琐
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019738 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved