首页 > 解决方案 > 为什么有些输入会通过 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);

标签: c#

解决方案


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


推荐阅读