switch和try-catch综合练习

       
    //输入任意年份,月份,判断该月份天数;
     static void Main(string[] args) { int intDay = 0; int intYear = 0; int intMonth = 0; //任意输入年份和月份, 判断该月有几天; try { Console.WriteLine("input your year:"); intYear = Convert.ToInt32(Console.ReadLine()); try { Console.WriteLine("input your month:"); intMonth = Convert.ToInt32(Console.ReadLine()); if (intMonth>=1 && intMonth<=12) { switch (intMonth) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: intDay = 31; break; case 2: if (intYear % 400 == 0 || (intYear % 4 == 0 && intYear % 100 != 0)) { intDay = 28; } else { intDay = 29; } break; default: intDay = 30; break; } Console.WriteLine(intDay); } else { Console.WriteLine("输入月份必须在1-12之间!"); } } catch { Console.WriteLine("输入的月份有误, 程序退出!"); } } catch { Console.WriteLine("输入年份有误, 退出程序!"); } Console.ReadKey(); } }

 

posted @ 2024-08-11 21:57  CSF践行  阅读(2)  评论(0编辑  收藏  举报