用于计算活动开始前的时间(天)的程序

 收藏

我写了一个程序来计算事件发生前的天数,可能有几次事件,我想知道如何减少或简化此代码。

   namespace Determining_the_time_before_the_event_starts
{
    class Program
    {
        struct Event
    {
        public string name;
        public DateTime time;
    }
    static Event[] InPutEvents()
    {
        Console.Write("Number of events: ");
        int n, year, month, day, hour, min;
        n = int.Parse(Console.ReadLine());
        Event[] time = new Event[n];
        for(int i=0; i<n; i++)
        {
                Console.Write("Event name: ");
                time[i].name = Console.ReadLine();
                Console.Write("Event start year: ");
                year = int.Parse(Console.ReadLine());
                Console.Write("Month of the beginning of the event: ");
                month = int.Parse(Console.ReadLine());
                Console.Write("The first day of the event: ");
                day = int.Parse(Console.ReadLine());
                Console.Write("Watch the beginning of the event: ");
                hour = int.Parse(Console.ReadLine());
                Console.Write("Minutes before the event starts: ");
                min = int.Parse(Console.ReadLine());
                time[i].time = new DateTime(year, month, day, hour, min, 0);
                Console.WriteLine();
            }
            return time;
    }
        static void Main(string[] args)
        {
            Event[] time = InPutEvents();
            Console.WriteLine();
            BeginEvent(time);
            Console.ReadLine();
        }
        static void BeginEvent (Event[] time)
        {
            DateTime now = DateTime.Now;
            for(int i = 0; i < time.Length; i++)
            {
                SearchTime(time[i], now);
                Console.WriteLine();
            }
        }
        static void SearchTime(Event time, DateTime now)
        {
            int year, month, day, hour, min;
            year = time.time.Year - now.Year;
            month = time.time.Month - now.Month;
            day = time.time.Day - now.Day;
            hour = time.time.Hour - now.Hour;
            min = time.time.Minute - now.Minute;
            if (min < 0)
            {
                min += 60;
                hour--;
            }
            if (min > 60)
            {
                min -= 60;
                hour++;
            }
            if (hour < 0)
            {
                hour += 24;
                day--;
            }
            if (hour > 24)
            {
                 hour -= 24;
                day++;
            }
            if (day < 1)
            {
                day += 31;
                month--;
            }
            if (day > 31)
            {
                day -= 31;
                month++;
            }
            if (month < 1)
            {
                month += 12;
                year--;
            }
            if (month > 12)
            {
                month -= 12;
                year++;
            }
            day += (month * 31) + (year * 365);
            Console.WriteLine($"Event {time.name} starts in {day} days, {hour}hours, and {min}m. ");
        }
    }
}
回复
  • Subtracting a DateTime object from another DateTime Object creates a TimeSpan object. The TimeSpan object has properties like Days, Hours, Minutes and so forth.

    只需使用这些属性即可构建所需的解决方案。

        private static void SearchTime(Event event, DateTime now)
        {
            var diff = event.time - now;
    
            Console.WriteLine($"Event {event.name} starts in {diff.Days} days, {diff.Hours}hours, and {diff.Minutes}m. ");
        }