为什么我的do-while循环中的continue关键字不起作用?

时间:2019-04-19 04:59:28

标签: c# while-loop

我刚刚开始学习C#,并且试图创建一个非常简单的程序来了解特定日期的DayOfWeek。 do-while循环中的continue关键字不适用于错误检查。

我尝试通过将条件直接包含在while循环内来进行错误检查的另一种方式,但是我很好奇为什么continue无法正常工作。

class Program
    {
        static void Main(string[] args)
        {
            int yearInput, monthInput, dateInput;
            Console.WriteLine("We can tell you any day of any date");
            bool correctInput;


            //the problem starts at dateInput request (the third do while loop)

            do { Console.WriteLine("\nSet a year :");
                correctInput = int.TryParse(Console.ReadLine(), out yearInput);

                if (!correctInput)
                {
                    Console.WriteLine("Incorrect Input!");

                }
            }

            while (!correctInput);

            // this part is where is starts

            do
            {
                Console.WriteLine("\nSet a month :");
                correctInput = int.TryParse(Console.ReadLine(), out monthInput);
                if (!correctInput || monthInput < 1 || monthInput > 12)
                {
                    Console.WriteLine("Incorrect Input!");

                }
            }
            while (!correctInput || monthInput < 1 || monthInput > 12);



            do
            {
                Console.WriteLine("\nSet a date :");
                correctInput = int.TryParse(Console.ReadLine(), out dateInput);
                if (!correctInput || dateInput > 31 || dateInput < 1)
                {
                    Console.WriteLine("Incorrect Input!");

                }
                else
                {

                    if (dateInput > DateTime.DaysInMonth(yearInput, monthInput))
                    {
                        Console.WriteLine("The date doesn't reach that many!");
                        continue;


                    }

                }
            } while (!correctInput || dateInput > 31 || dateInput < 1);

            DateTime day = getDayofDate(yearInput, monthInput, dateInput);
            Console.WriteLine("\nIt is {0}.", day.DayOfWeek);
            Console.ReadKey();

        }
        static public DateTime getDayofDate (int year, int month, int day)
        {
            return new DateTime(year, month, day);
        }


    }

我希望它在遇到错误后会重复循环,但是它显示的是ArgumentsOutOfRangeException。

1 个答案:

答案 0 :(得分:2)

continue 可以使用,但仍会检查循环条件。但是,条件是false-循环停止!要解决此问题,您可以将correctInput设置为false,还可以删除continue-因为if之后没有其他要执行的语句(计算机自动转到下一个迭代):

        do
        {
            Console.WriteLine("\nSet a date :");
            correctInput = int.TryParse(Console.ReadLine(), out dateInput);
            if (!correctInput || dateInput > 31 || dateInput < 1)
            {
                Console.WriteLine("Incorrect Input!");

            }
            else
            {

                if (dateInput > DateTime.DaysInMonth(yearInput, monthInput))
                {
                    Console.WriteLine("The date doesn't reach that many!");
                    correctInput = false; // Makes one more iteration of the loop
                }

            }
        } while (!correctInput || dateInput > 31 || dateInput < 1);