zoukankan      html  css  js  c++  java
  • .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 }

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

  • 相关阅读:
    Oracle(二)常用操作语句
    Oracle(一)概念理解
    Spring MVC实现文件上传和下载
    Spring MVC 的执行流程
    Spring MVC原理及配置详解
    idea创建maven web项目
    Spring Bean的生命周期
    integer和int的区别
    web项目搜索框智能提示
    html-tab page
  • 原文地址:https://www.cnblogs.com/NBOWeb/p/7122655.html
Copyright © 2011-2022 走看看