switch(表达式)//符合表达式中的条件,执行以下很多个相同变量但是值不一样,导致不同的结果的语句,break可以跳出
{
case 常量表达式1:语句1;
case 常量表达式2:语句2;
case 常量表达式3:语句3;
……
case 常量表达式n:语句n;
default:语句n+1;
}
判断闰年练习:
using System;
namespace switch_case练习
{
class Program
{
static void Main(string[] args)
{
//判断闰年练习:先输入年份;再输入月份;再输出这个月的天数;
Console.WriteLine(“请输入年份”);
try
{
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“请输入月份”);
try
{
int month = Convert.ToInt32(Console.ReadLine());
if (month >= 1 && month <= 12)
{
int day = 0;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
day = 28;
}
else
{
day = 29;
}
break;
defult: day = 30;
break;
}
Console.WriteLine("{0}年{1}月的天数是{2}", year, month, day);
}//if判断的括号
else
{
Console.WriteLine(“你输入的月份有误”);
}
}//try月份的括号
catch
{
Console.WriteLine(“你输入的月份有误,程序退出”);
}
}//try年份的括号
catch
{
Console.WriteLine(“你输入的年份有误,程序退出”);
}
Console.ReadKey ();
}
}
}
using System;
namespace switch_case语句练习
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“请输入你的姓名:”);
string name = Console.ReadLine();
switch (name)
{
case “老顾”: Console.WriteLine(“警察”);
break;
case "老张":Console.WriteLine("奥里给");
break;
default:
Console.WriteLine("不认识,应该是一坨翔");
break;
}
Console.ReadKey();
}
}
}




