在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论