码迷,mamicode.com
首页 > 其他好文 > 详细

选择语句

时间:2016-07-22 23:03:41      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

培训第三天

今天主要学选择语句。

其类型:

1、 if(表达式) 

{结果}

2、if(表达式)

else

{另外结果}

3、if(表达式)
{
}
else if
{
}
else if
{
}
...
else
{
}

这是对应的多选一,如果符合其中一项,后面的就不用运行了。

4、if的嵌套

if(表达式)
{
 if(){}
else{}
}
else
{
 if(){}
}

5、switch case

switch case必须与break一同使用。

 

补练习题:输入年月日,看看格式是否正确!!(复制到vs中看)

 

 Console.Write("请输入年份:");             int y = int.Parse(Console.ReadLine());             if (y >= 0 && y <= 9999)             {                 Console.Write("请输入月份:");                 int m = int.Parse(Console.ReadLine());                 if (m >= 1 && m <= 12)                 {                     Console.Write("请输入日份:");                     int d = int.Parse(Console.ReadLine());                     if (d >= 1 && d <= 31)                     {                         if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)                         {                             Console.WriteLine("日期格式正确,您输入的日期为:{0}-{1}-{2}。", y, m, d);                         }                         else if (m == 4 || m == 6 || m == 9 || m == 11)                         {                             if (d <= 30)                             {                                 Console.WriteLine("日期格式正确,您输入的日期为:{0}-{1}-{2}。", y, m, d);                             }                             else                             {                                 Console.WriteLine("日期格式错误!");                             }                         }                         else                         {                             if (d <= 28)                             {                                 Console.WriteLine("日期格式正确,您输入的日期为:{0}-{1}-{2}。", y, m, d);                             }                             else if (d == 29)                             {                                 if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)                                 {                                     Console.WriteLine("日期格式正确,您输入的日期为:{0}-{1}-{2}。", y, m, d);                                 }                                 else                                 {                                     Console.WriteLine("日期格式错误!");                                 }

                            }                             else                             {                                 Console.WriteLine("日期格式错误!");                             }                         }                     }                     else                     {                         Console.WriteLine("日份不对");                     }                 }                 else                 {                     Console.WriteLine("月份不对");                 }             }             else             {                 Console.WriteLine("年份不对");             }

            Console.ReadLine();

选择语句

标签:

原文地址:http://www.cnblogs.com/wwz-wwz/p/5697251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!