我已經完成了一個名爲nextDay的方法,它應該增加用戶在控制檯中鍵入日期的那一天。該方法還根據輸入的內容更改年份並更改月份。例如。如果我在我的控制檯中輸入13/10/2012我希望輸出是14/10/2012,我希望根據幾個月和一年。任何方式,我可以實現這一點或修復我的當前方法,以便此輸出工作?目前我的程序工作正常,只檢索用戶輸入的日期,並且還檢查錯誤。我特別要求的是在用戶輸入之前的第二天。C#增量天數
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
}
}
}
}
}
有,爲什麼你想實現自己的日期處理的理由? DateTime讓你失望,還是這是一項學術演習? – spender
您忘記告訴我們您當前的實施*如何不按預期工作。有錯誤嗎?意外的輸出?當你調試這個,特別是它失敗?發生這種情況時的運行時間值是什麼? – David
日期工作正常,但是當我輸入日期時,我希望第二天。這是爲了實驗的目的,我想玩弄c# – ramteen1993