C#基础视频中的实用方法封装统计 1、冒泡排序法public static void Paixu() { int[] num = { 12, 13, 23, 34, 45, 56, 67, 78, 89, 90, 21, 32, 43, 54, 65, 76, 87, 98, 9 }; for (int i = 0; i < num.Length - 1; i++) { for (int j = 0; j < num.Length - 1 - i; j++) { if (num[j] > num[j + 1])//从小到大排序,反之则从大到小 { int temp = num[j]; num[j] = num[j + 1]; num[j + 1] = temp; } } } for (int i = 0; i < num.Length; i++) { Console.Write(" {0}", num); } } 2、只能输入数字public static int ReadInt() { int number = 0; do { try { number = Convert.ToInt32(Console.ReadLine()); return number; } catch { Console.WriteLine("输入有误!请重新输入!"); } } while (true); } 3、闰年方法public static void Run() { int year = 0; Console.WriteLine("请输入一个年份?"); year = Convert.ToInt32(Console.ReadLine()); if ((year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0)) { Console.WriteLine("{0}年是闰年!", year); } } 4、九九乘法表(矩形)public static void Biao() { for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { Console.Write("{0}*{1}={2}\t", i, j, i * j);//"\t"是对齐的作用 } Console.WriteLine();//换行的作用 } } 5、九九乘法表(三角形)public static void Biao() { for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { Console.Write("{0}*{1}={2}\t", i, j, i * j);//"\t"是对齐的作用 } Console.WriteLine();//换行的作用 } } 总结:由于水平有限,就学了这么多,所以就总结这些吧!如有不当之处,还望指点! |