题目:推箱子不完整程序,该程序可嵌入自定义地图;无法做到返回操作!
int[,] s = new int[,] { {2,2,2,2,2,2,2,2,2,2}, {2,0,0,0,3,3,0,0,0,2}, {2,0,0,0,1,1,0,0,0,2}, {2,0,0,0,0,0,0,0,0,2}, {2,3,1,0,4,0,0,0,0,2}, {2,3,1,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,2}, {2,0,0,0,1,1,0,0,0,2}, {2,0,0,0,3,3,0,0,0,2}, {2,2,2,2,2,2,2,2,2,2} }; int o = 0; for (; ; ) { if (o == 1) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (s[i, j] == 0) { Console.Write(" "); if (j == 9) Console.WriteLine(); } if (s[i, j] == 1) { Console.Write("□"); if (j == 9) Console.WriteLine(); } if (s[i, j] == 2) { Console.Write("※"); if (j == 9) Console.WriteLine(); } if (s[i, j] == 3) { Console.Write("×"); if (j == 9) Console.WriteLine(); } if (s[i, j] == 4) { Console.Write("●"); if (j == 9) Console.WriteLine(); } } } Console.WriteLine("恭喜你闯关成功"); break; } else { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (s[i, j] == 0) { Console.Write(" "); if (j == 9) Console.WriteLine(); } if (s[i, j] == 1) { Console.Write("□"); if (j == 9) Console.WriteLine(); } if (s[i, j] == 2) { Console.Write("※"); if (j == 9) Console.WriteLine(); } if (s[i, j] == 3) { Console.Write("×"); if (j == 9) Console.WriteLine(); } if (s[i, j] == 4) { Console.Write("●"); if (j == 9) Console.WriteLine(); } } } int x = 0, y = 0, p = 0; for (int x1 = 0; x1 < 10; x1++) { if (p != 0) break; else { for (int y1 = 0; y1 < 10; y1++) { if (s[x1, y1] == 4) { p += 1; x = x + x1; y = y + y1; break; } else if (x1 == 9 && y1 == 9 && s[9, 9] != 4) { Console.WriteLine("错误!"); } } } } Console.Write("请通过输入w向上,s向下,a向左,d向右移动人物●,将箱子□推到指定位置×;"); ConsoleKeyInfo fangxiang = Console.ReadKey(); string a = fangxiang.Key.ToString().ToLower(); if (a == "w") { if (s[x - 1, y] == 2 || s[x - 1, y] == 1 && s[x - 2, y] == 2 || s[x - 1, y] == 1 && s[x - 2, y] == 1 || s[x - 1, y] == 2 && s[x - 2, y] == 4 || s[x - 1, y] == 3){ } else { s[x, y] = 0; s[x - 2, y] = Math.Abs(s[x - 2, y] - s[x - 1, y]); s[x - 1, y] = 4; x -= 1; } } if (a == "s") { if (s[x + 1, y] == 2 || s[x + 1, y] == 1 && s[x + 2, y] == 2 || s[x + 1, y] == 1 && s[x + 2, y] == 1 || s[x + 1, y] == 2 && s[x + 2, y] == 4 || s[x + 1, y] == 3) { } else { s[x, y] = 0; s[x + 2, y] = Math.Abs(s[x + 2, y] - s[x + 1, y]); s[x + 1, y] = 4; x += 1; } } if (a == "a") { if (s[x, y - 1] == 2 || s[x, y - 1] == 1 && s[x, y - 2] == 2 || s[x, y - 1] == 1 && s[x, y - 2] == 1 || s[x, y - 1] == 2 && s[x, y - 2] == 2 || s[x , y-1] == 3) { } else { s[x, y] = 0; s[x, y - 2] = Math.Abs(s[x, y - 2] - s[x, y - 1]); s[x, y - 1] = 4; y -= 1; } } if (a == "d") { if (s[x, y + 1] == 2 || s[x, y + 1] == 1 && s[x, y + 2] == 2 || s[x, y + 1] == 1 && s[x, y + 2] == 1 || s[x, y + 1] == 2 && s[x, y + 2] == 2 || s[x , y+1] == 3) { } else { s[x, y] = 0; s[x, y + 2] = Math.Abs(s[x, y + 2] - s[x, y + 1]); s[x, y + 1] = 4; y += 1; } } int h = 0; for (int b = 0; b < 10; b++) { if (h == 1) break; else { for (int c = 0; c < 10; c++) { if (s[b, c] == 3) { h = 1; break; } if (b == 9 && c == 9 && s[9, 9] != 3) { o = 1; } } } } Console.Clear(); } }
题目:生成十个不重复的数放入数组并打印出来;
Random ran = new Random(); int[] s=new int[10]; s[0] = ran.Next(); int x = 0; for(int i = 1;; i++) { if (x == 9) break; else { int a = ran.Next(); for (int j = 0; j < i && j < 10; j++) { if (a - s[j] == 0) break; if (j == 9 && a - s[9] != 0) { x += 1; s[x] = a; } } } } for (int y = 0; y < 10; y++) { Console.WriteLine("s["+y+"]="+s[y]); }
题目:五个候选班长,20人投票。(输入1-5来表示支持哪个人)最后查看票数,看那个人胜出。
int[] s=new int[20]; for(int i=0;i<20;i++) { Console.Write("请问你支持的候选人是几号:"); for (; ; ) { int a = int.Parse(Console.ReadLine()); if (a < 1 || a > 5) Console.Write("输入有误,请重新输入您支持的候选人号码:"); else { s[i] = a; break; } } } int s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0; for (int j = 0; j < 20; j++) { if(s[j] ==1) s1 += 1; if (s[j] == 2) s2 += 1; if (s[j] == 3) s3 += 1; if (s[j] == 4) s4 += 1; if (s[j] == 5) s5 += 1; } if (s1 >= s2 && s1 >= s3 && s1 >= s4 && s1 >= s5) Console.Write("一号 "); if (s2 >= s1 && s2 >= s3 && s2 >= s4 && s2 >= s5) Console.Write("二号 "); if (s3 >= s2 && s3 >= s1 && s3 >= s4 && s3 >= s5) Console.Write("三号 "); if (s4 >= s2 && s4 >= s3 && s4 >= s1 && s4 >= s5) Console.Write("四号 "); if (s5 >= s2 && s5 >= s3 && s5 >= s4 && s5 >= s1) Console.Write("五号 "); Console.WriteLine("获胜");
题目:输入n个手机号,做抽奖活动
Console.Write("请输入能容纳的手机号个数n="); int n = int.Parse(Console.ReadLine()); Random ran = new Random(); string[] s = new string[n]; for (int i = 0; i < n; i++) { Console.Write("请输入您的手机号:"); for (; ; ) { string a = Console.ReadLine(); int j = a.Length; if (j == 11) { s[i] = a; break; } else Console.Write("您的手机号输入有误,请重新输入:"); } } for (int a=0;a<50 ;a++ ) { int x = ran.Next(n); if (a == 49) { Console.Clear(); Console.WriteLine("恭喜手机号是" + s[x] + "的机主获得本次活动的大奖"); } else { Console.Clear(); Console.WriteLine("中奖号码是:" + s[x]); System.Threading.Thread.Sleep(100); } }
题目:冒泡排序;
int[] shuzu = new int[] {3,1,8,4,0,5,6 }; int zhong; for(int i=0;i<7;i++) { for (int j = i + 1; j < 7; j++) { if (shuzu[i] < shuzu[j]) { zhong = shuzu[i]; shuzu[i] = shuzu[j]; shuzu[j] = zhong; } } } for (int i = 0; i < 7; i++) { Console.Write(shuzu[i]+" "); }
知识点:foreach(int a in shuzu)//遍历数组,一般只用于打印; object接收全部类型;常用于不知道的数组类型的时候;
题目:输入人数,输入成绩,求最高分,最低分;去掉两个最高,两个最低,求平均分;
Console.Write("请输入班级人数n="); int n = int.Parse(Console.ReadLine()); double[] shuzu = new double[n]; for (int i = 0; i < n; i++) { Console.Write("请输入第" + (i + 1) + "个同学的成绩:"); double d = double.Parse(Console.ReadLine()); shuzu[i] = d; } double zhong; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (shuzu[i] > shuzu[j]) { zhong = shuzu[i]; shuzu[i] = shuzu[j]; shuzu[j] = zhong; } } } Console.WriteLine("最低分是"+shuzu[0]); Console.WriteLine("最高分是"+shuzu[n-1]); double sum = 0; for (int i = 2; i < n - 2; i++) { sum += shuzu[i]; } Console.WriteLine("平均分是" + sum / (n - 4));
知识点;ConsoleKeyInfo fangxiang = Console.ReadKey();//定义接收一次不用回车就执行;
string a = fangxiang.Key.ToString().ToLower();//定义类型,转化类型,转化成小写;
知识点;获取满足特殊条件的数组元素的索引号;
结构如下。功能;获取数组里面等于4的元素的索引值;
int[,] s = new int[,] { {2,2,2,2,2,2,2,2,2,2}, {2,0,0,0,3,3,0,0,0,2}, {2,0,0,0,1,1,0,0,0,2}, {2,0,0,0,0,0,0,0,0,2}, {2,3,1,0,4,0,0,0,0,2}, {2,3,1,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,2}, {2,0,0,0,1,1,0,0,0,2}, {2,0,0,0,3,3,0,0,0,2}, {2,2,2,2,2,2,2,2,2,2} }; int x = 0, y = 0, p = 0; for (int x1 = 0; x1< 10; x1++) { if (p != 0) break; else { for (int y1 = 0; y1 < 10; y1++) { if (s[x1, y1] == 4) { p += 1; x = x + x1; y = y + y1; break; } else if (x1 == 9 && y1 == 9 && s[9, 9] != 4) { Console.WriteLine("错误!"); } } } } Console.WriteLine(x+" "+y);
知识点:函数;能够独立完成某项功能的
结构;返回类型,函数名,
//传值的四种方式; /// <summary> /// 累加求和方法1;没有输入值,没有返回值; /// </summary> public void leijiaqiuhe1() { int sum = 0; Console.Write("请输入一个正整数n="); int n = int.Parse(Console.ReadLine()); for (int i = 1; i <= n; i++) { sum += i; } Console.Write(sum); } /// <summary> /// 累加求和方法2,有输入值,没有返回值; /// </summary> /// <param name="args"></param> public void leijiaqiuhe2(int n) { int sum = 0; for (int i = 1; i < +n; i++) { sum += i; } Console.WriteLine(sum); } /// <summary> /// 累加求和方法3,没有输入值,有返回值; /// </summary> /// <param name="args"></param> public int leijiaqiuhe3() { int sum = 0; Console.Write("请输入一个正整数n="); int n = int.Parse(Console.ReadLine()); for (int i = 1; i <= n; i++) { sum += i; } return sum; } /// <summary> /// 累加求和方法4,有输入值,有返回值; /// </summary> /// <param name="args"></param> public int lejiaqiuhe4(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } return sum; }
题目:新建阶乘累加求和函数;
public int jiechengleijiaqiuhe(int n) { int sum = 0,s = 1; for (int i = 1; i <= n; i++) { s = s * i; sum += s; } return sum; }
题目:新建求范围内质数的和,被打印质数;
/// <summary> /// 求范围内质数的和,并且打印出质数;有输入,没返回; /// </summary> /// <param name="args"></param> public void zhishu(int n) { int sum = 0; for (int i = 0; i <= n; i++) { for (int j = 2; j <= i; j++) { sum += i; if (i == j) { Console.Write(i + "\t"); break; } else if (i % j == 0) { sum -= i; break; } else { sum -= i; } } } Console.WriteLine(); Console.WriteLine("100以内质数的和为:" + sum); }