标签:使用 代码 出现 ring default int ade for 遇到
char grade = ‘C‘;
switch (grade)
{
case ‘A‘:
case ‘B‘:
case ‘C‘: Console.WriteLine("CCC"); break;
case ‘D‘: Console.WriteLine("DDD"); break;
default: Console.WriteLine("无效的成绩"); break;
}
无论grad = A
或B
或C
都输出CCC
char grade = ‘A‘;
switch (grade)
{
case ‘A‘: Console.WriteLine("AAA");
case ‘B‘:
case ‘C‘: Console.WriteLine("CCC"); break;
case ‘D‘: Console.WriteLine("DDD"); break;
default: Console.WriteLine("无效的成绩"); break;
}
提示错误:控制不能从一个case标签("case ‘A‘:"
)贯穿到另一个case标签
char grade = ‘D‘;
switch (grade)
{
case ‘A‘: Console.WriteLine("AAA"); break;
case ‘B‘:
case ‘C‘: Console.WriteLine("CCC"); break;
case ‘D‘:
default: Console.WriteLine("其他成绩"); break;
}
当grade=D
或其他值(不是ABC)
时输出其他成绩
default
语句块中也必须添加break
int a = 100;
int b = 201;
switch (a)
{
case 100:
Console.WriteLine("100");
switch (b)
{
case 200: Console.WriteLine("200"); break;
case 201: Console.WriteLine("201"); break;
}
break;
case 101:
Console.WriteLine("101");
break;
default:
Console.WriteLine("其他值");
break;
}
输出100
、101
for(int i = 0; i < 5; i++)
{
switch (i)
{
case 1: Console.Write("111"); break;
case 2: Console.Write("222"); break;
case 3: Console.Write("333"); break;
case 4: Console.Write("444"); break;
default: Console.Write("其他"); break;
}
Console.WriteLine("\t第" + i.ToString() + "次循环");
}
111.png
for(int i = 0; i < 5; i++)
{
switch (i)
{
case 1: Console.Write("111"); break;
case 2: Console.Write("222"); continue; break;
case 3: Console.Write("333"); break;
case 4: Console.Write("444"); break;
default: Console.Write("其他"); break;
}
Console.WriteLine("\t第" + i.ToString() + "次循环");
}
222.png
注意第2次循环
没有输出
不建议在switch中使用continue
标签:使用 代码 出现 ring default int ade for 遇到
原文地址:https://www.cnblogs.com/rainman/p/12895017.html