傳智的光輝歲月-C#基礎篇五實值型別和參考型別

來源:互聯網
上載者:User

標籤:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace P01Method{    class Program    {        static void Main(string[] args)        {            //int a1 = 11;            //int b2 = 22;            //Add2Num(a1, b2);//在調用方法時,為 方法括弧中 傳遞的 值 就叫做 實參(實際參數)            //Add2Num(102, 205);//在調用方式時,也可以 直接 用 把值 傳遞個 方法的 形參            int res = Add2NumWith(222, 555);//使用 res 變數 接收 方法的傳回值            Console.WriteLine("傳回值為:" + res);            M01Review();            Console.ReadLine();        }        #region 2.2 傳回值: 計算兩個 數值 的 和,並返回給方法的調用者        /// <summary>        /// 2.2 計算兩個 數值 的 和        /// </summary>        static int Add2NumWith(int a, int b)//括弧中的 “變數”,叫 形參(形式參數):本質就是 方法的局部變數        {            int x = a + b;            return x;//將計算的 結果 返回給 方法 的 調用        }        #endregion        #region 2.1 形參:計算兩個 數值 的 和        /// <summary>        /// 2.1 計算兩個 數值 的 和        /// </summary>        static void Add2Num(int a,int b)//括弧中的 “變數”,叫 形參(形式參數):本質就是 方法的局部變數        {            int x = a + b;            Console.WriteLine(x);        }         #endregion        #region 1.0 方法(第一個方法)        /// <summary>        /// 1.0 方法(第一個方法)        ///     方法的好處: 複用代碼-方便調用 和 統一修改        ///                        減少代碼的冗餘(重複的代碼)        ///                封裝代碼:管理業務思路,將不同業務的代碼 分開!        /// </summary>        static void ShowLogo()        {            Console.WriteLine("*****************");            Console.WriteLine("*     小白      *");            Console.WriteLine("*    I  love u   *");            Console.WriteLine("*****************");        }        #endregion        #region 0.2 實值型別和參考型別        /// <summary>        /// 0.2 實值型別和參考型別        /// </summary>        static void M02StatckAndHeap()        {            int a = 110;            int b = a;            b = 112;            Console.WriteLine("a=" + a);//110            int[] arr = new int[1];            arr[0] = 220;            int[] arr2 = arr;            arr2[0] = 212;            Console.WriteLine("arr[0]=" + arr[0]);//212        }         #endregion        #region 0.1 複習 goto M01Review()        /// <summary>        /// 0.1 複習 goto        /// </summary>        static void M01Review()        {        //goto 語句的標籤:        sss:            Console.WriteLine("我在迴圈之前哦~~~!");            for (int row = 0; row < 5; row++)            {                for (int col = 0; col < 5; col++)                {                    if (row == 0 && col == 1)                    {                        //1.通過滿足外部迴圈條件 的方式 退出 外部迴圈                        //row = 100;                        //2.goto語句  退出 外部迴圈                        goto end;//直接 根據 標籤名 跳到 標籤後的代碼 執行                        break;                    }                    Console.Write("*");                }                Console.Write("\n");            }            //goto 語句的標籤:        end:            Console.WriteLine("");        }         #endregion    }}

  

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace P02StuManage{    class Program    {        //------------0. 定義靜態全域變數(類成員變數)stuCount ,arrID ,arrName ,arrAge ,arrSex---------------        #region 0.0 定義靜態全域變數(類成員變數) stuCount ,arrID ,arrName ,arrAge ,arrSex        /// <summary>        /// 學員數量        /// </summary>        static int stuCount = 0;        /// <summary>        /// 學員ID種子,作為新增學員時,自動產生ID號的 標誌        /// </summary>        static int stuIdSeed = 1;        /// <summary>        /// 0.1 儲存所有學員的 id 數組        /// </summary>        static int[] arrID = new int[2];        /// <summary>        /// 0.2 儲存所有學員的 名字 的數組        /// </summary>        static string[] arrName = new string[2];        /// <summary>        /// 0.3 儲存所有學員的 年齡 的數組        /// </summary>        static int[] arrAge = new int[2];        /// <summary>        /// 0.4 儲存所有學員的 性別 的數組        /// </summary>        static bool[] arrSex = new bool[2];        #endregion        static void Main(string[] args)        {            do            {                Console.Clear();//清空螢幕                // 方法練習:學員管理                //1.詢問使用者要進行的操作,需要寫一個方法,接收使用者的操作                int usrOpeType = GetUserOperation();                //2.根據使用者操作選擇,執行操作業務                OperateByOneLevelType(usrOpeType);                //3.詢問使用者是否要繼續操作                Console.WriteLine("操作已結束,您是否要繼續操作呢?(y/n)");            } while (Console.ReadLine() == "y");            Console.ReadLine();        }        //--------------------------------------1.0 程式開始操作層級的 代碼------------------------------------        #region 1.0 擷取使用者操作 int GetUserOperation()        /// <summary>        /// 1.0 擷取使用者操作        /// </summary>        /// <returns>返回使用者選擇的操作類型(1-4的一個數值)</returns>        static int GetUserOperation()        {            do            {                Console.WriteLine("歡迎使用16期基礎班學員管理系統");                Console.WriteLine("請選擇您要進行的操作(1-4):");                Console.WriteLine("1.新增學員");                Console.WriteLine("2.刪除學員");                Console.WriteLine("3.修改學員");                Console.WriteLine("4.查詢學員");                string strOpeType = Console.ReadLine();//擷取使用者的操作選擇                int opeType = 0;                //1.1檢查 使用者輸入 是否是 1-4 之間的數值!                if (int.TryParse(strOpeType, out opeType) && (opeType >= 1 && opeType <= 4))                {                    //1.2執行操作                    return opeType;//將使用者選擇的操作 返回 給方法的調用,【注意:return後,方法結束運行!(迴圈也就不執行了!)】                }                else//1.3不通過,要求使用者重新輸入                {                    Console.WriteLine("您的輸入必須是1-4之間的數值,請重新選擇~");                }            } while (true);        }         #endregion        #region 2.0 接收使用者選擇的 一級 操作選擇,並根據 選擇,執行不同的業務 void OperateByType(int opeType)        /// <summary>        /// 2.0 接收使用者選擇的 一級 操作選擇,並根據 選擇,執行不同的業務        /// </summary>        /// <param name="opeType">使用者一級操作選擇(1-4)</param>        static void OperateByOneLevelType(int opeType)        {            switch (opeType)            {                case 1://新增學員                    {                        AddStu();                        break;                    }                case 2://刪除學員                    {                        break;                    }                case 3://修改學員                    {                        break;                    }                case 4://查詢學員                    {                        ChooseQueryType();                        break;                    }                default:                    {                        break;                    }            }        }        #endregion        //--------------------------------------3.0進行各項業務操作的方法------------------------------------        //--------------------------------------3.1新增業務------------------------------------        #region 3.1要能添加學員(ID,名字,年齡,性別)void AddStu()        /// <summary>        /// 3.1要能添加學員(ID,名字,年齡,性別)        /// </summary>        static void AddStu()        {            //1.接收使用者的資訊            Console.Write("請輸入學員的名字:");            string strStuName = Console.ReadLine();            Console.Write("請輸入學員的年齡:");            string strStuAge = Console.ReadLine();            Console.Write("請輸入學員的性別:(男/女)");            string strStuSex = Console.ReadLine();            //2.將使用者輸入的資訊 分別存入 不同的數組中!            //2.1問題.數組中是否已經存滿了呢?如果滿了,需要擴容(建立一個新的更大的數組來存放資料)            if (stuCount >= arrID.Length)//如果 學員的個數 已經 滿了,就需要 【擴容】            {                //a.為 4 個數組 做擴容,建立 老數組長度 的 2倍 的新數組                int[] arrIDTemp = new int[arrID.Length * 2];                string[] arrNameTemp = new string[arrID.Length * 2];                int[] arrAgeTemp = new int[arrID.Length * 2];                bool[] arrSexTemp = new bool[arrID.Length * 2];                //b.將老數組裡的資料  複製到 新數組中                arrID.CopyTo(arrIDTemp, 0);                arrName.CopyTo(arrNameTemp, 0);                arrAge.CopyTo(arrAgeTemp, 0);                arrSex.CopyTo(arrSexTemp, 0);                //c.將新數組對象 覆蓋 老數組                arrID = arrIDTemp;                arrName = arrNameTemp;                arrAge = arrAgeTemp;                arrSex = arrSexTemp;            }            //3.將新學員資料 根據 已添加的學員人數,放在 數組 對應的位置            arrID[stuCount] = stuIdSeed++;//學員ID自動產生,注意:先執行 =號,再 ++            arrName[stuCount] = strStuName;            arrAge[stuCount] = int.Parse(strStuAge);            arrSex[stuCount] = strStuSex == "男" ? true : false;            //4.學員總數加1            stuCount++;            Console.WriteLine("新增完畢~~");        }         #endregion        //--------------------------------------3.2刪除業務------------------------------------        #region 3.2根據 ID 刪除 void RemoveStuById()        /// <summary>        /// 3.2要能移除學員(根據【ID】移除學員/根據【名字】移除學員)        /// </summary>        static void RemoveStuById()        {        }         #endregion        #region 3.3 根據 姓名 刪除 void RemoveStuByName()        /// <summary>        /// 3.3 根據 姓名 刪除        /// </summary>        static void RemoveStuByName()        {        }         #endregion        //--------------------------------------3.4修改業務------------------------------------        #region 3.4要能修改學員(除了 ID 不能改,其它都能改) void ModifyStu()        /// <summary>        /// 3.4要能修改學員(除了 ID 不能改,其它都能改)        /// </summary>        static void ModifyStu()        {        }         #endregion        //--------------------------------------3.4 查詢業務 要能根據條件查詢學員(根據 ID/名字/年齡段/性別 單條件查詢------------------------------------        #region 3.4 根據使用者的選擇,使用不同的查詢方式 void ChooseQueryType()        /// <summary>        /// 3.4 根據使用者的選擇,使用不同的查詢方式        /// </summary>        static void ChooseQueryType()        {            do            {                Console.WriteLine("請輸入您要進行的查詢方式(1-4):");                Console.WriteLine("1.根據【學員ID】查詢");                Console.WriteLine("2.根據【學員名稱】查詢");                Console.WriteLine("3.根據【年齡段】查詢");                Console.WriteLine("4.根據【學員性別】查詢");                string strOpeType = Console.ReadLine();                int opeType = 0;                if (int.TryParse(strOpeType, out opeType))                {                    switch (opeType)                    {                        case 1://根據【學員ID】查詢                            {                                QueryStuById();                                break;                            }                        case 2://根據【學員名稱】查詢                            {                                QueryStuByName();                                break;                            }                        case 3://根據【年齡段】查詢                            {                                QueryStuByAge();                                break;                            }                        case 4://根據【學員性別】查詢                            {                                QueryStuBySex();                                break;                            }                    }                    break;                }                else                {                    Console.WriteLine("請輸入1-4之間的數值~!");                }             } while (true);        }         #endregion        #region  3.4.1 根據【學員id】查詢 void QueryStuById()        /// <summary>        /// 3.4.1 根據【學員id】查詢        /// </summary>        static void QueryStuById()        {            int stuId = 0;            while (true)            {                Console.WriteLine("請輸入您要查詢的【學員ID】");                string strStuId = Console.ReadLine();                if (int.TryParse(strStuId, out stuId))                {                    break;                }            }            //1.根據id到 學員id數組 arrID  中 尋找,看是否存在 相同的 ID            //根據 學員數量 迴圈 尋找 id數組裡是否有相同的id            for (int i = 0; i < stuCount; i++)            {                if (arrID[i] == stuId)                {                    ShowStuMsgByIndex(i);                    break;                }            }        }         #endregion        #region 3.4.2 根據【學員名稱】 void QueryStuByName()        /// <summary>        /// 3.4.2 根據【學員名稱】         /// </summary>        static void QueryStuByName()        {            Console.WriteLine("請輸入您要查詢的【學員名稱】");            string strName = Console.ReadLine();            //1.根據id到 學員id數組 arrID  中 尋找,看是否存在 相同的 ID            //根據 學員數量 迴圈 尋找 id數組裡是否有相同的id            for (int i = 0; i < stuCount; i++)            {                if (arrName[i] == strName)                {                    ShowStuMsgByIndex(i);                    break;                }            }        }        #endregion        #region 3.4.3 根據【年齡段】查詢 void QueryStuByAge()        /// <summary>        /// 3.4.3 根據【年齡段】查詢        /// </summary>        static void QueryStuByAge()        {            Console.WriteLine("請輸入 開始年齡:");            int ageBegin = int.Parse(Console.ReadLine());            Console.WriteLine("請輸入 結束年齡:");            int ageEnd = int.Parse(Console.ReadLine());            if (ageBegin > ageEnd)            {                Console.WriteLine("開始年齡必須 小於 或 等於 結束年齡");            }            else            {                for (int i = 0; i < stuCount; i++)                {                    //如果找到 年齡 在要求的 年齡段 之間,則顯示學員                    if (arrAge[i] >= ageBegin && arrAge[i] <= ageEnd)                    {                        ShowStuMsgByIndex(i);                    }                }            }        }         #endregion        #region 3.4.4 根據性別查詢 void QueryStuBySex()        /// <summary>        /// 3.4.4 根據性別查詢        /// </summary>        static void QueryStuBySex()        {            Console.WriteLine("請輸入您要查詢的【學員性別】(男/女)");            bool boolGender = Console.ReadLine() == "男";// ? true : false;            //1.根據 性別 到 學員 性別數組 arrID  中 尋找,看是否存在 相同的 性別 學員            for (int i = 0; i < stuCount; i++)            {                if (arrSex[i] == boolGender)                {                    ShowStuMsgByIndex(i);                }            }        }         #endregion        //------------------------------        #region 9.0 根據學員 下標 ,顯示 在4個數組中 儲存的 對應學員資訊 void ShowStuMsgByIndex(int stuIndex)        /// <summary>        /// 9.0 根據學員 下標 ,顯示 在4個數組中 儲存的 對應學員資訊        /// </summary>        /// <param name="stuIndex"></param>        static void ShowStuMsgByIndex(int stuIndex)        {            int stuID = arrID[stuIndex];            string stuName = arrName[stuIndex];            int stuAge = arrAge[stuIndex];            string strSex = arrSex[stuIndex] ? "男" : "女";            string strMsg = string.Format("學員ID:{0} - 學員名稱:{1} - 學員年齡:{2} - 學員性別:{3}", stuID, stuName, stuAge, strSex);            Console.WriteLine(strMsg);        }         #endregion    }}

  

傳智的光輝歲月-C#基礎篇五實值型別和參考型別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.