.Net基础篇_学习笔记_第四天_switch-case02

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 第五天_流程控制
 8 {
 9     class Program
10     { 
11         static void Main(string[] args)
12         {
13             //请用户输入年份,再输入月份,获得月份的天数
14             Console.WriteLine("请输入年份:");
15             try
16             {
17 
18                 int _year = Convert.ToInt32(Console.ReadLine());
19                 Console.WriteLine("请输入一个月份:");
20                 try
21                 {
22                     int _month = Convert.ToInt32(Console.ReadLine());
23                     if (_month >= 1 && _month <= 12)
24                     {
25                         int _day;
26                         switch (_month)
27                         {
28                             case 1:
29                             case 3:
30                             case 5:
31                             case 7:
32                             case 8:
33                             case 10:
34                             case 12:
35                                 _day = 31;
36                                 break;
37                             case 2:
38                                 if (_year / 400 == 0 || _year / 4 == 0 && _year / 100 != 0)
39                                 {
40                                     _day = 29;
41                                 }
42                                 else
43                                     _day = 28;
44                                 break;
45                             default:
46                                 _day = 30;
47                                 break;
48                         }
49                         Console.WriteLine("{0}年的{1}月有{2}天", _year, _month, _day);
50                     }//if判断的括号
51                     else
52                     {
53                         Console.WriteLine("输入的月份格式有误,程序退出"); 
54                     }
55                 }//try月份的括号
56                 catch//跟月份配对
57                 {
58                     Console.WriteLine("您输入的月份格式有误,程序退出");
59                 }
60             }//try年份的括号
61             catch//跟年份配对
62             {
63                 Console.WriteLine("您输入的年份格式有误,程序退出");
64             }
65             Console.ReadKey();
66         }
67     }
68 }

先梳理能完成的代码,最后再写异常处理。全局考虑异常。

原文地址:https://www.cnblogs.com/NBOWeb/p/7122655.html