标题:请高手帮忙改进一下 一个显示日期的小程序 自己写的感觉有点繁琐
只看楼主
心若定水
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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-362819-1-1.html




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

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