switch语句是一个多分支选择语句,又称开关语句。它可以根据一个整型表达式有条件地选择一个语句执行。
switch-case 语句:
语法结构:
switch(表达式/变量)
{
case 值1:
语句块1;
break;
case 值2:
语句块2;
break;
default:
语句块3;
break;
}
执行过程:
首先计算表达式,然后根据计算结果去匹配case后面的值,如果有匹配,则执行匹配项后面的语句,直到break语句跳出switch-case。如果所有的case值都不匹配,那么有default则执行default后面的语句,知道break结束,如果没有default,则跳出switch-case,什么都不执行。
注:语句块后必须跟break;
Switch后的值直接搜索匹配项和default的值没关系,只和case后的值有关系。
几个小例子:
1.年月日
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _02练习年月日
{
class Program
{
static void Main(string[] args)
{
//年份月份天
Console.WriteLine("请输入年份?");
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入月份?");
int month = Convert.ToInt32(Console.ReadLine());
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
Console.WriteLine("{0}年,{1}月有31天",year,month);
break;
case 2:
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
{
//是闰年
Console.WriteLine("{0}是闰年,2月有29天",year);
}
else
{
Console.WriteLine("{0}是平年,2月有28天", year);
}
break;
default:
Console.WriteLine("{0}年,{1}月有30天", year, month);
break;
}
Console.ReadKey();
}
}
}
2.成绩评定
if语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace if_else
{
class Program
{
static void Main(string[] args)
{
//练习三
Console.WriteLine("请输入您的结业考试成绩");
int score = Convert.ToInt32(Console.ReadLine());
if (score >= 90)
{
Console.WriteLine("A");
}
if (score >= 80 && score < 90)
{
Console.WriteLine("B");
}
if (score >= 70 && score < 80)
{
Console.WriteLine("C");
}
if (score >= 60 && score < 70)
{
Console.WriteLine("D");
}
if (score >= 0 && score < 60)
{
Console.WriteLine("E");
}
Console.ReadKey();
}
}
}
if-else-if语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace if_else_if
{
class Program
{
static void Main(string[] args)
{
int score = 95;
if (score >= 90)
{
Console.WriteLine("A");
}
else if (score >= 80)
{
Console.WriteLine("B");
}
else if (score >= 70)
{
Console.WriteLine("C");
}
else if (score >= 60)
{
Console.WriteLine("D");
}
else
{
Console.WriteLine("E");
}
Console.ReadKey();
}
}
}
switch语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _02switch测评
{
class Program
{
static void Main(string[] args)
{
//输出成绩评测
Console.WriteLine("请输入你的成绩评测?");
int score=Convert.ToInt32(Console.ReadLine());
switch (score / 10)
{
case 9:
Console.WriteLine("A");
break;
case 8:
Console.WriteLine("B");
break;
case 7:
Console.WriteLine("C");
break;
case 6:
Console.WriteLine("D");
break;
default:
Console.WriteLine("E");
break;
}
Console.ReadKey();
}
}
}
3.等级评定
if-else-if语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace switch_case
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入您对李四的评定等级(A-E)?");
string input = Console.ReadLine();
decimal salary = 5000;
bool flag = false;
if (input == "A")
{
//salary=salary+500;
salary += 500;
}
else if (input == "B")
{
salary += 200;
}
else if (input == "C")
{
}
else if (input == "D")
{
salary -= 200;
}
else if (input == "E")
{
salary -= 500;
}
else
{
Console.WriteLine("输入有误,只能输入大写ABCDE");
flag = true;
}
if (flag == false)
Console.WriteLine("李四的工资为" + salary);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace switch_case
{
class Program
{
static void Main(string[] args)
{
//switch-case
Console.WriteLine("请输入您对李四的评定等级(A-E)?");
string input = Console.ReadLine();
decimal salary = 5000;
bool flag = false;
switch (input)
{
case "A":
salary += 500;
break;
case "B":
salary += 200;
break;
case "C":
salary = salary;
break;
case "D":
salary -= 200;
break;
case "E":
salary -= 500;
break;
default:
Console.WriteLine("你的输入有问题");
flag = true;
break;
}
if (flag == false)
Console.WriteLine("李四的工资为" + salary);
Console.ReadKey();
}
}
}
总结: if-else if与switch的比较
相同点:都可以实现多分支结构
不同点:switch:一般 只能用于等值比较
if-else if:可以处理范围