标签:style blog color strong for ar 2014 div log
例1:彩票生成器 36选7.
int[] a = new int[7]; Random ran = new Random();
//生成7个数 for (; a[6] == 0; ) { int n = ran.Next(36) + 1; //检查是否重复并赋值给数组每个元素 for (int j = 0; j < 7; j++) { if (a[j] == n) { break; } if (a[j] == 0) { a[j] = n; break; } } } //输出数组 for (int i = 0; i < 7; i++) { Console.Write(a[i] + "\t"); }
2. 方法二:
int[] a = new int[7]; Random rand = new Random(); for (int i = 0; i < 7; i++) { int n = rand.Next(36) + 1; bool cf = false; for (int j = 0; j < i; j++) { if (n == a[j]) { cf == true; break; } } if (cf = false) break; } else { i--; }
例2:在十个手机号码中随机生成一个号码--抽奖
//前景置色,号码显示颜色为红色. Console.ForegroundColor=ConsoleColor.Red; //背景置色,背景显示为黄色 // Console.BackgroundColor = ConsoleColor.Yellow; string[] s=new string[10]{"13025577889","13102277866","13233788677","13399866756","13499800989","13566577867","13655786789","13799866521","13876988767","139776598769"};
//随机生成一个号码 Random rand=new Random();
//运行10S后停止 DateTime overtime=DateTime.Now.AddSeconds(10); //当前时间小于运行10S后的时间 while(DateTime.Now<=overtime) { //清屏 Console.Clear(); //--生成一个10以内的数,生成这个数组中的一个元素. int n=rand.Next(10); Console.WriteLine(s[n]); } //作弊--清屏后显示18766971061. Console.Clear(); Console.WriteLine("18766971061");
冒泡排序:
是按照数组数值大小依次按照升序或降序的顺序排列,以便于比较数值大小.
例:
//生成一个数组,赋值 int[] a = new int[7] { 7, 6, 8, 4, 5, 1, 9 }; //两层for(),7个数值比较完一次用6趟,依次递减. for (int i = 1; i <= 6; i++) { for (int j = 1; j <= 7 - i; j++) { //比较判断然后将数值更换循环. if (a[j - 1] > a[j]) { int t = a[j - 1]; a[j - 1] = a[j]; a[j] = t; } } } for (int i = 0; i < a.Length; i++) { //输出 Console.WriteLine(a[i]); }
标签:style blog color strong for ar 2014 div log
原文地址:http://www.cnblogs.com/DORCASQING/p/3933773.html