#region 呼叫 /* 我們在main函式中呼叫Test()函式,我們管main函式稱為呼叫者, Test函式稱為被呼叫者. 如果被呼叫者想要得到呼叫者的值: 1) 傳遞引數; 2) 使用靜態欄位來模擬全域變數; 如果呼叫者想要得到被呼叫者的值: 1) 回傳值; */ #endregion namespace 方法呼叫 { class Group { //欄位 public static int _number = 10; static void Main(string[] args) { string a = "tt"; int[] b = { 1, 4, 7 }; int c = 3; //給d賦值,防止報錯 int d = 0; bool t=true; while (t) { try { Console.Write("請輸入您想要確認是否潤年的年份:"); d = Convert.ToInt32(Console.ReadLine()); t = false; } catch { Console.WriteLine("您輸入的數字有誤"); } } Test(a); TestTwo(b); int res=TestThree(c); bool res2=RunNian(d); string res3 = RunNian2(d); //得到不變的a值 Console.WriteLine(a); //得到更改后的陣列 for(int i = 0; i < b.Length; i++) { Console.WriteLine(b[i]); } //得到更改后的回傳值 Console.WriteLine(res); //進行閏年的判斷(bool回傳) Console.WriteLine(res2); //進行閏年的判斷(字串回傳) Console.WriteLine(res3); } /// <summary> /// 形參傳不回去,無論是數字、字符或字串 /// </summary> /// <param name="a"></param> public static void Test(string a) { a = "TAT"; } /// <summary> /// 對整數陣列進行更改發現能夠傳回 /// </summary> /// <param name="b"></param> public static void TestTwo(int[] b) { b[0] = 5; } /// <summary> /// 通過回傳值得到方法處理的結果 /// </summary> /// <param name="c"></param> /// <returns></returns> public static int TestThree(int c) { c = c + 5; return c; } /// <summary> /// 閏年判斷1(bool型別回傳) /// 輸入:年份 /// 輸出:是否閏年 /// </summary> /// <param name="year">要判斷的年份</param> /// <returns>是否是閏年</returns> public static bool RunNian(int year) { bool b = ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)); return b; //if (d % 400 == 0) //{ // return true; //} //else if(d % 100 == 0){ // return false; //} //else if (d % 4 == 0) //{ // return true; //} //else //{ // return false; //} } /// <summary> /// 閏年判斷2(字串回傳) /// </summary> /// <param name="year">要判斷的年份</param> /// <returns>是否是閏年</returns> public static string RunNian2(int year) { string result=Convert.ToString(year); if (year % 400 == 0) { result += "是閏年"; return result; } else if (year % 100 == 0) { result += "不是閏年"; return result; } else if(year%4 == 0) { result += "是閏年"; return result; } else { result += "不是閏年"; return result; } } } } #region 思考 /* * 在呼叫方法時,void可直接對陣列進行處理,卻不能對數字,字符, * 字串進行更改,若想對這些進行更改則要通過return回傳進行; */ #endregion
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/490075.html
標籤:C#
