【2017-12-14】c#基础-函数
函数/方法:可独立完成某项功能的一个个体
作用:1.提高代码的重用性
2.提高功能开发的效率
3.提高程序代码的可维护性
四要素:输入,输出,函数体,函数名(输入,输出可以没有)
ref - 可出可进
out - 只出不进
解释代码如下:
1 public static int test( int a) 2 { 3 a = 5; 4 int b = a + 10; 5 return b; 6 } 7 static void Main(string[] args) 8 { 9 int a = 10; 10 int b = test(a); 11 Console.WriteLine(a + "," + b);//正常a=10,b=15 12 Console.ReadLine(); 13 }
1 public static int test(ref int a) 2 { 3 a = 5; 4 int b = a + 10; 5 return b; 6 } 7 static void Main(string[] args) 8 { 9 int a = 10; 10 int b = test(ref a); 11 Console.WriteLine(a + "," + b);//使用“ref”后a随函数改变并传回值,a=5,b=15 12 Console.ReadLine(); 13 }
1 public static int test(out int a) 2 { 3 a = 5;//必须为a重新赋值,a的值没有传过来 4 int b = a + 10; 5 return b; 6 } 7 static void Main(string[] args) 8 { 9 int a = 10; 10 int b = test(out a); 11 Console.WriteLine(a + "," + b);//使用“out”后a赋新值改变并传回值。a=5,b=15 12 Console.ReadLine(); 13 }
练习:
1、定义一个函数,需要用户输入一个姓名
输出 “xxx,你好啊!”
1 public static void test1(string name) 2 { 3 Console.WriteLine(name + ",你好啊!"); 4 } 5 static void Main(string[] args) 6 { 7 Console.Write("请输入姓名:"); 8 string name = Console.ReadLine(); 9 test1(name); 10 }
2、定义一个函数,需要用户输入两个姓名
输出“xxx和xxx你们的缘分指数是(1-100),散了吧/缘分不错!”
1 public static void test2(string n1, string n2) 2 { 3 Random r = new Random(); 4 int a = r.Next(1, 101); 5 if (a >= 60) 6 Console.WriteLine(n1 + "和" + n2 + "你们的缘分指数是" + a + ",缘分不错!"); 7 else 8 Console.WriteLine(n1 + "和" + n2 + "你们的缘分指数是" + a + ",散了吧!"); 9 } 10 static void Main(string[] args) 11 { 12 Console.Write("请输入第一个姓名:"); 13 string name1 = Console.ReadLine(); 14 Console.Write("请输入第二个姓名:"); 15 string name2 = Console.ReadLine(); 16 test2(name1,name2); 17 }
3、定义一个函数,计算乘除,需要用户输入两个数和一个运算符
按照输入的运算符,对两个数进行相对的运算,输出结果
1 public static string test3(double a, double b,string f) 2 { 3 if (f == "*") 4 return (a * b).ToString(); 5 else if (f == "/") 6 return (a / b).ToString(); 7 else 8 return "错误"; 9 } 10 static void Main(string[] args) 11 { 12 Console.Write("请输入第一个数:"); 13 double a = Convert.ToDouble(Console.ReadLine()); 14 Console.Write("请输入第二个数:"); 15 double b = Convert.ToDouble(Console.ReadLine()); 16 Console.Write("请输入符号(*或者/):"); 17 string f = Console.ReadLine(); 18 string jg = test3(a,b,f); 19 Console.WriteLine(a+f+b+"="+jg); 20 }
4、猜拳方法
定义一个猜拳方法,返回比试结果,需要输入两个手势
输出结果:“选手1的手势是石头,选手2的手势是包袱,选手2获胜!”
“手势有误!
1 public static int test4(string a,string b) 2 { 3 if (a == b) 4 return 0;//平局 5 else if ((a == "石头" && b == "剪刀") || (a == "剪刀" && b == "布") || (a == "布" && b == "石头")) 6 return 1;//a获胜 7 else if ((a == "剪刀" && b == "石头") || (a == "布" && b == "剪刀") || (a == "石头" && b == "布")) 8 return 2;//b获胜 9 else 10 return -1;//错误 11 } 12 static void Main(string[] args) 13 { 14 Console.Write("选手一请出招:"); 15 string a = Console.ReadLine(); 16 Console.Clear(); 17 Console.Write("选手二请出招:"); 18 string b = Console.ReadLine(); 19 Console.Clear(); 20 int jg = test4(a,b); 21 if (jg == -1) 22 Console.WriteLine("手势错误!"); 23 else 24 { 25 Console.Write("选手一出的是"+a+",选手二出的是"+b+","); 26 if (jg == 0) 27 Console.WriteLine("平局!"); 28 else if(jg==1) 29 Console.WriteLine("选手一获胜!"); 30 else if (jg == 2) 31 Console.WriteLine("选手二获胜!"); 32 } 33 Console.ReadLine(); 34 }
5、菲波那切数列
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765
1 class Program 2 { 3 public static void test5(int a,int b) 4 { 5 int c = a + b; 6 if (c < 10000) 7 { 8 Console.Write("," + c); 9 test5(b, c); 10 } 11 else 12 return; 13 } 14 static void Main(string[] args) 15 { 16 int a = 1; 17 int b = 1; 18 Console.Write(a + "," + b); 19 test5(a,b); 20 Console.ReadLine(); 21 } 22 }