多分支语句:
switch (表达式)
{
case 常量1:
语句序列1;
break ;
case 常量2:
语句序列2;
break ;
case 常量3:
语句序列3;
break ;
default:
语句序列;
break;
}
1.首先计算switch后表达式必须是整数类型:
1. sbyte , byte, ushort, short , uint , int , ulong , long ,
2. float,double ,decimal;
3. char,bool ,
4. string, (enum)
2.case常量值必须类型兼容,必须互异,不能重复。
3.case语句后面必须添加break语句(如果case后面没有语句序列X,可以进行穿透操作!)直到遇见break语句跳出。
//Control cannot fall through from one case label ('default:') to another
//注:之前的版本不只支持小数,目前已经支持小数版本
using System;
public class Program
{
public static void Main()
{
string input = Console.ReadLine();
double score = Convert.ToDouble(input);
string s = "";
if(score>=0&&score<=100)
{
switch ((int)score/10)
{
case 1:
case 2:
case 3:
case 4:
case 5:
s ="不及格";
break;
case 6:
case 8:
case 9:
case 10:
Console.WriteLine("及格");
break;
default :
Console.WriteLine("不会执行");
break;
}
}
else
{
s ="不存在-";
}
Console.WriteLine(s);
}
}