方法练习
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _15方法练习 { class Program { static void Main(string[] args) { // 提示用户输入两个数字 计算这两个数字之间所有整数的和 //1、用户只能输入数字 //2、计算两个数字之间和 //3、要求第一个数字必须比第二个数字小 就重新输入 Console.WriteLine("请输入第一个数字"); string strNumberOne = Console.ReadLine(); int numberOne = GetNumber(strNumberOne); Console.WriteLine("请输入第二个数字"); string strNumberTwo = Console.ReadLine(); int numberTwo = GetNumber(strNumberTwo); //判断第一个数字是否小于第二个数字 JudgeNumber(ref numberOne, ref numberTwo); //求和 int sum = GetSum(numberOne, numberTwo); Console.WriteLine(sum); Console.ReadKey(); } public static void JudgeNumber(ref int n1, ref int n2) { while (true) { if (n1 < n2) { //复合题意 return; } else//>=2 { Console.WriteLine("第一个数字不能大于或者等于第二个数字,请重新输入第一个数字"); string s1 = Console.ReadLine(); //调用GetNumber n1 = GetNumber(s1); Console.WriteLine("请重新输入第二个数字"); string s2 = Console.ReadLine(); n2 = GetNumber(s2); } } } public static int GetNumber(string s) { while (true) { try { int number = Convert.ToInt32(s); return number; } catch { Console.WriteLine("输入有误!!!请重新输入"); s = Console.ReadLine(); } } } public static int GetSum(int n1, int n2) { int sum = 0; for (int i = n1; i <= n2; i++) { sum += i; } return sum;
// return作用
//1、在方法中返回要返回的值。
//2、立即结束本次方法。 } } }
练习二
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _01方法的练习 { class Program { static void Main( string [] args) { //79、用方法来实现:有一个字符串数组: //{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最 //string[] names = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" }; //string max = GetLongest(names); //Console.WriteLine(max); //Console.ReadKey(); // 80、用方法来实现:请计算出一个整型数组的平均值。保留两位小数 //int[] numbers = { 1, 2, 7 }; //double avg = GetAvg(numbers); //保留两位小数 //string s = avg.ToString("0.00"); //avg = Convert.ToDouble(s); //Console.WriteLine(avg); //Console.WriteLine(s); //Console.WriteLine("{0:0.00}", avg); //Console.WriteLine(avg); //double d = 3.148; //Console.WriteLine(d.ToString("0.00")); // Console.WriteLine("{0:0.00}",d); //1、写一个方法,用来判断用户输入的数字是不是质数 //再写一个方法 要求用户只能输入数字 输入有误就一直让用户输入 while ( true ) { Console.WriteLine( "请输入一个数字,我们将判断你输入的数字是否是质数" ); string strNumber = Console.ReadLine(); int number = GetNumber(strNumber); bool b = IsPrime(number); Console.WriteLine(b); Console.ReadKey(); } } public static bool IsPrime( int number) { if (number < 2) { return false ; } else //>=2 { //让这个数字从2开始除 除到自身的前一位 for ( int i = 2; i < number; i++) { if (number % i == 0) { //给非质数准备的 return false ; } //else //{ // return true; //} } //给质数准备的 return true ; } } public static int GetNumber( string strNumber) { while ( true ) { try { int number = Convert.ToInt32(strNumber); return number; } catch { Console.WriteLine( "请重新输入" ); strNumber = Console.ReadLine(); } } } public static double GetAvg( int [] nums) { double sum = 0; for ( int i = 0; i < nums.Length; i++) { sum += nums[i]; } return sum / nums.Length; } /// <summary> ///求一个字符串数组中最长的元素 /// </summary> /// <param name="s"></param> /// <returns></returns> public static string GetLongest( string [] s) { string max = s[0]; for ( int i = 0; i < s.Length; i++) { if (s[i].Length > max.Length) { max = s[i]; } } 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _02方法的练习 { class Programe { static void Main( string [] args) { //95、接受输入后判断其等级并显示出来。 //判断依据如下:等级={优 (90~100分);良 (80~89分) //Console.WriteLine("请输入考试成绩"); //int score = Convert.ToInt32(Console.ReadLine()); //string level = GetLevel(score); //Console.WriteLine(level); //Console.ReadKey(); //97、请将字符串数组{ "中国", "美国", "巴西", "澳大利亚", "加拿大" }中的内容反转 string [] names = { "中国" , "美国" , "巴西" , "澳大利亚" , "加拿大" }; Test(names); for ( int i = 0; i < names.Length; i++) { Console.WriteLine(names[i]); } Console.ReadKey(); //98写一个方法 计算圆的面积和周长 面积是 pI*R*R 周长是 2*Pi*r //double r = 5; //double perimeter; //double area; //GetPerimeterArea(r, out perimeter, out area); //Console.WriteLine(perimeter); //Console.WriteLine(area); // Console.ReadKey(); } public static void GetPerimeterArea( double r, out double perimeter, out double area) { perimeter = 2 * 3.14 * r; area = 3.14 * r * r; } public static void Test( string [] names) { for ( int i = 0; i < names.Length / 2; i++) { string temp = names[i]; names[i] = names[names.Length - 1 - i]; names[names.Length - 1 - i] = temp; } } public static string GetLevel( int score) { string level = "" ; switch (score / 10) { case 10: case 9: level = "优" ; break ; case 8: level = "良" ; break ; case 7: level = "中" ; break ; case 6: level = "差" ; break ; default : level = "不及格" ; break ; } return level; } } } |
树立目标,保持活力,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工具