我做了一个名为nextDay的方法,它应该增加用户在控制台中键入日期的那一天。该方法还根据输入的内容更改年份并更改月份。例如。如果我进入我的控制台13/10/2012我希望输出为14/10/2012,我希望根据月份和年份。我可以用任何方式实现这个或修复我当前的方法,以使此输出有效吗?目前我的程序工作正常,只检索用户键入的日期,并进行错误检查。我特别要求的是在用户输入之前的第二天。
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
}
}
}
}
}
答案 0 :(得分:0)
务实的答案:
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...
}
答案 1 :(得分:-1)
试试这个:
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);
}
根据需要调整您的代码。将输入解析为日期时间。使用方法。相应地解析输出。
答案 2 :(得分:-1)
您的问题是您的所有逻辑都在您的属性中,但在您的foreach (ContactResponse cr in lstContactResponse)
{
ContactResponseRecord crr = new ContactResponseRecord() {
ContactId = cr.ContactId,
ActivityDate = cr.ResponseDate,
LinkClicked = cr.Referrer};
var vJourneyNodeData = from x in lstJourneyNodeData where x.Id == cr.GrandparentId select x;
if(null != vJourneyNodeData && vJourneyNodeData.Count() > 0)
{
jnd = vJourneyNodeData.FirstOrDefault();
crr.CampaignWaveId = "J" + jnd.Id;
crr.WaveDescription = jnd.Label;
}
var vJourney = from x in lstJourney where x.Id == jnd.JourneyId select x;
if (null != vJourney && vJourney.Count() > 0)
{
j = vJourney.FirstOrDefault();
crr.OptTopic = j.TopicId;
}
var vCampaignElement = from x in lstCampaignElement where x.LinkedJourney == j.Name select x;
if (null != vCampaignElement && vCampaignElement.Count() > 0)
{
ce = vCampaignElement.FirstOrDefault();
crr.Ccg_Id = ce.CCGId;
crr.ElementDescription = ce.Description.ToString();
crr.CampaignElementId = ce.Id;
var vCampaign = from x in lstCampaign where x.Id == ce.CampaignId select x;
if (null != vCampaign && vCampaign.Count() > 0)
{
c = vCampaign.FirstOrDefault();
crr.ActivityDate = c.AtTaskId;
crr.BrandName = c.Brand;
crr.CampaignId = c.Id;
crr.CampaignName = c.Name;
crr.CompanyName = c.Company;
}
}
方法中,您正在增加本地字段。最重要的是,以下内容将不会有任何变化。
nextDay
因为这相当于
_day = _day++;