C#流程控制詳解
流程控制語句分類
- 分支語句: if語句和switch語句
- 迭代語句
- 跳轉(zhuǎn)語句
1、if語句
if (判斷條件表達(dá)式){ 表達(dá)式結(jié)果為true時(shí)執(zhí)行}else{表達(dá)式結(jié)果為false時(shí)執(zhí)行}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace if語句 { class Program { static void Main(string[] args) { //判斷a變量與10的關(guān)系 Console.WriteLine("請(qǐng)輸入你要比較的第一個(gè)數(shù)字"); int a=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("請(qǐng)輸入你要比較的第而個(gè)數(shù)字"); //int.parse 用于將屏幕輸入的語句轉(zhuǎn)換為整型 int b = int.Parse(Console.ReadLine()); if (a < b) { Console.WriteLine("您輸入的第一個(gè)數(shù)字{0}小于第二個(gè)數(shù)字{1}", a,b); } else if (a == b) { Console.WriteLine("您輸入的第一個(gè)數(shù)字{0}等于第二個(gè)數(shù)字{1}", a,b); } else { Console.WriteLine("您輸入的第一個(gè)數(shù)字{0}大于第二個(gè)數(shù)字{1}", a,b); } Console.ReadKey(); } } }
2、switch
輸入1顯示為星期一,依次類推
swithc(條件表達(dá)式){
case 常量表達(dá)式:條件語句;
case 常量表達(dá)式:條件語句;
case 常量表達(dá)式:條件語句;
default:條件表達(dá)式
}
控件無法從最終用例標(biāo)簽(XX)脫離開關(guān)——程序無法判定為結(jié)束,所以必須加一個(gè)break;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace switch控制語句 { class Program { static void Main(string[] args) { // 輸入一顯示星期一,一次類推 Console.WriteLine("請(qǐng)輸入1-7的數(shù)字"); int week = int.Parse(Console.ReadLine()); switch (week) { case 1: Console.WriteLine("星期一"); break; //結(jié)束當(dāng)前代碼體 case 2: Console.WriteLine("星期二"); break; case 3: Console.WriteLine("星期三"); break; case 4: Console.WriteLine("星期四"); break; case 5: Console.WriteLine("星期五"); break; case 6: Console.WriteLine("星期六"); break; case 7: Console.WriteLine("星期日"); break; default: Console.WriteLine("您輸入的數(shù)據(jù)錯(cuò)誤"); break; //超出規(guī)定值設(shè)置相應(yīng)提示 } Console.ReadKey(); //判斷2020年每個(gè)月的天數(shù), 1,3,5,7,8,10,12為31天,4,6,9,11位30天,二月29天 Console.WriteLine("請(qǐng)輸月份數(shù)"); int month = int.Parse(Console.ReadLine()); switch (month) { case 2: Console.WriteLine("您輸入的{0}月份有28天",month); break; case 4: case 6: case 9: case 11: Console.WriteLine("您輸入的{0}月份有30天",month); break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: Console.WriteLine("您輸入的{0}月份有31天", month); break; default: Console.WriteLine("您輸入的{0}月份錯(cuò)誤", month); break; } Console.ReadKey(); } } }
3、三位運(yùn)算符
條件判斷表達(dá)式?成立是執(zhí)行的語句:不成立時(shí)執(zhí)行的語句
三元運(yùn)算符適用條件:只使用與判斷具有兩個(gè)結(jié)果的情況
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 三位運(yùn)算符 { class Program { static void Main(string[] args) { // 判斷輸入述職與10的關(guān)系(<10 提示小于10, >=10提示大于等于10) Console.WriteLine("請(qǐng)輸入您要比較的數(shù)據(jù)"); int number = int.Parse(Console.ReadLine()); //Console.WriteLine(number < 10 ? Console.WriteLine("小于10") : Console.WriteLine("大于等于10") ); Console.WriteLine(number < 10 ? "小于10" : "大于等于10"); Console.ReadKey(); } } }
4、迭代語句之while語句
4.1 迭代語句概述
迭代語句時(shí)程序中重復(fù)的執(zhí)行,直到滿足指定以條件才停止的一段代碼。當(dāng)用戶想重復(fù)執(zhí)行某些語句時(shí),可依據(jù)當(dāng)前不同的任務(wù),
選擇不同的循環(huán)依據(jù)使用,分別是:
- while語句
- do……while語句
- for語句
- foreach語句
4.2 while語句
while(條件表達(dá)式){
代碼語句
}
while語句當(dāng)條件滿足時(shí)才執(zhí)行
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace while語句 { class Program { static void Main(string[] args) { //輸出1-50的數(shù)字到屏幕上 int a = 1; while (a<=50){ Console.WriteLine(a); a++; } Console.ReadKey(); } } }
5、迭代語句之do……while
do{
循環(huán)體語句
}while();
do……while語句至少執(zhí)行一次,即使條件不成立也會(huì)執(zhí)行一次
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace do__while { class Program { static void Main(string[] args) { //輸出1-50的數(shù)字到屏幕上 int num = 0; do { num++; Console.WriteLine(num); } while (num < 50); // 計(jì)算現(xiàn)金存入銀行多長(zhǎng)時(shí)間才可以答案到我們的預(yù)期收益(均按一年期定期存款,到期后自動(dòng)轉(zhuǎn)存) // 分析題目需要的變量 :本金, 目標(biāo)收益,利率 時(shí)間(年) // 一年的收益: 本金*(1+利率)*1年 double Balace = 0; double Rate = 0; int Year = 0; double TargetBalace = 0; Console.WriteLine("請(qǐng)輸入您的本金"); Balace = double.Parse(Console.ReadLine()); Console.WriteLine("請(qǐng)輸入您的當(dāng)前利率百分比"); Rate = double.Parse(Console.ReadLine())/100; Console.WriteLine("請(qǐng)輸入您的目標(biāo)收益"); do { TargetBalace = double.Parse(Console.ReadLine()); if (TargetBalace<Balace) { Console.WriteLine("恭喜您現(xiàn)在已經(jīng)擁有了{(lán)0}元,請(qǐng)輸入一個(gè)更大的目標(biāo)",TargetBalace); } } while (TargetBalace<Balace); do { Balace *= (Rate + 1); Year++; } while (Balace < TargetBalace); Console.WriteLine("您將在{0}年內(nèi),獲得{1}元的收益",Year,Balace); Console.ReadKey(); } } }
6、迭代語句之for循環(huán)語句
for循環(huán)可以循環(huán)次數(shù)的限定,并維護(hù)自己的計(jì)時(shí)器;
有時(shí)候我們會(huì)省略初始條件,判斷條件,循環(huán)條件,但兩個(gè)分號(hào)不能省略
for(初始條件;判斷條件;循環(huán)條件){
循環(huán)語句
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace for循環(huán)語句 { class Program { static void Main(string[] args) { //求輸入數(shù)據(jù)的階乘 // 1!=1 2!=2x1; 3!=3x2x1 Console.WriteLine("請(qǐng)輸入你要計(jì)算的階乘數(shù)"); for (;;) { int num = int.Parse(Console.ReadLine()); int result = 1; for (int i=num; i!=0; i--) { result *= i; }; Console.WriteLine("{0}的階乘結(jié)果是{1}", num, result); }; //Console.ReadKey(); } } }
for循環(huán)嵌套(九九乘法表)
循環(huán)嵌套就是一個(gè)循環(huán)中嵌套著另一個(gè)循環(huán)
使用for循環(huán)時(shí),一般在for循環(huán)語句進(jìn)行聲明循環(huán)計(jì)數(shù)次的變量
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace for循環(huán)語句 { class Program { static void Main(string[] args) { //九九乘法表 Console.WriteLine("==================九九乘法口訣========================="); for (int i = 1; i < 10; i++) { for (int j=1; j<= i; j++) { Console.Write("{0}X{1}={2}\t", j, i, j * i); } Console.WriteLine(); } Console.ReadKey(); } } }
7、迭代語句之foreach
foreach提供了一個(gè)for語句的捷徑,而且還存進(jìn)了集合類更為一致
foreach(類型;變量;in 集合){
代碼體
}
string類型(字符串)可以看成是char類型(字符)的一個(gè)集合
char.IsWhiteSpace© 判斷字符是不是空格
foreach每執(zhí)行一內(nèi)含代碼,循環(huán)變量就會(huì)一次讀取集合中的一個(gè)元素,向當(dāng)時(shí)循環(huán)便利
此處循環(huán)變量只是一個(gè)只讀型的局部變量,這個(gè)值如果被修改編譯會(huì)報(bào)錯(cuò)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace @foreach { class Program { static void Main(string[] args) { //將語句識(shí)別為單詞,并逐行輸出 //語句用string類型,字母用char Console.WriteLine("請(qǐng)輸入一句英文語句"); string sentence = Console.ReadLine(); foreach (char word in sentence) { if (char.IsWhiteSpace(word)) { Console.WriteLine(); } else { Console.Write(word); //word='t'; //foreach語句的迭代變量不允許重新賦值 } } Console.ReadLine(); } } }
8、跳轉(zhuǎn)語句之break語句
跳轉(zhuǎn)語句是程序運(yùn)行到某一位置時(shí),可以跳轉(zhuǎn)到程序中另一行代碼的語句
- break:1)switch語句中用于從case語句中跳出,結(jié)束switch分支語句。2)用于跳出迭代語句結(jié)束當(dāng)前訓(xùn)話
- continute語句
- goto語句
- return語句
通過迭代語句,準(zhǔn)備輸出1~500這500個(gè)數(shù),每行輸出10個(gè)數(shù)。當(dāng)輸出的值同時(shí)是2、3、4、5、6】7的倍數(shù)是,跳出for迭代語句。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace break語句 { class Program { static void Main(string[] args) { //通過迭代語句,準(zhǔn)備輸出1~500這500個(gè)數(shù),每行輸出10個(gè)數(shù)。當(dāng)輸出的值同時(shí)是2、3、4、5、6、7的倍數(shù)是,跳出for迭代語句。 Console.WriteLine("輸出1~500這500個(gè)數(shù),每行輸出10個(gè)數(shù)"); for (int i=1;i<501;i++) { if (i % 2 == 0 && i % 3 == 0 && i % 4 == 0 && i % 5 == 0 && i % 6 == 0 && i % 7 == 0) { Console.WriteLine(); Console.WriteLine("2、3、4、5、6、7的最小公倍數(shù)倍數(shù)是"+i); break; } if (i % 10 == 0) { Console.WriteLine(i); } else Console.Write(i + "\t"); } Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace break語句 { class Program { static void Main(string[] args) { //通過迭代語句,準(zhǔn)備輸出1~500這500個(gè)數(shù),每行輸出10個(gè)數(shù)。當(dāng)輸出的值同時(shí)是2、3、4、5、6、7的倍數(shù)是,跳出for迭代語句。 Console.WriteLine("輸出1~500這500個(gè)數(shù),每行輸出10個(gè)數(shù)"); for (int i=1;i<501;i++) { if (i % 2 == 0 && i % 3 == 0 && i % 4 == 0 && i % 5 == 0 && i % 6 == 0 && i % 7 == 0) break; //{ // Console.WriteLine(); // Console.WriteLine("2、3、4、5、6、7的最小公倍數(shù)倍數(shù)是"+i); // break; //} if (i % 10 == 0) { Console.WriteLine(i); } else Console.Write(i + "\t"); } Console.ReadKey(); } } }
9、continue語句
用于停止當(dāng)前的迭代語句,結(jié)束本次循環(huán),進(jìn)入下一次循環(huán)(本次循環(huán)中continue后面的語句不執(zhí)行)。breack是直接結(jié)束循環(huán)
只能用于迭代語句中
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace continute語句 { class Program { static void Main(string[] args) { //實(shí)現(xiàn)50以內(nèi)的奇數(shù)輸出,利用continue Console.WriteLine("請(qǐng)輸入一個(gè)數(shù),會(huì)自動(dòng)顯示小于次數(shù)的所有奇數(shù)"); int num = int.Parse(Console.ReadLine()); for (int i = 1; i < num+1; i++) { if (i % 2 == 0) continue; //滿足條件時(shí)跳出此次循環(huán),進(jìn)入下一個(gè)循環(huán);且本次循環(huán)continute后的語句不執(zhí)行 Console.WriteLine(i); } Console.ReadLine(); } } }
10、跳轉(zhuǎn)語句之return
return語句使用時(shí),一般有兩種格式:1)return; 2)return 表達(dá)式;
return語句只能出現(xiàn)在方法當(dāng)中,當(dāng)調(diào)傭方法時(shí),執(zhí)行到return語句時(shí);直接跳轉(zhuǎn)到main()函數(shù)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace continute語句 { class Program { static void Main(string[] args) { //實(shí)現(xiàn)50以內(nèi)的奇數(shù)輸出,利用continue Console.WriteLine("請(qǐng)輸入一個(gè)數(shù),會(huì)自動(dòng)顯示小于次數(shù)的所有奇數(shù)"); int num = int.Parse(Console.ReadLine()); for (int i = 1; i < num+1; i++) { if (i % 2 == 0) continue; //滿足條件時(shí)跳出此次循環(huán),進(jìn)入下一個(gè)循環(huán);且本次循環(huán)continute后的語句不執(zhí)行 Console.WriteLine(i); } Console.ReadLine(); } } }
使用方法實(shí)現(xiàn):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace @return { class Program { static void Main(string[] args) { Console.WriteLine("請(qǐng)輸入三個(gè)整數(shù),按回車鍵確認(rèn)每個(gè)數(shù)的輸入"); int a = int.Parse(Console.ReadLine()); int b = int.Parse(Console.ReadLine()); int c = int.Parse(Console.ReadLine()); //double averageresult = (a + b + c) / 3; double averageresult = average(a,b,c); Console.WriteLine("您輸入的三個(gè)數(shù){0}、{1}、{2}的平均數(shù)是{3}",a,b,c, averageresult); Console.ReadKey(); } static double average(int a, int b, int c) { return (a + b + c) / 3; } } }
11、跳轉(zhuǎn)語句之goto
格式:goto 標(biāo)標(biāo)識(shí)符;
標(biāo)識(shí)符標(biāo)識(shí)程序位置的方法
標(biāo)識(shí)方法——標(biāo)識(shí)符+“:”
作用:當(dāng)程序執(zhí)行到goto語句時(shí),程序會(huì)直接跳轉(zhuǎn)到標(biāo)識(shí)符所表示的程序位置。繼續(xù)執(zhí)行
goto的使用會(huì)使代碼的易讀性下降,在編寫程序的時(shí)候盡量少用goto語句
任務(wù):利用goto語句實(shí)現(xiàn)選擇題:
5!=?
1、5!=5
2、5!=10
3、5!=20
4、5!=60
如果選擇真確,提示:恭喜你,答對(duì)了!
如果選擇錯(cuò)誤,提示:很遺憾,你答錯(cuò)了
如果選擇的選項(xiàng)不是1、2、3、4,提示:你所選的選項(xiàng)不存在
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace goto語句 { class Program { static void Main(string[] args) { int a = 0; Console.WriteLine("請(qǐng)選擇5的階乘正確答案,輸入選項(xiàng)編號(hào)回車鍵確認(rèn)"); Console.WriteLine("1. 5!=5\n2. 5!=10\n3. 5!=20\n4. 5!=60\n"); error: { a++; //第一次執(zhí)行時(shí) a=1;因此不執(zhí)行,當(dāng)goto跳轉(zhuǎn)到此語句時(shí),再次自加1,a=2此時(shí)執(zhí)行下面語句 if (a > 1) Console.WriteLine("很遺憾,您打錯(cuò)了,請(qǐng)重新輸入答案"); // 加入a判斷條件原因是,避免在第一次執(zhí)行是輸出此提示 } input: int result = int.Parse(Console.ReadLine()); switch (result) { case 1: case 2: case 3: goto error; case 4: goto right; default: Console.WriteLine("您的選項(xiàng){0}不存在,請(qǐng)重新輸入",result); goto input; } right: { Console.WriteLine("恭喜你答對(duì)了!"); } Console.ReadKey(); } } }
12、任務(wù)實(shí)施
接受a\b\c三個(gè)整數(shù),然后輸出三個(gè)數(shù)中居中的那個(gè)數(shù),并輸出其階乘
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 任務(wù)實(shí)施 { class Program { static void Main(string[] args) { Console.WriteLine("請(qǐng)輸入三個(gè)整數(shù)"); int a = Convert.ToInt32(Console.ReadLine()); int b = Convert.ToInt32(Console.ReadLine()); int c = Convert.ToInt32(Console.ReadLine()); //判斷中間變量 ///如果a是中間值,那么有兩種情況,b是最大值或b是最小值 int temp = 0; int jc = 1; if ((a>=b && a<=c) || (a>=c && a<=b)) { Console.WriteLine(a + "是中間值"); temp = a; Console.WriteLine("錯(cuò)誤"); } if (b >= a && b <= c || b >= c && b <= a) { Console.WriteLine(b + "是中間值"); temp = b; } if (c >= a && c <= b || c >= b && c <= a) { Console.WriteLine(c + "是中間值"); temp = c; } for (int i = 1; i < b+1; i++) { jc *= i; } Console.WriteLine("中間數(shù){0}階乘結(jié)果是{1}",temp,jc); Console.ReadKey(); } } }
到此這篇關(guān)于C#流程控制詳解的文章就介紹到這了,更多相關(guān)C#流程控制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#(.net)中按字節(jié)數(shù)截取字符串最后出現(xiàn)亂碼問題的解決
這篇文章主要給大家介紹了關(guān)于C#(.net)中按字節(jié)數(shù)截取字符串最后出現(xiàn)亂碼問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06C#?DateTime.Now.ToString()?用法示例講解
這篇文章主要介紹了C#?DateTime.Now.ToString()?用法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01通過C#調(diào)用cmd來修改服務(wù)啟動(dòng)類型
可以使用System.ServiceProcess.ServiceController這個(gè)類允許連接到正在運(yùn)行或者已停止的服務(wù)、對(duì)其進(jìn)行操作或獲取有關(guān)它的信息但是這個(gè)類并沒有提供修改服務(wù)啟動(dòng)類型的方法,可以通過C#調(diào)用cmd來修改2012-12-12C#自定義導(dǎo)出數(shù)據(jù)到Excel的類實(shí)例
這篇文章主要介紹了C#自定義導(dǎo)出數(shù)據(jù)到Excel的類,實(shí)例分析了C#操作Excel的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03WPF設(shè)置窗體可以使用鼠標(biāo)拖動(dòng)大小的方法
這篇文章主要介紹了WPF設(shè)置窗體可以使用鼠標(biāo)拖動(dòng)大小的方法,涉及針對(duì)窗口的操作與設(shè)置技巧,具有很好的借鑒價(jià)值,需要的朋友可以參考下2014-11-11