C# 当我想要实现try-and-catch异常时,代码块会重复
我正在编写一个C#console应用程序,您可以在其中以yyyy/mm/dd格式输入姓名和出生日期,让控制台以年和月为单位告诉您的年龄。现在我已经弄明白了那个部分,它工作了。直到我还试图实现一个try-and-catch异常来检查您输入的日期的格式是否正确。如果没有,它应该告诉你重试3次,然后告诉你日期格式不正确,然后退出应用程序 现在的问题是该应用程序可以正常工作,但它会告诉你日期格式是不正确的,即使它不是。然后,在通过请求姓名和出生日期的应用程序循环3次后,仍然会继续提供正确的输出。这是我到目前为止的代码(我知道一年一个月的输出有点混乱,我只是花了太长时间测试太多的东西,现在无法尝试并更改它,但我对改进和更改持开放态度): 如您所见,它3次询问姓名和出生日期,但仍然告诉我日期格式不正确,然后它给出了正确的输出(您的出生日期是:1997/02/13 12:00:00 AM 7294 年:20个月:0)3次 它应该询问一次,输出一次,但我不知道怎么做。任何帮助都将不胜感激 这里是控制台输出的屏幕截图,如果有帮助的话C# 当我想要实现try-and-catch异常时,代码块会重复,c#,console-application,C#,Console Application,我正在编写一个C#console应用程序,您可以在其中以yyyy/mm/dd格式输入姓名和出生日期,让控制台以年和月为单位告诉您的年龄。现在我已经弄明白了那个部分,它工作了。直到我还试图实现一个try-and-catch异常来检查您输入的日期的格式是否正确。如果没有,它应该告诉你重试3次,然后告诉你日期格式不正确,然后退出应用程序 现在的问题是该应用程序可以正常工作,但它会告诉你日期格式是不正确的,即使它不是。然后,在通过请求姓名和出生日期的应用程序循环3次后,仍然会继续提供正确的输出。这是我到
我对你的代码做了一些修改 我没有检查你剩下的代码,只是想问一下日期
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;
}
private static void Main(字符串[]args)
{
输入日期();
}
私有静态void EnterDate()
{
Console.WriteLine(“输入您的姓名:”);
var userName=Console.ReadLine();
//询问日期
var enteredDoBString=AskForDate();
如果(enteredDoBString==null)
返回;
DateTime enteredDoB=DateTime.Parse(enteredDoBString);
WriteLine($“您的DoB是:{enteredDoB}”);
DateTime dateToday=DateTime.Today;
如果(dateToday12)
{
Console.WriteLine($“年:{年-1},月:{12-(月-12}”);
}
否则,如果(月数<0)
{
Console.WriteLine($“年:{年},月:{月-月}”);
}
其他的
{
Console.WriteLine($“年:{年},月:{月}”);
}
Console.ReadKey();
Console.ReadKey();
}
私有静态字符串AskForDate()
{
var计数=0;
而(计数+++<3)
{
尝试
{
Console.WriteLine(“输入您的出生日期,格式为yyyy/mm/dd:”);
return DateTime.Parse(Console.ReadLine(),System.Globalization.CultureInfo.InvariantCulture).ToSortDateString();
}
捕获(例外e)
{
控制台写入线(e.Message);
//如果日期输入错误3次,应用程序应退出。。
}
}
Console.WriteLine(“\a截止日期格式仍然不正确-按任意键退出应用程序”);
Console.ReadKey();
返回null;
}
我所做的:
我删除了您的函数parseDateString
,并创建了一个名为AskForDate
的新函数。这个函数,如名称所示,向用户询问日期,在这个函数中,检查输入的日期是否有效。
我认为最好检查和询问相同的函数。
如果用户输入的日期正确,则返回的日期为ToSortDateString
。如果日期不正确,函数将再询问两次,第三次返回null
然后,在EnterDate
函数中,如果AskForDate
函数返回null
,则程序退出
我还更改了一些字符串插值,并删除了一些变量。问题是您正在检查
日期字符串
,而不是输入的字符串
,然后最终使用输入的字符串
。删除dateString
并将enteredDoBstring
传递给该方法。为什么输入的日期字符串有两行代码,即enteredDoBstring和
dateString`?@ChibuezeOpata和awh112感谢大家,我这么做之后,它现在工作得很好。谢谢你的帮助。真不敢相信我没看到哈哈,我还是个新手,还在学C#。
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
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;
}