C#增加天数

本文关键字:增加 | 更新日期: 2023-09-27 18:22:37

我已经完成了一个名为nextDay的方法,它应该增加用户在控制台中键入日期的日期。该方法还根据输入的内容更改年份并更改月份。例如,如果我在控制台中输入2012年10月13日,我希望输出为2012年10日14日,并且我希望根据月份和年份进行输出。我有什么办法可以实现这一点或修复我当前的方法以使输出正常工作?目前,我的程序运行良好,只检索用户键入的日期,还进行错误检查。我特别要求的是在用户输入内容的第二天之前获得。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace date
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("'t't't't't'tNextDate Application'n't't't't't-------------------------------------"); // title
            Console.WriteLine("This application will allow you to enter a valid date and this will get the next day.'n"); // intro of what the application does
            Console.WriteLine("please enter date as dd/MM/yyyy'n"); // tells user to input a date in the formats
            int day; // sets variable
            int month; // sets variable
            int year; // sets variable
            string[] read = Console.ReadLine().Split('/');  // "/" can be read from each value and sets new array
            day = int.Parse(read[0]); // day is first position in array
            month = int.Parse(read[1]); // month is second position in array
            year = int.Parse(read[2]); // year is third position in array
            try
            {
                Date date = new Date(day, month, year); // initialises a new date class
                Console.WriteLine("{0}/{1}/{2}", date.Day, date.Month, date.Year); // given to user as "day/month/year"
                Console.ReadLine(); // reads the line
            }
            catch (ArgumentOutOfRangeException exc)
            {
                Console.WriteLine(exc.Message); // states the message for ArgumentOutOfRangeException
                Console.Read(); // breaks
            }     
        }
        class Date
        {
            private int _month; // 1-12
            private int _day; // 1-31 depending on month
            private int _year; // sets the year
            public Date(int day, int month, int year)
            {
                Month = month;
                Day = day;         
                Year = year;
            }
            public void nextDay()
            {
                try
                {
                    _day = _day++;
                }
                catch (ArgumentOutOfRangeException)
                {
                    if (_month == 12) // e.g if month dec 31st then it does a try 
                    {
                        _month = 1; // month then = 1
                        _year++; // year increments
                    }
                    else
                    {
                        _month++; 
                    }
                    _day = 1;
                }
            }
            public int Year
            {
                get { return _year; }
                set
                {
                    if (value >= 1820 && value <= 2020) // if value is higher than or equal to 1820 and less than or equal to 2020
                        _year = value; // sets year as value
                    else
                        throw new ArgumentOutOfRangeException("Year must be between 1820 and 2020"); // throws an exception
                }
            }
            public int Month
            {
                get { return _month; }
                set
                {
                    if (value > 0 && value <= 12) // if value is higher than 0 and less than or equal to 12
                        _month = value; // sets month as value
                    else 
                        throw new ArgumentOutOfRangeException("Month must be between 1-12"); // throws an exception
                }
            }
            public int Day
            {
                get { return _day; }
                set
                {
                    int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // array of days of max each month
                    if (value > 0 && value <= days[_month]) // if value is higher than 0 and less than or equal to days of month
                        _day = value; // sets day as value
                    else if (_month == 2 && value == 29 && // else if month is equal to 2 and value is equal to 29
                        _year % 400 == 0 || (_year % 4 == 0 && _year % 100 != 0)) 
                        _day = value;
                    else
                        throw new ArgumentOutOfRangeException("Day is out of range"); // throws an exception
                }
            }
        }
    }
}

C#增加天数

务实的答案:

public Date AddDay()
{
    var tomorrow = new DateTime(this._year, this._month, this._year).AddDays(1).Date;
    return new Date(tomorrow.Day, tomorrow.Month, tomorrow.Year);
}

或者如果你喜欢突变

public void AddDay()
{
    var tomorrow = new DateTime(this._year, this._month, this._day).AddDays(1).Date;
    this._day = tomorrow.Day;
   //etc...
}

试试这个:

    private static void Main(string[] args)
    {
        DateTime theDateTime = DateTime.Now;
        DateTime tomorrow = NextDay(theDateTime);
        Console.WriteLine($"Now: {theDateTime:yy/MM/dd}'t'tTomorrow:" +
            $"{tomorrow:yy/MM/dd}");
    }
    private static DateTime NextDay(DateTime source)
    {
        return source + new TimeSpan(1, 0, 0, 0);
    }

根据需要调整代码。将输入分析为日期时间。使用方法。相应地分析输出。

您的问题是所有的逻辑都在属性中,但在nextDay方法中,您却在增加本地字段。除此之外,以下内容不会产生任何变化。

_day = _day++;

因为这相当于

int temp = _day;
_day = _day + 1;
_day = temp;