switch(表达式)
{
case 值 1:
语句块 1;
break;
case 值 2:
语句块 2;
break;
...
default:
语句块 n;
break;
}
class Program { static void Main(string[] args) { Console.WriteLine("请输入学生考试的成绩(0~100的整数)"); int points = int.Parse(Console.ReadLine()); switch (points / 10) { case 10: Console.WriteLine("优秀"); break; case 9: Console.WriteLine("优秀"); break; case 8: Console.WriteLine("良好"); break; case 7: Console.WriteLine("及格"); break; case 6: Console.WriteLine("及格"); break; default: Console.WriteLine("不及格"); break; } } }执行上面代码,效果如下图所示。
class Program { static void Main(string[] args) { Console.WriteLine("请输入学生考试的成绩(0~100的整数)"); int points = int.Parse(Console.ReadLine()); if(points < 0 || points > 100) { points = 0; } switch (points / 10) { case 10: case 9: Console.WriteLine("优秀"); break; case 8: Console.WriteLine("良好"); break; case 7: case 6: Console.WriteLine("及格"); break; default: Console.WriteLine("不及格"); break; } } }上面代码执行效果与简化前是一致的。
本文链接:http://task.lmcjl.com/news/5593.html