【问题标题】:relevant loop and leap year c#相关循环和闰年c#
【发布时间】:2014-05-12 00:48:13
【问题描述】:

我要提前说,我是编程初学者,这个问题似乎无关紧要。但是,我真的很想知道在这种情况下如何进行。

这是我的代码:

string startdate;

Console.WriteLine("Please, type in your birthdate (dd-mm-yyyy)");
startdate = Console.ReadLine();
DateTime bday = DateTime.Parse(startdate);
        // prob 1.
DateTime now = DateTime.Now;
TimeSpan ts1 = now.Subtract(bday);
DateTime dt = new DateTime(0001, 01, 01);
TimeSpan ts2 = new TimeSpan(365, 0, 0, 0);
        //prob2.
TimeSpan ts3 = new TimeSpan(3650, 0, 0, 0);
dt = dt + ts1 - ts2;
Console.WriteLine("Your current age is:{0}", dt.ToString("yy"));
dt = dt + ts3;
Console.WriteLine("Your Age after 10 years will be:{0}", dt.ToString("yy"));

问题 1:我想创建一个循环,如果控制台中提供的信息与 dd-mm-yyyy 不同,则再次重复整个过程。

问题2:我想看看下一年(从当前一年)是否是闰年,从而知道ts2应该是365天还是366天。

提前谢谢你。

【问题讨论】:

  • 并非所有人都可以访问 pastebin,您也可以发布代码吗?
  • 我不是 C# 程序员,但更标准的做法是使用可能在 DateTime 类上可用的 addYears() 方法。
  • @Bathsheba 确实如此,除了它被称为 AddYears 以匹配 .NET 的正常大写规则。
  • DateTime.IsLeapYear(year) ?

标签: c# loops leap-year


【解决方案1】:

回复。问题一:

看看DateTime.TryParseExact:这允许您指定格式,而不是抛出异常,在输入格式不匹配时返回 false。因此

DateTime res;
String inp;
do {
  inp = Console.ReadLine("Date of birth: ");
} while (!DateTime.TryParseExact(inp, "yyyy-MM-dd", CultureInfo.CurrentCulture, DateTimeStyles.None, out res));

关于问题 2:请参阅 Q 上的 cmets 中所述的 DateTime.AddYears

【讨论】:

  • 这在第一个问题上帮助了我很多。谢谢你的回答。
【解决方案2】:

感谢Framewrok,闰年问题并不是真正的问题

int daysToAdd = 0;
for(int i = 1; i <= 10; i++)
   daysToAdd += (DateTime.IsLeapYear(DateTime.Today.Year + i) ? 366 : 365);

第一个问题可以用

解决
DateTime inputDate;
while(true)
{
    Console.WriteLine("Please, type in your birthdate (dd-mm-yyyy)");
    string startdate = Console.ReadLine();
    if(DateTime.TryParseExact(startdate, "dd-MM-yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out inputDate))
        break;
}

【讨论】:

  • 您如何将 DateTime.IsLeapYear 的直接使用应用于 10 年间隔(提示十年可以包含一个、两个或三个闰年)。
  • 谢谢。这真的帮助我解决了第二个问题。请注意,您在“?”之前错过了一个“)”。
【解决方案3】:

问题一:

这可以使用 while 循环来解决。

while(!DateTime.Parse(startdate))// The "!" is for NOT
{
    Console.WriteLine("Incorrect format please type your birthday again(dd-mm-yyyy)");
    startdate = Console.ReadLine();
}

然而这带来了另一个问题 DateTime.Parse 在字符串不正确时会抛出错误。(http://msdn.microsoft.com/en-us/library/1k1skd40%28v=vs.110%29.aspx)

为了解决这个问题,您需要使用 try catch 子句来“捕获”错误。

在此处查看更多详细信息(http://msdn.microsoft.com/en-us/library/0yd65esw.aspx) 因此代码将如下所示:

bool isCorrectTime = false;
while(!isCorrectTime) // The "!" is for NOT
{
    try
    {
        Console.WriteLine("Incorrect format please type your birthday again(dd-mm-yyyy)");
        startdate = Console.ReadLine();
        isCorrectTime = true; //If we are here that means that parsing the DateTime
        // did not throw errors and therefore your time is correct!
    }
    catch
    {
        //We leave the catch clause empty as it is not needed in this scenario
    }

}

对于问题 2,请参阅史蒂夫的回答。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多