switch

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace program
 7 {
 8     class Program
 9     {
10         //switch有时比if...else要简便,但是【常量表达式】的值必须是整数。
11         //【表达式】的类型必须是sbyte,byte,short,ushort,int,uint,long,ulong,char,string和枚举类型中的一种
12         //Switch语句的格式:
13         //switch(【表达式】)
14         //{     case 【常量表达式】:【语句块】       //【常量表达式】的值必须是【表达式】类型兼容的常量
15         //      break;                                                      //各【常量表达式】不能相同,否则出现编译错误
16         //      ......
17         //      default:【语句块】    //只能有一个default
18         //      break;
19         //}
20         //根据月份,得出所属季节
21         static void Main(string[] args)
22         {
23             Console.Write("请输入月份:");
24             int theMonth = Convert.ToInt32(Console.ReadLine());
25             string theSeason;
26             switch(theMonth)
27             {
28                 case 12://注意,是冒号
29                 case 1:
30                 case 2:theSeason = "你输入的月份是冬季";
31                 break;//跳出switch语句
32 
33                 case 3:
34                 case 4:
35                 case 5: theSeason = "你输入的月份是春季";
36                 break;
37 
38                 case 6:
39                 case 7:
40                 case 8: theSeason = "你输入的月份是夏季";
41                 break;
42 
43                 case 9:
44                 case 10:
45                 case 11: theSeason = "你输入的月份是秋季";
46                 break;
47 
48                 default: theSeason = "输入月份有误";
49                 break;
50 
51             }
52             Console.WriteLine(theSeason);//theSeason是string类型,不用加双引号
53             Console.ReadLine();
54         }
55     }
56 }

 

posted @ 2015-08-28 19:16  宋英杰  阅读(190)  评论(0编辑  收藏  举报