如题,代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { class Program { static void Main(string[] args) { Random r = new Random(); int 红球池 = 33, 蓝球池 = 16,剩余红球=6,应选红球=6,剩余蓝球=1/*,选蓝球=1*/,t; List<string> 红球, 蓝球,选择=new List<string>(); string 预选; 红球 = 初始化(红球池).ToList(); 蓝球 = 初始化(蓝球池).ToList(); //预选红球开始 do { Console.WriteLine("输入预选红球[回车跳过]:"); 预选 = Console.ReadLine(); if(预选.Length==1) { 预选 = "0" + 预选; } if(红球.Remove(预选)) { 选择.Add(预选); 剩余红球--; } } while (预选!=""); //预选红球结束 for (int i = 0; i < 剩余红球; i++) { t = r.Next(红球.Count); r = new Random(t); 选择.Add(红球[t]); 红球.RemoveAt(t); } 选择.Sort((x, y) => x.CompareTo(y)); //预选蓝球开始 do { Console.WriteLine("输入预选蓝球[回车跳过]:"); 预选 = Console.ReadLine(); if (预选.Length == 1) { 预选 = "0" + 预选; } if (蓝球.Remove(预选)) { 选择.Add(预选); 剩余蓝球--; } } while (预选 != ""); //预选蓝球结束 for (int i = 0; i < 剩余蓝球; i++) { t = r.Next(蓝球.Count); 选择.Add(蓝球[t]); 蓝球.RemoveAt(t); } //开始输出 Console.WriteLine("红球:"); for (t = 0; t< 应选红球; t++) { Console.Write($"{选择[t]} "); } Console.WriteLine(" 蓝球:"); for (; t < 选择.Count; t++) { Console.Write($"{选择[t]} "); } Console.Write(" 程序结束,按任意键退出"); Console.ReadKey(); } static IEnumerable<string> 初始化(int 个数) { for (int i = 0; i < 个数; i++) { if (i < 9) { yield return "0" + (i + 1).ToString(); } else { yield return (i + 1).ToString(); } } } } }
运行结果: