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