C#方法(函數)
阿新 • • 發佈:2017-10-23
tasks 年份 錯誤 數組 ask tor 最大 return 最小
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _04.方法_函數_ 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //求2個整數的最大值 14 //int max = Program.GetMax(1, 2); 15 //Console.WriteLine("最大值為{0}", max); 16 //Console.ReadKey(); 17 18 //判斷閏年 19 //bool b = IsRun(2100); 20 //Console.WriteLine(b); 21 //Console.ReadKey(); 22 23 //輸入數字跳出循環,否則繼續 24 //Console.WriteLine("請輸入一個數字"); 25 //string input = Console.ReadLine(); 26 //int number = GetNum(input); 27 //Console.WriteLine(number); 28 //Console.ReadKey(); 29 30 //輸入yes\no,否則繼續 31 //Console.WriteLine("請輸入yes\no"); 32 //string input = Console.ReadLine(); 33 //string result = IsYesOrNo(input); 34 //Console.WriteLine(result); 35 //Console.ReadKey(); 36 37 //求數組的和 38 //int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 39 //int result = GetSum(nums); 40 //Console.WriteLine(result); 41 //Console.ReadKey(); 42 43 //求數組的最大、最小、總和、平均值 44 //int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 45 //int max = 0; 46 //int min = 0; 47 //int sum = 0; 48 //int avg = 0; 49 //Test(nums,out max,out min,out sum,out avg); 50 //Console.WriteLine(max); 51 //Console.WriteLine(min); 52 //Console.WriteLine(sum); 53 //Console.WriteLine(avg); 54 //Console.ReadKey(); 55 56 //判斷登錄條件 57 //Console.WriteLine("username"); 58 //string username = Console.ReadLine(); 59 //Console.WriteLine("password"); 60 //string password = Console.ReadLine(); 61 //string msg; 62 //bool b = IsLogin(username, password, out msg); 63 //Console.WriteLine(b); 64 //Console.WriteLine(msg); 65 //Console.ReadKey(); 66 67 //交換2個int型的整數 68 //int n1 = 10; 69 //int n2 = 20; 70 //Change(ref n1, ref n2); 71 //Console.WriteLine("{0}, {1}", n1, n2); 72 //Console.ReadKey(); 73 74 //輸入姓名、學號、成績,計算總成績 75 ////int[] s = { 99, 99, 99 }; 76 //Score("張三", 101, 99, 99, 99); 77 //Console.ReadKey(); 78 79 //方法遞歸 80 //TellStory(); 81 //Console.ReadKey(); 82 83 //方法綜合練習6.7.12 84 85 //求一個字符串數組中最長的元素 86 //string[] names = { "馬雲", "羅振宇", "科比布萊恩特", "紮克伯格"}; 87 //Console.WriteLine(GetLongStr(names)); 88 //Console.ReadKey(); 89 } 90 91 #region 2個數的最大值 92 /// <summary> 93 /// 求2個數的最大值 94 /// </summary> 95 /// <param name="n1">第一個整數</param> 96 /// <param name="n2">第二個整數</param> 97 /// <returns>返回最大值</returns> 98 public static int GetMax(int n1, int n2) 99 { 100 return n1 > n2 ? n1 : n2; 101 } 102 #endregion 103 104 #region 判斷閏年 105 /// <summary> 106 /// 判斷給出的年份是否為閏年 107 /// </summary> 108 /// <param name="year">年份</param> 109 /// <returns>bool</returns> 110 public static bool IsRun(int year) 111 { 112 bool b = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); 113 return b; 114 } 115 #endregion 116 117 #region 判斷用戶的輸入是否為數字 118 /// <summary> 119 /// 判斷用戶的輸入是否為數字 120 /// </summary> 121 /// <param name="s"></param> 122 /// <returns></returns> 123 public static int GetNum(string s) 124 { 125 while (true) 126 { 127 try 128 { 129 int number = Convert.ToInt32(s); 130 return number; 131 } 132 catch 133 { 134 Console.WriteLine("輸入有誤"); 135 s = Console.ReadLine(); 136 } 137 } 138 } 139 #endregion 140 141 #region yes\no跳出循環 142 /// <summary> 143 /// 輸入yes\no 144 /// </summary> 145 /// <param name="input"></param> 146 /// <returns></returns> 147 public static string IsYesOrNo(string input) 148 { 149 while (true) 150 { 151 if (input == "yes" || input == "no") 152 { 153 return input; 154 } 155 else 156 { 157 Console.WriteLine("請重新輸入"); 158 input = Console.ReadLine(); 159 } 160 } 161 } 162 #endregion 163 164 #region 求數組的和 165 /// <summary> 166 /// 求數組的和 167 /// </summary> 168 /// <param name="numbers"></param> 169 /// <returns></returns> 170 public static int GetSum(int[] numbers) 171 { 172 int sum = 0; 173 for (int i = 0; i < numbers.Length; i++) 174 { 175 sum += numbers[i]; 176 } 177 return sum; 178 } 179 #endregion 180 181 #region 返回數組的最大、最小、總和、平均值 182 /// <summary> 183 /// 返回數組的最大、最小、總和、平均值 184 /// </summary> 185 /// <param name="nums">數組</param> 186 /// <param name="max">多於反悔的最大值</param> 187 /// <param name="min">多於反悔的最小值</param> 188 /// <param name="sum">多於反悔的總和</param> 189 /// <param name="avg">多於反悔的平均值</param> 190 public static void Test(int[] nums, out int max, out int min, out int sum, out int avg) 191 { 192 max = nums[0]; 193 min = nums[0]; 194 sum = 0; 195 for (int i = 0; i < nums.Length; i++) 196 { 197 if (nums[i] > max) 198 { 199 max = nums[i]; 200 } 201 if (nums[i] < min) 202 { 203 min = nums[i]; 204 } 205 sum += nums[i]; 206 } 207 avg = sum / nums.Length; 208 } 209 #endregion 210 211 #region 判斷登錄條件 212 /// <summary> 213 /// 判斷登錄條件 214 /// </summary> 215 /// <param name="uid">username</param> 216 /// <param name="pwd">password</param> 217 /// <param name="msg">錯誤信息</param> 218 /// <returns></returns> 219 public static bool IsLogin(string uid, string pwd, out string msg) 220 { 221 if (uid == "admin" && pwd == "123") 222 { 223 msg = "登錄成功"; 224 return true; 225 } 226 else if (uid == "admin") 227 { 228 msg = "密碼錯誤"; 229 return false; 230 } 231 else if (pwd == "123") 232 { 233 msg = "用戶名錯誤"; 234 return false; 235 } 236 else 237 { 238 msg = "全部錯誤"; 239 return false; 240 } 241 } 242 #endregion 243 244 #region 交換2個int型的整數 245 /// <summary> 246 /// 交換2個int型的整數 247 /// </summary> 248 /// <param name="n1">n1</param> 249 /// <param name="n2">n2</param> 250 public static void Change(ref int n1, ref int n2) 251 { 252 int temp = n1; 253 n1 = n2; 254 n2 = temp; 255 } 256 #endregion 257 258 #region 輸入姓名、學號、成績,計算總成績 259 /// <summary> 260 /// 輸入姓名、學號、成績,計算總成績 261 /// </summary> 262 /// <param name="name">姓名</param> 263 /// <param name="id">學號</param> 264 /// <param name="score">成績數組</param> 265 public static void Score(string name, int id, params int[] score) 266 { 267 int sum = 0; 268 for (int i = 0; i < score.Length; i++) 269 { 270 sum += score[i]; 271 } 272 Console.WriteLine("{0}的總成績為{1},學號{2}", name, sum, id); 273 } 274 #endregion 275 276 #region 方法遞歸 277 /// <summary> 278 /// 方法遞歸 279 /// </summary> 280 public static int i = 0; 281 public static void TellStory() 282 { 283 Console.WriteLine("從前有座廟"); 284 Console.WriteLine("廟裏有個老和尚和小和尚"); 285 Console.WriteLine("有一天,老和尚對小和尚說:"); 286 i++; 287 if (i > 10) 288 { 289 return; 290 } 291 TellStory(); 292 } 293 #endregion 294 295 #region 計算字符串數組中的最大值 296 /// <summary> 297 /// 計算字符串數組中的最大值 298 /// </summary> 299 /// <param name="s">數組</param> 300 /// <returns>最大值</returns> 301 public static string GetLongStr(string[] s) 302 { 303 string max = s[0]; 304 for (int i = 0; i < s.Length; i++) 305 { 306 if (s[i].Length > max.Length) 307 { 308 max = s[i]; 309 } 310 } 311 return max; 312 } 313 #endregion 314 } 315 } 316
C#方法(函數)