方法的调用练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 练习一:<br> class Program { //虽然是公有字段,但方法中失效。 public static int _number = 10; static void Main( string [] args) { int a = 3; int res = Test(a); Console.WriteLine(res); // Console.WriteLine(_number); //输出:10 Console.ReadKey(); } public static int Test( int a) { a = a + 5; return a; // Console.WriteLine(_number); //无输出 } public static void TestTwo() { // Console.WriteLine(_number); //无输出 } } |
练习二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class Program { static void Main( string [] args) { //举例:写一个方法,判断一个年份是否是润年. bool b = IsRun(2001); Console.WriteLine(b); Console.ReadKey(); } /// <summary> /// 判断给定的年份是否是闰年 /// </summary> /// <param name="year">要判断的年份</param> /// <returns>是否是闰年</returns> public static bool IsRun( int year) { bool b = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); return b; } } |
练习三
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Program { public static void Main( string [] args) { //比较两个数字的大小 返回最大的 //int a1 = 10; //int a2 = 20; int max = GetMax(10, 20); //实参 Console.WriteLine(max); Console.ReadKey(); } /// <summary> /// 计算两个整数之间的最大值 并且返回最大值 /// </summary> /// <param name="n1">第一个数</param> /// <param name="n2">第二个数</param> /// <returns>返回的最大值</returns> public static int GetMax( int n1, int n2) //形参 { int max = n1 > n2 ? n1 : n2; //如果 n1 > n2成立,则输出n1,否则输出n2 return max; } } |
练习四
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | class Program { static void Main( string [] args) { //1 读取输入的整数 //多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入) Console.WriteLine( "请输入一个数字" ); string input = Console.ReadLine(); int number = GetNumber(input); Console.WriteLine(number); Console.ReadKey(); } /// <summary> /// 这个方法需要判断用户的输入是否是数字 /// 如果是数字,则返回 /// 如果不是数字,提示用户重新输入 /// </summary> public static int GetNumber( string s) { while ( true ) { try { int number = Convert.ToInt32(s); return number; } catch { Console.WriteLine( "请重新输入" ); s = Console.ReadLine(); } } } } |
练习五
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | class Program { static void Main( string [] args) { //只能让用户输入yes或者no,只要不是,就重新输入 //Console.WriteLine("请输入yes或者no"); //string str = Console.ReadLine(); //string result = IsYerOrNo(str); //Console.WriteLine(result); //Console.ReadKey(); //4计算输入数组的和:int Sum(int[] values) int [] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int sum = GetSum(nums); Console.WriteLine(sum); Console.ReadKey(); } /// <summary> /// 计算一个整数类型数组的总和 /// </summary> /// <param name="nums">要求总和的数组</param> /// <returns>返回这个数组的总和</returns> public static int GetSum( int [] nums) { int sum = 0; for ( int i = 0; i < nums.Length; i++) { sum += nums[i]; } return sum; } /// <summary> /// 限定用户只能输入yes或者no 并且返回 /// </summary> /// <param name="input">用户的输入</param> /// <returns>返回yes或者no</returns> public static string IsYerOrNo( string input) { while ( true ) { if (input == "yes" || input == "no" ) { return input; } else { Console.WriteLine( "只能输入yes或者no,请重新输入" ); input = Console.ReadLine(); } } } } |
树立目标,保持活力,gogogo!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具