最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#流程控制詳解

 更新時(shí)間:2022年07月18日 12:01:30   作者:Mwyldnje2003  
這篇文章主要介紹了C#流程控制詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

流程控制語句分類

  • 分支語句: 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("請輸入你要比較的第一個(gè)數(shù)字");
            int a=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("請輸入你要比較的第而個(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("請輸入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("請輸月份數(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("請輸入您要比較的數(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í)行一次,即使條件不成立也會執(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)金存入銀行多長時(shí)間才可以答案到我們的預(yù)期收益(均按一年期定期存款,到期后自動轉(zhuǎn)存)
            // 分析題目需要的變量  :本金, 目標(biāo)收益,利率  時(shí)間(年)
            // 一年的收益: 本金*(1+利率)*1年 
            double Balace = 0;
            double Rate = 0;
            int Year = 0;
            double TargetBalace = 0;
            Console.WriteLine("請輸入您的本金");
            Balace = double.Parse(Console.ReadLine());
            Console.WriteLine("請輸入您的當(dāng)前利率百分比");
            Rate = double.Parse(Console.ReadLine())/100;
            Console.WriteLine("請輸入您的目標(biāo)收益");
            do {
                TargetBalace = double.Parse(Console.ReadLine());
                if (TargetBalace<Balace) {
                    Console.WriteLine("恭喜您現(xiàn)在已經(jīng)擁有了{(lán)0}元,請輸入一個(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án)條件,但兩個(gè)分號不能省略

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("請輸入你要計(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)變量就會一次讀取集合中的一個(gè)元素,向當(dāng)時(shí)循環(huán)便利
此處循環(huán)變量只是一個(gè)只讀型的局部變量,這個(gè)值如果被修改編譯會報(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)
        {
            //將語句識別為單詞,并逐行輸出
            //語句用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)語句是程序運(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("請輸入一個(gè)數(shù),會自動顯示小于次數(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("請輸入一個(gè)數(shù),會自動顯示小于次數(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("請輸入三個(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)識符;
標(biāo)識符標(biāo)識程序位置的方法
標(biāo)識方法——標(biāo)識符+“:”

作用:當(dāng)程序執(zhí)行到goto語句時(shí),程序會直接跳轉(zhuǎn)到標(biāo)識符所表示的程序位置。繼續(xù)執(zhí)行
goto的使用會使代碼的易讀性下降,在編寫程序的時(shí)候盡量少用goto語句

任務(wù):利用goto語句實(shí)現(xiàn)選擇題:

5!=?
1、5!=5
2、5!=10
3、5!=20
4、5!=60

如果選擇真確,提示:恭喜你,答對了!
如果選擇錯(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("請選擇5的階乘正確答案,輸入選項(xiàng)編號回車鍵確認(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ò)了,請重新輸入答案");  // 加入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}不存在,請重新輸入",result);
                    goto input;
            }
        right:
            {
                Console.WriteLine("恭喜你答對了!");
            }
            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("請輸入三個(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

  • C#利用ReportViewer生成報(bào)表

    C#利用ReportViewer生成報(bào)表

    這篇文章主要為大家詳細(xì)介紹了C#利用ReportViewer生成報(bào)表的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • C#字符串的常用操作工具類代碼分享

    C#字符串的常用操作工具類代碼分享

    這篇文章主要介紹了C#字符串的常用操作工具類代碼分享,需要的朋友可以參考下
    2014-04-04
  • C#中間語言及ILDASM工具用法

    C#中間語言及ILDASM工具用法

    這篇文章介紹了C#中間語言及ILDASM工具用法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • c#動態(tài)執(zhí)行腳本的3種方式詳解

    c#動態(tài)執(zhí)行腳本的3種方式詳解

    本文主要介紹了c#動態(tài)執(zhí)行腳本的3種方式詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解

    C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解

    這篇文章主要介紹了C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • C#實(shí)現(xiàn)把指定數(shù)據(jù)寫入串口

    C#實(shí)現(xiàn)把指定數(shù)據(jù)寫入串口

    這篇文章主要介紹了C#實(shí)現(xiàn)把指定數(shù)據(jù)寫入串口,直接給出示例代碼,需要的朋友可以參考下
    2015-06-06
  • Question:基于C#連續(xù)賦值的面試題介紹

    Question:基于C#連續(xù)賦值的面試題介紹

    本篇文章是關(guān)于C#中連續(xù)賦值的面試題介紹,需要的朋友參考下
    2013-05-05
  • C#實(shí)現(xiàn)加密exe文件

    C#實(shí)現(xiàn)加密exe文件

    這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)加密exe文件的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-01-01
  • 圖文詳解C#中的協(xié)變與逆變

    圖文詳解C#中的協(xié)變與逆變

    “協(xié)變”是指能夠使用與原始指定的派生類型相比,派生程度更大的類型,“逆變”則是指能夠使用派生程度更小的類型,這篇文章主要給大家介紹了關(guān)于C#中協(xié)變與逆變的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • 最新評論

    通化县| 湄潭县| 英吉沙县| 海原县| 钟山县| 乐昌市| 宜良县| 九龙坡区| 平陆县| 阿瓦提县| 墨玉县| 兰西县| 贵德县| 崇文区| 东丽区| 江北区| 望奎县| 余庆县| 鄂托克前旗| 永定县| 清涧县| 岚皋县| 辽宁省| 任丘市| 应城市| 青浦区| 黔江区| 南投县| 师宗县| 哈巴河县| 蚌埠市| 宜川县| 固阳县| 鸡西市| 邢台市| 和龙市| 凭祥市| 弥勒县| 红河县| 万山特区| 繁峙县|