Console.WriteLine("输入日期代号:");
            Console.WriteLine("(1)星期一;(2)星期二;(3)星期三;(4)星期四;(5)星期五;(6)星期六;(7)星期日");
            int myData=Convert.ToInt32(Console.ReadLine());
            MyDay myDay=(MyDay)myData;
            switch (myDay)
            {
                case MyDay.Monday:
                    Console.WriteLine("今天是周一");
                    break;
                case MyDay.Tuesday:
                    Console.WriteLine("今天是周二");
                    break;
                case MyDay.Wednessday:
                    Console.WriteLine("今天是周三");
                    break;
                case MyDay.Thursday:
                    Console.WriteLine("今天是周四");
                    break;
                case MyDay.Friday:
                    Console.WriteLine("今天是周五");
                    break;
                case MyDay.Satuarday:
                    Console.WriteLine("今天是周六");
                    break;
                case MyDay.Sunday:
                    Console.WriteLine("今天是周日");
                    break;
                default:
                    Console.WriteLine("你输入的有误");
                    break;
            }
            Console.ReadLine();        }
        enum MyDay
        {
            Monday=1,
            Tuesday=2,
            Wednessday=3,
            Thursday=4,
            Friday=5,
            Satuarday=6,
            Sunday=7
        }
这段代码 我想加个 判断(如果输入不是数字,console.writeline("你输入有误"))请大家指教

解决方案 »

  1.   

      int myData=-1;
    try
    {
    myData = Convert.ToInt32(Console.ReadLine()); 
    }catch{}
    if(myData == -1)
    {Console.WriteLine("你输入的有误"); }
    else
    {            MyDay myDay=(MyDay)myData; 
                switch (myDay) 
                { 
                    case MyDay.Monday: 
                        Console.WriteLine("今天是周一"); 
                        break; 
                    case MyDay.Tuesday: 
                        Console.WriteLine("今天是周二"); 
                        break; 
                    case MyDay.Wednessday: 
                        Console.WriteLine("今天是周三"); 
                        break; 
                    case MyDay.Thursday: 
                        Console.WriteLine("今天是周四"); 
                        break; 
                    case MyDay.Friday: 
                        Console.WriteLine("今天是周五"); 
                        break; 
                    case MyDay.Satuarday: 
                        Console.WriteLine("今天是周六"); 
                        break; 
                    case MyDay.Sunday: 
                        Console.WriteLine("今天是周日"); 
                        break; 
                    default: 
                        Console.WriteLine("你输入的有误"); 
                        break; 
                } 
                Console.ReadLine(); 
    }
      

  2.   

    不要使用try....cath 或 int.Parse建议用int.TryParseint num=0;
    bool isNum = int.TryParse(Console.ReadLine(), out num)
    if (!isNum)
       console.writeline("你输入有误");num就是输入的数据(int型)
    关于 int.Parse 与 int.TryParse,可以查看MSDN说明。
      

  3.   

    支持2楼,在这里解释一下int.TryParse()【转帖】;int i = -1;
    int.TryParse(null, out i);执行完毕后,i等于0,而不是等于 -1,切记。1 (int)是一种类型转换;当我们觟nt类型到long,float,double,decimal类型,可以使用隐式转换,但是当我们从long类型到int类型就需要使用显式转换,否则会产生编译错误。2 int.Parse()是一种类容转换;表示将数字内容的字符串转为int类型。   如果字符串为空,则抛出ArgumentNullException异常;   如果字符串内容不是数字,则抛出FormatException异常;   如果字符串内容所表示数字超出int类型可表示的范围,则抛出OverflowException异常;int.TryParse与 int.Parse 又较为类似,但它不会产生异常,转换成功返回 true,转换失败返回 false。   最后一个参数为输出值,如果转换失败,输出值为 04 Convert.ToInt32()是一种类容转换;但它不限于将字符串转为int类型,还可以是其它类型的参数; 比较:   Convert.ToInt32 参数为 null 时,返回 0; int.Parse 参数为 null 时,抛出异常。    Convert.ToInt32 参数为 "" 时,抛出异常; int.Parse 参数为 "" 时,抛出异常。   Convert.ToInt32 可以转换的类型较多; int.Parse 只能转换数字类型的字符串