3 20 22 31 52. 任何人都可以提供帮助吗? 我尝试了什么:i have this code which displays random number of 5 different ranges. when code is executed it displays thisB I N G O1 11 30 35 455 15 25 38 503 20 22 31 52Now i want to put a star(*) or null in the middle row.something like this:B I N G O1 11 30 35 455 15 NULL 38 503 20 22 31 52.CAN ANYONE HELP?What I have tried:Random rand = new Random(); HashSet<int> number1 = new HashSet<int>(); HashSet<int> number2 = new HashSet<int>(); HashSet<int> number3 = new HashSet<int>(); HashSet<int> number4 = new HashSet<int>(); HashSet<int> number5 = new HashSet<int>(); int range1; int range2; int range3; int range4; int range5; Console.WriteLine("B I N G O"); for (int i = 0; i <= 10; i++) { do { range1 = rand.Next(0, 10); } while (number1.Contains(range1)); number1.Add(range1); do { range2 = rand.Next(11,21); } while (number2.Contains(range2)); number2.Add(range2); do { range3 = rand.Next(22,32); } while (number3.Contains(range3)); number3.Add(range3); do { range4 = rand.Next(33,43); } while (number3.Contains(range4)); number4.Add(range4); do { range5 = rand.Next(44, 54); } while (number5.Contains(range5)); number5.Add(range5); Console.WriteLine("{0} {1} {2} {3} {4}",range1, range2,range3, range4, range5); } Console.ReadLine(); }推荐答案这是一个更有效的解决方案:Here is a more efficient solution:Random rand = new Random();List<int> list1 = new List<int> {0,1,2,3,4,5,6,7,8,9,10};List<int> list2 = new List<int> {11,12,13,14,15,16,17,18,19,20,21};List<int> list3 = new List<int> {22,23,24,25,26,27,28,29,30,31,32};List<int> list4 = new List<int> {33,34,35,36,37,38,39,40,41,42,43};List<int> list5 = new List<int> {44,45,46,47,48,49,50,51,52,53,54};Console.WriteLine("B I N G O");for (int i = 0; i <= 10; i++){string range3string;// Console.WriteLine("count = " + list1.Count);var r = rand.Next(0, list1.Count);int range1 = list1[r];list1.RemoveAt(r);r = rand.Next(0, list2.Count);int range2 = list2[r];list2.RemoveAt(r);r = rand.Next(0, list3.Count);int range3 = list3[r];list3.RemoveAt(r);r = rand.Next(0, list4.Count);int range4 = list4[r];list4.RemoveAt(r);r = rand.Next(0, list5.Count);int range5 = list5[r];list5.RemoveAt(r);if (i == 5)range3string = "NULL";elserange3string = range3.ToString();Console.WriteLine("{0} {1} {2} {3} {4}",range1, range2, range3string, range4, range5);}Console.ReadLine();小心Be careful} while (number3.Contains(range4));您可以为range3使用字符串变量,例如range3string:You could use a string variable for range3, e.g. range3string:string range3string;if (<your condition>) range3string = range3;else range3string = "NULL"; 更具体一点:To be more specific:Random rand = new Random(); HashSet<int> number1 = new HashSet<int>(); HashSet<int> number2 = new HashSet<int>(); HashSet<int> number3 = new HashSet<int>(); HashSet<int> number4 = new HashSet<int>(); HashSet<int> number5 = new HashSet<int>(); int range1; int range2; int range3; int range4; int range5; string range3string; Console.WriteLine("B I N G O"); for (int i = 0; i <= 10; i++) { do { range1 = rand.Next(0, 10);  } while (number1.Contains(range1)); number1.Add(range1); do { range2 = rand.Next(11,21); } while (number2.Contains(range2)); number2.Add(range2);  do { range3 = rand.Next(22,32); } while (number3.Contains(range3)); number3.Add(range3); do { range4 = rand.Next(33,43); } while (number4.Contains(range4)); number4.Add(range4);  do { range5 = rand.Next(44, 54); } while (number5.Contains(range5)); number5.Add(range5);  if (i == 5) range3string = "NULL"; else range3string = range3.ToString(); Console.WriteLine("{0} {1} {2} {3} {4}",range1, range2, range3string, range4, range5); } Console.ReadLine(); } 这篇关于在随机数的中间放置一个null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 14:59