【发布时间】:2015-11-20 03:13:23
【问题描述】:
我已经完成了一个名为 nextDay 的方法,它应该增加用户在控制台中输入日期的日期。该方法还根据输入的内容更改年份并更改月份。例如。如果我在 2012 年 13 月 10 日在控制台中输入,我希望输出为 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
}
}
}
}
}
【问题讨论】:
-
您尝试实现自己的日期处理是否有原因? DateTime 是否让您失望了,或者这是一个学术练习?
-
您忘记告诉我们您当前的实现如何没有按预期工作。有错误吗?出乎意料的输出?当您调试它时,它具体在哪里失败?发生这种情况时的运行时值是多少?
-
日期工作正常,但是当我输入日期时,我想要第二天。这是出于实验目的,我想玩 c#
-
@ramteen1993:好的,你在哪里尝试计算“第二天”?如果您出于学术原因这样做,那么要求我们为您这样做有点违背了目的。如果你想让别人为你做这件事,
DateTime对象已经做到了。 -
在关注月份和年份的同时获得下一天,最好的方法是
date.AddDays(1),然后根据需要进行格式化。喜欢date.AddDays(1).ToString("dd/MM/yyyy")
标签: c# .net visual-studio