标签:
函数:
数据类型-变量类型-运算符号表达式-语句(顺序,分支,循环)-数字
程序里的函数:能完成一个相对独立功能的代码块。
数学里的函数:高度抽象。
函数四要素:函数名,输入(参数),输出(返回值类型),加工(函数体)
函数定义:
[static]返回类型 函数名(输入参数列表)
{
//函数体-加工
}
函数调用:
[数据类型 变量名]=函数(参数);
函数名(参数);---用于无返回类型的函数
数据类型 变量=函数名(参数);---用于有返回类型的函数
1.参数匹配
在调用函数时,调用的参数和函数定义的参数保持一对待:个数,类型,对应。
形参:形式参数。--函数定义的参数。
实参:实际参数。--函数调用的参数。
实参、形参传值的规律--传址,传值。
传值:鉴于整型、浮点、bool、char这几种内建函数传递参数的时候,默认都是传值。
传值是把实参做一个副本(copy)传递给形参。
m=30;
Add(m);
static void Add(int a)
{
a+=20;
}
传址:默认情况下,数组就是传地址。字符串也是传地址。
对于内建的整型、浮点、bool、char这些类型,如果要变成传址的话,需要在前面加ref
m=30;
Add(ref m);
static void Add(ref int a)
{
a+=20;
}
对于传值和传址大家要记住:
1.什么是传值,什么传址?这个要分清楚。
2.默认情况下,哪些类型传值?哪些类型传址?
3.对于默认传值的类型,如何让他们变为传址?ref
以后为了防止因为传值、传址引起的错误,建议采用返回值的形式,明确返回的数据。
eg.1 传值的方法 static void Main(string[] args) { int m = 30; Console.WriteLine("Main函数第一次打印:"+m);//30 Add(m); Console.WriteLine("Main函数第二次打印:" + m);//30 } static void Add(int a) { Console.WriteLine("Add函数第一次打印:"+a);//30 a += 20; Console.WriteLine("Add函数第二次打印:"+a);//50 } eg.2 传址方式,加ref static void Mainb(string[] args) { int m = 30; Console.WriteLine("Main函数第一次打印:" + m);//30 Add1( m); Console.WriteLine("Main函数第二次打印:" + m);//30 Console.WriteLine("Main函数第三次打印:" + m);//30 Add2(ref m); Console.WriteLine("Main函数第四次打印:" + m);//50 } static void Add1(int a) { Console.WriteLine("Add1传值函数第一次打印:" + a);//30 a += 20; Console.WriteLine("Add1传值函数第二次打印:" + a);//50 } static void Add2(ref int a) { Console.WriteLine("Add2传址函数第一次打印:" + a);//30 a += 20; Console.WriteLine("Add2传址函数第二次打印:" + a);//50 } eg.3 传址与传值 数组名 传址,数组元素传值。 static void Main1(string[] args) { int[] m = new int[4] {3,5,7,9 }; //打印原值 foreach (int a in m) { Console.Write(a+"\t"); } //调用函数,传递整个数组 Add(m);//传得是数组名,传的是地址。 Add9(m[2]);//传得是数组的某个元素,是传值。 //打印改变后的值 foreach (int a in m) { Console.Write(a+"\t"); } } static void Add9(int a) { Console.WriteLine("Add9被调用了,在Add9中把传进来的" + a + "变成了" + a * 100); a = a * 100; } static void Add(int[] b) { for (int i = 0; i < b.Length; i++) { b[i] *= 10; } }
2、函数重载?
在函数重载时,应包括函数签名的所有方面,例如,有两个不同的函数,他们分别值参数和引用参数。
static void ShowDouble(ref int val)
{
....
}
static void ShowDouble(int val)
{
....
}
选择使用哪个版本纯粹是根据是否包含ref关键字来确定的,下面的代码将调用引用版本:
ShowDouble(ref val);
下面的代码将调用值版本:
ShowDouble(val);
另外还要根据参数的个数来区分函数。
3、委托。
class 对战713 { struct Player { public string Name; public int Blood; public int Attact; public int Defence; public int DuoBi; public ArrayList JiNeng; public ArrayList ShangHai; } static void Main (string[] args) { #region 定义技能库 string[] JN; int[] SH; jineng(out JN, out SH); #endregion Console.WriteLine("请输入玩家1姓名:"); string name1 = Console.ReadLine(); Console.WriteLine("请输入玩家2姓名:"); string name2 = Console.ReadLine(); #region 造玩家,显示基本信息 //造玩家 Random sj = new Random(); //造玩家 Player P1 = wanjia(JN,SH,name1,sj); Player P2 = wanjia(JN,SH,name2,sj); #endregion //显示信息 jbxx(ref P1, ref P2); Console.WriteLine("按下回车键开始战斗"); ConsoleKeyInfo key = Console.ReadKey(); if (key.Key.ToString().ToLower() == "enter") { while (true) { #region 胜负条件 if (P1.Blood == 0 && P2.Blood == 0) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("{0}与{1}同归于尽了~!",P1.Name,P2.Name); break; } else if (P2.Blood == 0) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("{0}把{1}KO了~!",P1.Name,P2.Name); break; } else if (P1.Blood == 0) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("{0}把{1}KO了~!", P2.Name, P1.Name); break; } #endregion //P1打P2 dajia(sj, ref P1, ref P2); //P2打P1 dajia(sj,ref P2,ref P1); // 显示剩余血量 xueliang(ref P1, ref P2); Console.WriteLine(); Thread.Sleep(2000); } } } private static void jbxx(ref Player P1, ref Player P2) { Console.WriteLine("姓名:{0}\t血量:{1}\t攻击力:{2}\t防御:{3}\t躲避:{4}\t所学技能:{5}、{6}、{7}", P1.Name, P1.Blood, P1.Attact, P1.Defence, P1.DuoBi, P1.JiNeng[0], P1.JiNeng[1], P1.JiNeng[2]); Console.WriteLine("姓名:{0}\t血量:{1}\t攻击力:{2}\t防御:{3}\t躲避:{4}\t所学技能:{5}、{6}、{7}", P2.Name, P2.Blood, P2.Attact, P2.Defence, P2.DuoBi, P2.JiNeng[0], P2.JiNeng[1], P2.JiNeng[2]); } private static void xueliang(ref Player P1, ref Player P2) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("{0}的剩余血量为:{1}\t{2}的剩余血量为:{3}", P1.Name, P1.Blood, P2.Name, P2.Blood); } private static void dajia(Random sj, ref Player P1, ref Player P2) { if (sj.Next(10) >= 8)//使用技能 { Console.ForegroundColor = ConsoleColor.White; int xx = sj.Next(3); Console.WriteLine("{0}使用了{1}技能,对{2}造成了{3}点伤害!", P1.Name, P1.JiNeng[xx], P2.Name, P1.ShangHai[xx]); P2.Blood -= Convert.ToInt32(P1.ShangHai[xx]); if (P2.Blood <= 0) { P2.Blood = 0; } } else //普通攻击 { //P2躲避攻击 if (P2.DuoBi >= sj.Next(11)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("{0}躲避了{1}的攻击~!", P2.Name, P1.Name); } else//p2未躲避 { Console.ForegroundColor = ConsoleColor.Red; int ptsh1 = P1.Attact + sj.Next(50) - P2.Defence - sj.Next(30);//p1造成的伤害 Console.WriteLine("{0}对{1}进行攻击,造成{2}点伤害", P1.Name, P2.Name, ptsh1); P2.Blood -= ptsh1; if (P2.Blood <= 0) { P2.Blood = 0; } } } } private static Player wanjia(string[] JN, int[] SH, string name1, Random sj) { Player P1 = new Player(); int seed1; if (name1.Length == 2) { seed1 = (int)Convert.ToChar(name1.Substring(0, 1)) + (int)Convert.ToChar(name1.Substring(1, 1)); } else { seed1 = (int)Convert.ToChar(name1.Substring(0, 1)) + (int)Convert.ToChar(name1.Substring(1, 1)) + (int)Convert.ToChar(name1.Substring(2, 1)); } Random r1 = new Random(seed1); P1.Name = name1; P1.Blood = 2000 + r1.Next(1200); P1.Attact = 200 + r1.Next(50); P1.Defence = 100 + r1.Next(30); P1.DuoBi = 1 + r1.Next(3); P1.JiNeng = new ArrayList(); P1.ShangHai = new ArrayList(); for (int i = 0; i < 3; i++) { int x = sj.Next(10); if (!P1.JiNeng.Contains(JN[x])) { P1.JiNeng.Add(JN[x]); P1.ShangHai.Add(SH[x]); } else { i--; } } return P1; } private static void jineng(out string[] JN, out int[] SH) { JN = new string[] { "龙腾", "横扫千军", "后发制人", "双龙戏珠", "紧箍咒", "五雷轰顶", "猛击", "鹰击", "魅惑", "阎罗令" }; SH = new int[] { 400, 480, 350, 420, 290, 460, 330, 500, 380, }; } } }
//推箱子(数组+函数) namespace ConsoleApplication2 { class 推箱子 { static void Main(string[] args) { int gs = 1; while (true) { int[,] map = zaoditu(gs); show(map); int a = qudian(map, 4, 1);//终点横坐标 int b = qudian(map, 4, 5);//终点纵坐标 int x = qudian(map, 1, 1);//REN 横坐标 int y = qudian(map, 1, 3);//RENzong坐标 //推箱子 while (map[a, b] != 2) { ConsoleKeyInfo key = Console.ReadKey(); #region 上 if (key.Key.ToString().ToLower() == "uparrow") { if (x > 1)//如果人不再靠墙的位置 { if (map[x - 1, y] == 0)//如果人上面是路 { map[x - 1, y] = 1; map[x, y] = 0; x--; } else if (map[x - 1, y] == 2 && map[x - 2, y] == 0)//人上面是箱子,箱子上面是路 { map[x - 2, y] = 2; map[x - 1, y] = 1; map[x, y] = 0; x--; } else if (map[x - 1, y] == 2 && map[x - 2, y] == 3)//人上面是箱子,箱子上面是终点 { map[x - 2, y] = 2; map[x - 1, y] = 1; map[x, y] = 0; x--; } else { Console.Write("/a"); } } else { Console.Write("\a"); } } #endregion #region 下 if (key.Key.ToString().ToLower() == "downarrow") { if (x < 8)//如果人不在靠墙的位置 { if (map[x + 1, y] == 0)//人下面是路 { map[x + 1, y] = 1; map[x, y] = 0; x++; } else if (map[x + 1, y] == 2 && map[x + 2, y] == 0)//人下面是箱子,箱子下面是路 { map[x + 1, y] = 1; map[x + 2, y] = 2; map[x, y] = 0; x++; } else if (map[x + 1, y] == 2 && map[x + 2, y] == 3)//人下面是箱子,箱子下面是终点 { map[x + 1, y] = 1; map[x + 2, y] = 2; map[x, y] = 0; x++; } else { Console.Write("/a"); } } else { Console.Write("/a"); } } #endregion #region 左 if (key.Key.ToString().ToLower() == "leftarrow") { if (y > 1) { if (map[x, y - 1] == 0)//人左边是路 { map[x, y - 1] = 1; map[x, y] = 0; y--; } else if (map[x, y - 2] == 0 && map[x, y - 1] == 2)//人左边是箱子,箱子左边是路 { map[x, y - 2] = 2; map[x, y - 1] = 1; map[x, y] = 0; y--; } else if (map[x, y - 2] == 3 && map[x, y - 1] == 2)//人左边是箱子,箱子左边是终点 { map[x, y - 2] = 2; map[x, y - 1] = 1; map[x, y] = 0; y--; } else { Console.Write("/a"); } } else { Console.Write("/a"); } } #endregion #region 右 if (key.Key.ToString().ToLower() == "rightarrow") { if (y < 8) { if (map[x, y + 1] == 0)//人右边是路 { map[x, y + 1] = 1; map[x, y] = 0; y++; } else if (map[x, y + 2] == 0 && map[x, y + 1] == 2)//人右边是箱子,箱子右边是路 { map[x, y + 2] = 2; map[x, y + 1] = 1; map[x, y] = 0; y++; } else if (map[x, y + 2] == 3 && map[x, y + 1] == 2)//人右边是箱子,箱子右边是终点 { map[x, y + 2] = 2; map[x, y + 1] = 1; map[x, y] = 0; y++; } else { Console.Write("/a"); } } else { Console.Write("/a"); } } #endregion #region 重新生成地图 Console.Clear(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.ForegroundColor = ConsoleColor.White; if (map[i, j] == 8) { Console.Write("■"); } else if (map[i, j] == 0) { Console.Write(" "); } else if (map[i, j] == 1) { Console.Write("♀"); } else if (map[i, j] == 2) { Console.Write("□"); } else if (map[i, j] == 3) { Console.ForegroundColor = ConsoleColor.Red; Console.Write("▲"); Console.ForegroundColor = ConsoleColor.White; } } Console.WriteLine(); } #endregion } Console.WriteLine("恭喜通过,按回车键继续下一关"); ConsoleKeyInfo key1= Console.ReadKey(); if (key1.Key.ToString().ToLower() == "enter") { gs = gs + 1; } Console.Clear(); } } static int[,] zaoditu(int a) { int[,] map1 = new int[10, 10] { {8,8,8,8,8,8,8,8,8,8}, {8,1,0,8,0,0,8,0,0,8}, {8,0,0,8,0,0,8,0,0,8}, {8,0,2,8,0,0,0,0,0,8}, {8,0,0,0,0,0,8,0,0,8}, {8,0,8,8,8,0,8,0,0,8}, {8,0,0,0,0,0,8,0,0,8}, {8,0,0,0,0,0,8,0,0,8}, {8,0,0,3,0,0,0,0,0,8}, {8,8,8,8,8,8,8,8,8,8} }; int[,] map2 = new int[10, 10] { {8,8,8,8,8,8,8,8,8,8}, {8,1,0,8,0,0,8,0,0,8}, {8,0,0,8,0,0,8,0,0,8}, {8,0,2,8,0,0,0,0,0,8}, {8,0,0,0,0,0,8,0,0,8}, {8,0,8,8,8,0,8,0,0,8}, {8,0,0,0,0,0,8,0,0,8}, {8,0,0,0,0,0,8,3,0,8}, {8,0,0,0,0,0,0,0,0,8}, {8,8,8,8,8,8,8,8,8,8} }; int[,] map3= new int[10, 10] { {8,8,8,8,8,8,8,8,8,8}, {8,1,0,8,0,0,8,0,0,8}, {8,0,0,8,0,0,8,0,0,8}, {8,0,2,8,0,0,0,0,0,8}, {8,0,0,0,0,0,8,0,0,8}, {8,0,8,8,8,0,8,0,0,8}, {8,0,0,0,0,0,8,3,0,8}, {8,0,0,0,0,0,8,8,0,8}, {8,0,0,0,0,0,0,0,0,8}, {8,8,8,8,8,8,8,8,8,8} }; if (a == 1) { return map1; } else if (a == 2) { return map2; } else { return map3; } } static void show(int[,] map) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.ForegroundColor = ConsoleColor.White; if (map[i, j] == 8) { Console.Write("■"); } else if (map[i, j] == 0) { Console.Write(" "); } else if (map[i, j] == 1) { Console.Write("♀"); } else if (map[i, j] == 2) { Console.Write("□"); } else if (map[i, j] == 3) { Console.ForegroundColor = ConsoleColor.Red; Console.Write("▲"); Console.ForegroundColor = ConsoleColor.White; } } Console.WriteLine(); } } static int qudian(int[,] map, int a, int b) { int m=0; if (a == 1)//取人坐标 { if (b == 1)//取人横坐标 { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (map[i, j] == 1) { m = i; } } } } else//人纵坐标 { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (map[i, j] == 1) { m =j; } } } } } else//去终点坐标 { if (b == 1)//取终点横坐标 { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (map[i, j] == 3) { m = i; } } } } else//终点纵坐标 { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (map[i, j] == 3) { m = j; } } } } } return m; } } }
标签:
原文地址:http://www.cnblogs.com/xtxtx/p/4668748.html