二维数组的应用:
案例1:成绩表:
static void Main(string[] args) { int i, j; int[,] a = new int[3, 4]; for (i = 0; i < 3; i++) { a[i, 0] = i + 1; Console.Write("第"+(i+1)+"位同学的语文成绩为"); a[i, 1] = Convert.ToInt32(Console.ReadLine()); Console.Write("第" + (i + 1) + "位同学的数学成绩为"); a[i, 2] = Convert.ToInt32(Console.ReadLine()); a[i, 3] = a[i, 1] + a[i, 2]; } Console.Clear(); Console.Write("学号\t语文\t数学\t总分"); Console.WriteLine(); for (i = 0; i < 3;i++ ) { for (j = 0; j < 4; j++) { Console.Write(a[i, j] + "\t"); } Console.WriteLine(); } }
效果图: 回车后:
案例2:推箱子
static void Main(string[] args) { int i, j; int[,] a = new int[10, 10] { {1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,2,0,0,8,0,0,0,1}, {1,0,0,0,0,1,1,0,0,1}, {1,0,0,0,0,1,0,0,0,1}, {1,0,0,1,1,1,0,0,0,1}, {1,0,1,1,0,0,0,0,0,1}, {1,0,0,0,0,0,9,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1}, }; int hang = 2, lie = 2, temp = 0; while (true) { //做地图 for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { if (a[i, j] == 1) { Console.Write("■"); } if (a[i, j] == 0) { Console.Write(" "); } if (a[i, j] == 2) { Console.Write("♀"); } if (a[i, j] == 9) { Console.Write("★"); } if (a[i, j] == 8) { Console.Write("□"); } } Console.WriteLine(); } Console.Write("请按键盘上的前后左右键行走:"); Console.WriteLine(); ConsoleKey s = Console.ReadKey().Key; Console.Clear(); // 向右 if (s.ToString() == "RightArrow") { //判断前边是不是空地 if (a[hang, lie+1] == 0) { temp = a[hang, lie]; a[hang, lie] = a[hang, (lie + 1)]; a[hang, (lie + 1)] = temp; lie++; } //判断前边是不是墙 else if (a[hang, lie + 1] == 1) { Console.Write("撞墙了"); } //判断前边是不是箱子 else if (a[hang, lie + 1] == 8) { if (a[hang, lie + 2] != 1 && a[hang, lie - 2] != 9) { temp = a[hang, (lie + 2)]; a[hang, lie + 2] = a[hang, lie + 1]; a[hang, lie + 1] = a[hang, lie]; a[hang, lie] = temp; lie++; } //判断前边是不是★ else if (a[hang, lie + 2] == 9) { a[hang, lie + 2] = a[hang, lie + 1]; a[hang, lie + 1] = a[hang, lie]; a[hang, lie] = 0; Console.Write("你赢了,请进入下一关"); break; } else { Console.Write("撞墙了");} } } //向左 else if (s.ToString() == "LeftArrow") { if (a[hang, lie-1] == 0) { temp = a[hang, lie]; a[hang, lie] = a[hang, (lie - 1)]; a[hang, (lie - 1)] = temp; lie--; } else if (a[hang, lie - 1] == 1) { Console.Write("撞墙了"); } else if (a[hang, lie - 1] == 8 ) { if (a[hang, lie - 2] != 1 && a[hang, lie - 2] != 9) { temp = a[hang, (lie - 2)]; a[hang, lie - 2] = a[hang, lie - 1]; a[hang, lie - 1] = a[hang, lie]; a[hang, lie] = temp; lie--; } else if (a[hang, lie - 2] == 9) { a[hang, lie - 2] = a[hang, lie - 1]; a[hang, lie - 1] = a[hang, lie]; a[hang, lie] = 0; Console.Write("你赢了,请进入下一关"); break; } else { Console.Write("撞墙了"); } } } //向上 else if (s.ToString() == "UpArrow") { if (a[hang-1, lie] == 0) { temp = a[hang, lie]; a[hang, lie] = a[(hang - 1), lie]; a[(hang - 1), lie] = temp; hang--; } else if (a[hang-1, lie] == 1) { Console.Write("撞墙了"); } else if (a[hang-1, lie] == 8) { if (a[hang - 2, lie] != 1 && a[hang - 2, lie] != 9) { temp = a[hang - 2, lie]; a[hang - 2, lie] = a[hang - 1, lie]; a[hang - 1, lie] = a[hang , lie]; a[hang, lie] = temp; hang--; } else if (a[hang - 2, lie] == 9) { a[hang - 2, lie] = a[hang - 1, lie]; a[hang - 1, lie] = a[hang, lie]; a[hang, lie] = 0; Console.Write("你赢了,请进入下一关"); break; } else { Console.Write("撞墙了"); } } } //向下 else if (s.ToString() == "DownArrow") { if (a[hang+1, lie] == 0) { temp = a[hang, lie]; a[hang, lie] = a[(hang + 1), lie]; a[(hang + 1), lie] = temp; hang++; } else if (a[hang + 1, lie] == 1) { Console.Write("撞墙了"); } else if (a[hang + 1, lie] == 8) { if (a[hang + 2, lie] != 1 && a[hang + 2, lie] != 9) { temp = a[hang + 2, lie]; a[hang + 2, lie] = a[hang + 1, lie]; a[hang + 1, lie] = a[hang , lie]; a[hang, lie] = temp; hang++; } else if (a[hang + 2, lie] == 9) { a[hang + 2, lie] = a[hang + 1, lie]; a[hang + 1, lie] = a[hang, lie]; a[hang, lie] = 0; Console.Write("你赢了,请进入下一关"); break; } else { Console.Write("撞墙了"); } } } Console.WriteLine(); } }
效果图: