如何捕获与枚举参数不匹配的输入

我这里有一种输入星期几的方法,但是如果我输入的数字值不是1到7,则程序会简单地得出结论。我想有一种方法来触发捕获。


        namespace DaysOfTheWeek

{

class Program

{

    public enum EDay

    {

        Monday,

        Tuesday,

        Wednesday,

        Thursday,

        Friday,

        Saturday,

        Sunday,

    }

    static void Main(string[] args)

    {

        try

        {

            Console.WriteLine("Hello!  A week has 7 days!  What day of this week is it?");

            EDay pickDay = (EDay)Enum.Parse(typeof(EDay), Console.ReadLine(), true);

            Console.WriteLine("The day you picked was {0}", pickDay - 1);

            Console.ReadLine();

        }

        catch (Exception)

        {

          Console.WriteLine("Please enter an actual numerical day of the week.");

            Console.ReadLine();

        }

    }


}

}


倚天杖
浏览 133回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP