zoukankan      html  css  js  c++  java
  • C# 語法---6.語句

    判斷(if    if...else    嵌套if    switch    嵌套switch)

    static void Ifstatement(string[] args)
            {
                if (args.Length = 0)
                {
                    if (args.Length = 1)            //嵌套if
                    {
                        Console.WriteLine("1");
                    }
                    else if (args.Length = 2)   
                    {
                        Console.WriteLine("2");
                    }
                    else
                    {
                        Console.WriteLine("other");
                    }
                }
               
            }
    
    static void Casestatement(string[] args)
            {
                int a = 100;
                int b = 200;
                switch (a)
                {
                    case 100:
                        Console.WriteLine("外部");
                        switch (b)                  //嵌套switch
                        {
                            case 200:
                                Console.WriteLine("內部");
                                break;
                        }
                        break;
                }
            }
     
    循環(for/foreach in  while   do...while) 
     
    for:反復運行語句或語句塊,知道指定的表達式計算為false
    foreach in 用於循環訪問集合,但不能在源集合中添加或移除項
    while: 執行一個語句或語句塊,直到表達式為false
    do..while: 與while不同的是至少執行一次do語句的一次循環
    用for foreach in while  實現快速排序實例:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            /// <summary>
            /// 快速排序
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                Console.WriteLine("請輸入待排序數列(以","分割):");   //轉義字符
                string _s = Console.ReadLine();   //獲取用戶輸入的值
                string[] _sArray = _s.Split(",".ToCharArray());  //split() 將字符串分割,tochararry()將字符串轉換為字符數組
                int _nLength = _sArray.Length;   //數組長度
              
                int[] _nArray = new int[_nLength];  //分配數組地址
                for (int i = 0; i < _nLength; i++)
                {
                    _nArray[i] = Convert.ToInt32(_sArray[i]);//Convert.ToInt32() 可以将多种类型(包括 object  引用类型)的值转换为 int  类型
                }
                var list = _nArray.ToList();
                QuickSort(list, 0, _nLength - 1);
                foreach (var i in list)
                {
                    Console.WriteLine(i.ToString());          //foreach 遍歷輸出數值
                };
                while (true)
                {
                    Thread.Sleep(10000);   //程序延遲10秒
                }
            }
    
    //獲得按曲軸值左右分流后曲軸的位置
            private static int Division(List<int> list, int left, int right)    
            {
                while (left < right)
                {
                    int num = list[left];  //首元素設為曲軸
                    if (num > list[left + 1])  //與曲軸右邊的值比較,若大於則交換,若小於,則與right的值置換到曲軸右邊
                    { 
                        list[left] = list[left + 1];
                        list[left + 1] = num;
                        left++;
                    }
                    else
                    {
                        int temp = list[right];
                        list[right] = list[left + 1];
                        list[left + 1] = temp;
                        right--;
                    }
                    Console.WriteLine(string.Join(",",list));
                }
                Console.WriteLine("----------------------
    ");
                return left; //指向此時曲軸的位置
            }
            private static void QuickSort(List<int> list, int left, int right)
            {
                if (left < rihgt)
                {
                    int i = Division(list, left,right);                      //遞歸調用函數
                    QuickSort(list, i + 1, right);     //對軸右邊進行排序
                    QuickSort(list, left, i - 1);      //對軸左邊進行排序
                }
            }
        }
    }
     
    迭代器yield return yield break
     
    yield  return 放回一個實現了IEnumerable接口的對象  ,保證每次循環遍歷的時候從前一次停止的地方開始執行
    實際應用中實現“按需供給”
    class MyClass
        {
            char[] chrs = { 'A', 'B', 'C', 'D' };
            public IEnumerator<int> GetEnumerator()
            {
                foreach (char ch in chrs)
                    yield return ch;                //實現迭代器方法循環輸出字符數組中的每個字符
            }
        }
        class MainClass
        {
            public static void Main()
            {
                MyClass mc = new MyClass();
                foreach (char ch in mc)                  //當使用foreach循環時,迭代器會自動返回每個字符,并記錄當前狀態,當執行下一次代碼時,迭代器會從上次循環的位置繼續下次輸出(yield),返回下一個字符。
                Console.Write(ch + "");
                Console.WriteLine();
           }
        }

    yield break 用於終止循環遍歷

    異常處理( try    catch   finally)
    try 的作用是將可能引發異常的代碼包含在 try 塊中 如果發生了異常,則轉入catch執行,finally可有可無(如果沒有catch塊就是必須要的),不管有無異常,都會在異常處理結構最後執行
     
    主要用於【忽略錯誤】,或出錯時,用messagebox【提示出錯】
    class DivNumbers
        {
            int result;
            DivNumbers()
            {
                result = 0;
            }
            public void division(int num1, int num2)
            {
                try
                {
                    result = num1 / num2;  //除法運算,一旦發生異常,轉入catch中執行,
                }
                catch (DivideByZeroException e)
                {
                    Console.WriteLine("Exception caught:{0}",e);  //嘗試將整數除以0發生異常執行的代碼
                }
                finally
                {
                    Console.WriteLine("Result: {0},result");   //無論發生什麼都會執行
                }
            }
            static void Main(string[] args)
            {
                DivNumbers d = new DivNumbers();
                d.division(23, 0);
                Console.ReadKey();
            }
        }
  • 相关阅读:
    张高兴的 UWP 开发笔记:用 Thumb 控件仿制一个可拖动 Button
    oracle并非所有变量都已绑定
    win7出现无法连接到代理服务器的错误,不能上网的问题的解决
    用asp.net c# HttpWebRequest获取网页源代码
    退出系统时跳出frame框架
    使用MagicAJax的AjaxPanel时有时会弹出"Using the AjaxCallHelper write methods outside of an AjaxCall is not allowed."
    UI学习网站
    Oracle服务无法启动,报:Windows无法启动OracleOraDb10g_home1TNSListener服务,错误 1067:进程意外终止。
    视频怎么下载大电脑上
    css模板
  • 原文地址:https://www.cnblogs.com/ygtup/p/9358934.html
Copyright © 2011-2022 走看看