gpt4 book ai didi

c# - 随机化一系列餐厅

转载 作者:行者123 更新时间:2023-12-01 20:03:47 24 4
gpt4 key购买 nike

对于我的计算机科学入门类(class),我必须制作一个应用程序来选择具有各种功能的餐厅,但我不知道如何随机化数组。下面是我的代码。

string[] myRestaurants = new string[9];

myRestaurants[0] = "Wendy's";
myRestaurants[1] = "Arby's";
myRestaurants[2] = "Olive Garden";
myRestaurants[3] = "The Pie";
myRestaurants[4] = "The Cheesecake Factory";
myRestaurants[5] = "Beto's";
myRestaurants[6] = "Dillinger's Saloon";
myRestaurants[7] = "Dayz Alpher";
myRestaurants[8] = "Firehouse subs";

var nextArray = myRestaurants.ToList();
Random rng = new Random();
int n = nextArray.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = nextArray[k];
nextArray[k] = nextArray[n];
nextArray[n] = value;
//bang
}

最佳答案

您可以使用 LINQ 和 OrderBy 方法对其进行随机化。结合您的 Random 类:

myRestaurants = myRestaurants
.OrderBy(x => myRandom.Next(myRestaurants.Length))
.ToArray();

关于c# - 随机化一系列餐厅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20695734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com