c# - 为什么有些输入会通过 while 循环?
问题描述
我最近的一项任务要求创建一个可以验证出生日期的程序,但是,每当我测试出生日期时,有些人会通过检查。例如,当我用 2022 年进行测试时,它允许它,即使我将程序设计为不允许 2021 年之后的几年,有什么建议吗?(PS 我试图在不使用 DateTime 函数的情况下完成它)。
int Year, Month, Date;
Console.Write("Please enter the year you were born: ");
Year = Convert.ToInt32(Console.ReadLine());
while (!(Enumerable.Range(1903, 2021).Contains(Year)))
{
Console.Write("It is impossible for you to have been born in that year, please try again.");
Console.Write("\nPlease enter the year you were born: ");
Year = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Please enter the month you were born (1-12): ");
Month = Convert.ToInt32(Console.ReadLine());
while (!(Enumerable.Range(1, 12).Contains(Month)))
{
Console.Write("It is impossible for you to have been born in that month, please try again.");
Console.Write("\nPlease enter the month you were born: ");
Month = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Please enter the date you were born: ");
Date = Convert.ToInt32(Console.ReadLine());
if (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12)
{
while (!(Enumerable.Range(1, 31).Contains(Date)))
{
Console.Write("It is impossible for you to have been born on that date, please try again.");
Console.Write("\nPlease enter the date you were born: ");
Date = Convert.ToInt32(Console.ReadLine());
}
}
else if (Month == 4 || Month == 6 || Month == 9 || Month == 11)
{
while (!(Enumerable.Range(1, 30).Contains(Date)))
{
Console.Write("It is impossible for you to have been born on that date, please try again.");
Console.Write("\nPlease enter the date you were born: ");
Date = Convert.ToInt32(Console.ReadLine());
}
}
else
{
if (Year % 4 == 0)
{
while (!(Enumerable.Range(1, 29).Contains(Date)))
{
Console.Write("It is impossible for you to have been born on that date, please try again.");
Console.Write("\nPlease enter the date you were born: ");
Date = Convert.ToInt32(Console.ReadLine());
}
}
else
{
while (!(Enumerable.Range(1, 28).Contains(Date)))
{
Console.Write("It is impossible for you to have been born on that date, please try again.");
Console.Write("\nPlease enter the date you were born: ");
Date = Convert.ToInt32(Console.ReadLine());
}
}
}
Console.WriteLine("Your date of birth has been validated and is: " + Date + "/" + Month + "/" + Year);
解决方案
You use incorrectly Enumerable.Range(start, count)
. Current
Enumerable.Range(1903, 2021)
means 2021
items (count) starting from 1903
(start). That's why the actual range will be [1903..3924]
and many unwanted values like 2222
will fit it. You, probably, want
Enumerable.Range(1903, 2021 - 1903 + 1)
I.E.
while (!(Enumerable.Range(1903, 2021 - 1903).Contains(Year)))
{
...
}
please, see the manual for details
推荐阅读
- mysql - 创建学说数据库时名称解析暂时失败
- c++ - std::set 外键使用自定义comaprator
- ios - SwiftUI 从 UIViewController 转到另一个视图
- python - 如何从包含数值的段落中提取句子。有一个约束。数值应介于 0 到 85 之间
- blazor - 如何将 SaveFileDialog 与 Blazor 服务器一起使用
- api - 如果涉及多个服务器调用,为什么在 REST API 中使用分页
- python - 使用 python/Pyspark 取消旋转具有 49 个类似类别的多列
- blazor - 如何确保 Blazor 渲染器识别集合更改
- google-sheets - 计算所拥有硬币的平均值
- javascript - 为什么这种 Luhn 算法的实现不起作用?