我正在编写一个C#控制台应用程序,您可以用yyyy / mm / dd格式输入您的姓名和生日,让控制台告诉您年龄和月份的年龄。现在我已经找到了那个部分并且它有效。直到我尝试执行try和catch异常来检查您输入的日期是否格式正确。如果没有,它应该告诉你再次尝试3次,然后告诉你日期格式不正确然后退出应用程序。
现在问题是应用程序的工作类型,但它告诉你日期格式不正确,即使它不是。然后在循环通过要求姓名和出生日期3次的应用程序后仍然继续提供正确的输出。这是我到目前为止的代码(我知道年份输出有点乱)我花了太长时间测试太多东西试图改变它现在,我愿意接受改进和改变):
namespace CSConsoleDateTimeTypes
{
class Program
{
static int Count = 0;
static void Main(string[] args)
{ EnterDate(); }
static void EnterDate()
{
string userName, enteredDoBString;
Console.WriteLine("Enter your name:");
userName = Console.ReadLine();
Console.WriteLine("Enter your date of birth in the format yyyy/mm/dd:");
enteredDoBString = Console.ReadLine();
string dateString = Console.ReadLine();
parseDateString(dateString);
DateTime enteredDoB = DateTime.Parse(enteredDoBString);
Console.WriteLine("Your DoB is: {0}", enteredDoB);
DateTime dateToday = DateTime.Today;
if (dateToday < enteredDoB)
{
DateTime date4 = dateToday;
dateToday = enteredDoB;
enteredDoB = date4;
}
TimeSpan ts = dateToday - enteredDoB;
//total days (irrelevant to the application though)
Console.WriteLine(ts.TotalDays);
//total years
int years = dateToday.Year - enteredDoB.Year;
int months = 0;
//Total months
if (dateToday.Month < enteredDoB.Month)
{
months = 12 - dateToday.Month + enteredDoB.Month;
}
else
{
months = enteredDoB.Month - dateToday.Month;
}
if (months > 12)
{
Console.WriteLine("Years: {0}, Months: {1}", years - 1, 12 - (months - 12));
}
else if (months < 0)
{
Console.WriteLine("Years: {0}, Months: {1}", years, months - months);
}
else
{
Console.WriteLine("Years: {0}, Months: {1}", years, months);
}
Console.ReadKey();
Console.ReadKey();
}
static void parseDateString(string datestring)
{
try
{
DateTime date3 = DateTime.Parse(datestring, System.Globalization.CultureInfo.InvariantCulture);
date3.ToShortDateString();
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
//if date was entered incorrectly 3 times, the application should exit..
Count++;
if (Count < 3)
{
EnterDate();
}
else
{
Console.WriteLine("\aSorry date still not in correct format - Press any key to exit the application");
Console.ReadKey();
}
}
}
}
}
这是我启动应用程序时输出的结果,它在输入后完成所有代码的运行:
Enter your name:
gerrit
Enter your date of birth in the format yyyy/mm/dd:
1997/02/13
String was not recognized as a valid DateTime.
Enter your name:
gerrit
Enter your date of birth in the format yyyy/mm/dd:
1997/02/13
String was not recognized as a valid DateTime.
Enter your name:
gerrit
Enter your date of birth in the format yyyy/mm/dd:
1997/02/13
String was not recognized as a valid DateTime.
Sorry date still not in correct format - Press any key to exit the application
Your DoB is: 1997/02/13 12:00:00 AM
7294
Years: 20, Months: 0
Your DoB is: 1997/02/13 12:00:00 AM
7294
Years: 20, Months: 0
Your DoB is: 1997/02/13 12:00:00 AM
7294
Years: 20, Months: 0
正如您所看到的那样,它要求出生姓名和出生日期3次,并且仍然告诉我日期格式不正确,那么它会给出正确的输出(您的DoB是:1997/02/13 12:00:00 AM 7294 年:20,月:0)3次
它应该询问一次并输出一次,但我无法弄清楚如何做到这一点。任何帮助都将受到高度赞赏。
以下是控制台输出的屏幕截图,如果它有帮助的话
答案 0 :(得分:0)
我改变了你的代码。
我没有检查过你的其余代码,只是询问了日期。
private static void Main(string[] args)
{
EnterDate();
}
private static void EnterDate()
{
Console.WriteLine("Enter your name:");
var userName = Console.ReadLine();
// ask for date
var enteredDoBString = AskForDate();
if (enteredDoBString == null)
return;
DateTime enteredDoB = DateTime.Parse(enteredDoBString);
Console.WriteLine($"Your DoB is: {enteredDoB}");
DateTime dateToday = DateTime.Today;
if (dateToday < enteredDoB)
{
DateTime date4 = dateToday;
dateToday = enteredDoB;
enteredDoB = date4;
}
TimeSpan ts = dateToday - enteredDoB;
// total days (irrelevant to the application though)
Console.WriteLine(ts.TotalDays);
// total years
var years = dateToday.Year - enteredDoB.Year;
var months = 0;
// Total months
if (dateToday.Month < enteredDoB.Month)
{
months = 12 - dateToday.Month + enteredDoB.Month;
}
else
{
months = enteredDoB.Month - dateToday.Month;
}
if (months > 12)
{
Console.WriteLine($"Years: {years - 1}, Months: {12 - (months - 12)}");
}
else if (months < 0)
{
Console.WriteLine($"Years: {years}, Months: {months - months}");
}
else
{
Console.WriteLine($"Years: {years}, Months: {months}");
}
Console.ReadKey();
Console.ReadKey();
}
private static string AskForDate()
{
var count = 0;
while (count++ < 3)
{
try
{
Console.WriteLine("Enter your date of birth in the format yyyy/mm/dd:");
return DateTime.Parse(Console.ReadLine(), System.Globalization.CultureInfo.InvariantCulture).ToShortDateString();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
//if date was entered incorrectly 3 times, the application should exit..
}
}
Console.WriteLine("\aSorry date still not in correct format - Press any key to exit the application");
Console.ReadKey();
return null;
}
我做了什么:
我删除了您的功能parseDateString
并创建了一个名为AskForDate
的新功能。与名称一样,此函数向用户询问日期,并且在此函数内部,如果有效,则检查输入的日期。
我认为最好检查并询问相同的功能。
如果用户输入的日期正确,则日期将返回ToShortDateString
。如果日期不正确,该函数会再问两次,第三次会返回null
。
然后,在EnterDate
函数中,如果从null
函数返回AskForDate
,程序将退出。
我还更改了一些字符串插值,并删除了一些变量。