條件語句2
知識點
switch語句
一、 switch語句
(一) 問題
1. 問題1
李四的年終工作評定,
如果定為A級,則工資漲500元,
如果定為B級,則工資漲200元,
如果定為C級,工資不變,
如果定為D級工資降200元,
如果定為E級工資降500元.
設李四的原工資為5000,請用戶輸入李四的評級,然后顯示李四來年的工資.
2. 使用條件語句解決
Console.WriteLine("請輸入對李四的年終評定");
string level = Console.ReadLine();//A B C D E 亂七八糟
double salary = 5000;
if (level == "A")
{
salary += 500;//salary=salary+500;
}
else if (level == "B")
{
salary += 200;
}
else if (level == "C")
{
// salary += 0;
}
else if (level == "D")
{
salary -= 200;
}
else if (level == "E")
{
salary -= 500;
}
else//輸入錯誤
{
Console.WriteLine("輸入有誤,程序退出?。?!");
}
Console.WriteLine("李四明年的工資是{0}", salary);
Console.ReadKey();
BUG****:
當輸入亂七八糟的數據時候,Console.WriteLine("李四明年的工資是{0}", salary);這句代碼不執行。
bool b = true;
else//輸入錯誤
{
Console.WriteLine("輸入有誤,程序退出!!!");
b = false;
}
if (b)
{
Console.WriteLine("李四明年的工資是{0}", salary);
}
Console.ReadKey();
3. 問題2
對學員的結業考試成績評測
成績 >= 90 :A
90 > 成績 >= 80 :B
80 > 成績 >= 70 :C
70 > 成績 >= 60 :D
成績 < 60 :E
4. 問題2解決
Console.WriteLine("請輸入你的考試成績");
int score = Convert.ToInt32(Console.ReadLine());
if (score >= 90)
{
Console.WriteLine("A");
}
else//<90
{
if (score >= 80)
{
Console.WriteLine("B");
}
else//<80
{
if (score >= 70)
{
Console.WriteLine("C");
}
else//<70
{
if (score >= 60)
{
Console.WriteLine("D");
}
else
{
Console.WriteLine("E");
}
}
}
}
(二) switch-case
對于判斷區間的語句,我們一般用if-else
對于定值的判斷,我們一般用switch語句
1. 語法
switch(要判斷的變量或者表達式)
{
case 值1:要執行的代碼;
break;
case 值2:要執行的代碼;
break;
case 值3:要執行的代碼;
break;
........
default:要執行的代碼;
break;
}
2. 流程圖
[圖片上傳失敗...(image-e77ae5-1541552904459)]
3. 執行過程
程序運行到switch處,
首先計算switch后面所帶的小括號中的變量或者表達式的值,拿著計算出來的這個結果跟每個case的值進行匹配,一旦匹配成功,則執行該case所帶的代碼塊,
如果跟每個casa所帶的值都不匹配,則看當前switch-csae結構中是否有default,如果有default,則執行default所帶的代碼塊,否則的話,什么都不做。
4. switch-case解決
bool b = true;
Console.WriteLine("請輸入對李四的一個年終評定");
string level = Console.ReadLine();
double salary = 5000;
switch (level)
{
case "A": salary += 500;
break;
case "B": salary += 200;
break;
case "C": break;
case "D": salary -= 200;
break;
case "E": salary -= 500;
break;
default: Console.WriteLine("輸入有誤,程序退出?。?!");
b = false;
break;
}
if (b)
{
Console.WriteLine("李四明年的工資是{0}", salary);
}
Console.ReadKey();
(三) 練習
1. 作業1:練習1讓用戶輸入姓名,然后顯示出這個人上輩子是什么職業。(老楊,老蘇,老鄒,老馬,老虎,老牛,老蔣,小楊)
Console.WriteLine("請輸入要計算的人的姓名");
string name = Console.ReadLine();
switch (name)
{
case "老楊":
Console.WriteLine("老楊上輩子是折翼的天屎");
break;
case "老蘇":
Console.WriteLine("老蘇上輩子是老鴇子");
break;
case "老鄒":
Console.WriteLine("老鄒上輩子是老蘇手下的頭牌");
break;
case "老虎":
Console.WriteLine("上輩子被武松掛了");
break;
case "老牛":
Console.WriteLine("上輩子是Cow");
break;
default:
Console.WriteLine("上輩子沒這個人");
break;
}
Console.ReadKey();
2. 練習2
對學員的結業考試成績評測(改成用Switch來做)
成績 >= 90 :A
90 > 成績 >= 80 :B
80 > 成績 >= 70 :C
70 > 成績 >= 60 :D
成績 < 60 :E
Console.WriteLine("請輸入考試成績");
int score = Convert.ToInt32(Console.ReadLine());
switch (score / 10)
{
case 10://當連續的幾個case中所帶的代碼一樣的時候,可以省略前面的,只寫最后一個case中的代碼
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. 作業2:練習3:請用戶輸年份,再輸入月份,輸出該月的天數.
Console.WriteLine("請輸入一個年份");
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入一個月份");
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 = 29;
}
else
{
day = 28;
}
break;
default: day = 30;
break;
}//swich
Console.WriteLine("{0}年{1}月有{2}天", year, month, day);
}//if
else
{
Console.WriteLine("月份必須在1~12月之間,程序退出!??!");
}
二、 總結:if-else if與switch的比較
(一) 相同點:
都可以實現多分支結構
(二) 不同點:
(1) . if-else if:可以處理范圍
(2) . switch:一般 只能用于等值比較
(三) 三者的區別:
if有條件的執行一條語句
if-else有條件的執行一條或另一條語句
switch有條件的執行一組語句中的一條語句