| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 473 人关注过本帖
标题:请高手帮忙改进一下 一个显示日期的小程序 自己写的感觉有点繁琐
只看楼主 加入收藏
心若定水
Rank: 1
来 自:河北联合大学轻工学
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-3-10
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
请高手帮忙改进一下 一个显示日期的小程序 自己写的感觉有点繁琐
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
goleft186
Rank: 2
等 级:论坛游民
帖 子:2
专家分:10
注 册:2012-3-12
收藏
得分:10 
只写一个方法,谁写谁繁琐
2012-03-12 09:11
goleft186
Rank: 2
等 级:论坛游民
帖 子:2
专家分:10
注 册:2012-3-12
收藏
得分:0 
有BUG
图片附件: 游客没有浏览图片的权限,请 登录注册
2012-03-12 09:22
lunajrq
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2012-3-16
收藏
得分:10 
回复 楼主 心若定水
楼主,帮你改了下,你看下行不

我也在学习c#。。。可以的话交流下

顺便帮你把写错的改了下


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=0, c=0, d=0, qqq=0, leapyear=0;
            bool flag;
            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)
                                leapyear = 1;
                            else
                                leapyear = 0;
                        }
                        else
                            leapyear = 1;
                    }
                    else
                        leapyear = 0;
                }
            }
            while (flag);

            //这里开始是我改的,感觉简单了点
            //get month

            do
            {
                Console.WriteLine("请输入月份:");
                a.month = Console.ReadLine();
                c = Int32.Parse(a.month);
                if (c < 1 || c > 12)
                {
                    Console.WriteLine("\a\a月份输入错误!请重新输入!");
                    flag = true;
                }
                else
                {
                    flag = false;
                    qqq = c % 2;
                    if (c == 2)
                    {
                        qqq = -2;
                    }
                }
            } while (flag);

            //get day
            do
            {
                Console.WriteLine("请输入日期:");
                a.day = Console.ReadLine();
                d = Int32.Parse(a.day);

                int max = 0;
                if (qqq != -2)
                {
                    leapyear = 0;
                }
                max = 30 + qqq + leapyear;

                if (d < 1 || (d > max))
                {
                    Console.WriteLine("\a\a日期输入错误!请重新输入!");
                    flag = true;
                }
                else
                {
                    flag = false;
                }
            } while (flag);
            Console.WriteLine("您输入的日期是:{0}年{1}月{2}日", a.year, a.month, a.day);                      //显示用户输入的日期
            Console.ReadLine();
        }
    }
}


















            
          /*  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();
        }
    }
}*/
2012-03-16 19:13
快速回复:请高手帮忙改进一下 一个显示日期的小程序 自己写的感觉有点繁琐
数据加载中...
 
   



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

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