如何找到下一个1000天的倍数和相应的日期?



我必须编写一个程序,询问某人的生日,并计算第二天是他们转1000天的倍数。

我已经写了下面的代码:

using System;
Console.WriteLine("welkom bij de verKdag calculator, vul de volgende getallen in:");
Console.WriteLine("wat is je geboortedag? ");
String s = Console.ReadLine();
int d = int.Parse(s);
Console.WriteLine("wat is je geboortemaand");
int m = int.Parse( Console.ReadLine() );
Console.WriteLine("wat is je geboortejaar");
int y = int.Parse(Console.ReadLine());
DateTime gebdat = new DateTime(y, m, d);
DateTime vandaag = DateTime.Today;
var diffofdates = vandaag.Subtract(gebdat);
Console.WriteLine(diffofdates);
Console.ReadLine();

Now<我需要找到一种方法来找到1000与diffofdates最接近的倍数并计算哪个日期与之对应,我该怎么做?>

使用模数运算符计算剩余天数,然后使用该操作符获得目标日期:

int daysRemaining = 1000 - (int)diffofdates.TotalDays % 1000;
DateTime targetDate = DateTime.Today.AddDays(daysRemaining);
Console.WriteLine("You'll reach a multiple of 1000 days " + 
$"on {targetDate.ToShortDateString()} (in {daysRemaining} days from now)");

作为旁注,考虑使用int.TryParse()而不是int.Parse(),并验证用户输入是有效日期。或者,您可以使用DateTime.TryParse()或(最好)DateTime.TryParseExact(),并允许用户一次性输入他们的出生日期。

最新更新