c#3種基本的條件判斷語句有1.if 2.if...else... 3.switch 1.5.1if結構 c#中if結構的語法與java完全相同,即 1. if(表達式) { 代碼塊 } 2. if...else...結構如下 if() { 代碼塊1 } else { 代碼塊2 } 3. 多重if結 ...
c#3種基本的條件判斷語句有1.if 2.if...else... 3.switch
1.5.1if結構
c#中if結構的語法與java完全相同,即
1.
if(表達式)
{
代碼塊
}
2.
if...else...結構如下
if()
{
代碼塊1
}
else
{
代碼塊2
}
3.
多重if結構如下
if(表達式1)
{
代碼塊1
}
else if(表達式2)
{
代碼塊2
}
else if(表達式3)
{
代碼塊3
}
......
else
{
代碼塊
}
4.嵌套if結構就是在if中再嵌入if結構,即
if(表達式1)
{
if(表達式2)
{
代碼塊1
}
else
{
代碼塊2
}
}
else
{
代碼塊3
}
例

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace HellowWorld 8 { 9 /// <summary> 10 /// 該程式實現向控制台輸出一條消息 11 /// </summary> 12 /// <param name="args"></param> 13 class Program 14 { 15 //程式的入口 16 static void Main(string[] args) 17 { 18 int price = 4000;//機票原價 19 int month; 20 int type; 21 string info1 = "please input the month when you go out:1~12"; 22 string info2 = "please choose first-class or economy-class?first-class 1,economy-class 2"; 23 string info3 = "your plane ticket price is {0} $"; 24 Console.WriteLine(info1); 25 month = int.Parse(Console.ReadLine()); 26 Console.WriteLine(info2); 27 type = int.Parse(Console.ReadLine()); 28 if (month >= 5 && month <= 10)//旺季 29 { 30 if (type == 1)//頭等艙 31 { 32 Console.WriteLine(info3, price * 0.9); 33 } 34 else if (type == 2)//經濟艙 35 { 36 Console.WriteLine(info3, price * 0.75); 37 } 38 } 39 else //淡季 40 { 41 if (type == 1)//頭等艙 42 { 43 Console.WriteLine(info3, price * 0.6); 44 } 45 else if (type == 2)//經濟艙 46 { 47 Console.WriteLine(info3, price * 0.3); 48 } 49 } 50 Console.ReadLine(); 51 } 52 } 53 }View Code
運行結果
註意:
1.只有當滿足外層if條件時才會判斷內層if的條件
2.else與離它最近的那個缺少else的if相匹配
規範:
1.為使if結構清晰,應把每個if或else包含的語句都用大括弧括起來
2.相匹配的一對if和else應該左對齊
3.內層的if結構相對於外層的if結構要有一定的縮進
1.5.2switch結構
java | c# | |
switch(int/char 表達式) { case 常量表達式1: 語句1; break;//可以沒有 case 常量表達式2: 語句2; break;//可以沒有 ...... default: 語句n; } |
switch(int/char/string 表達式) { case 常量表達式1: 語句1; break;//必須有 case 常量表達式2: 語句2; break;//必須有 ...... default: 語句n; break;//必須有 } |
switch(int/char/string 表達式) { case 常量表達式1: case 常量表達式2: 語句2; break;//必須有 ...... default: 語句n; break;//必須有 } |
例

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace HellowWorld 8 { 9 /// <summary> 10 /// 該程式實現向控制台輸出一條消息 11 /// </summary> 12 /// <param name="args"></param> 13 class Program 14 { 15 //程式的入口 16 static void Main(string[] args) 17 { 18 string name1 = "Jack"; 19 string name2 = "Tom"; 20 string subject1 = "c#"; 21 string subject2 = "java"; 22 int score1 = 91; 23 int score2 = 89; 24 Console.WriteLine("please choose to output which person's information,Jack or Tom?"); 25 string choice = Console.ReadLine(); 26 Console.WriteLine("{0}\t{1}\t{2}","name","subject","score"); 27 switch (choice) 28 { 29 case "Jack": 30 Console.WriteLine("{0}\t{1}\t{2}",name1,subject1,score1); 31 break; 32 case "Tom": 33 Console.WriteLine("{0}\t{1}\t{2}", name2, subject2, score2); 34 break; 35 default: 36 Console.WriteLine("sorry,there is no the person you search for!"); 37 break; 38 } 39 Console.ReadLine(); 40 } 41 } 42 }View Code
運行結果如圖
經常會有忘記寫break造成出錯,在vs中可通過單擊菜單 工具>選項>文本編輯器>c#,選中右邊的顯示下的行號即可。