Skip to main content
 首页 » 编程设计

c#之如何从二维数组中随机选择一个位置

2024年02月05日19tuyile006

我目前正在开发一款控制台问答游戏,为此我需要从二维数组的随机类别中选择随机问题。但我不确定如何从二维数组中随机选取。

我无法显示我的完整阵列,因为它们非常大 - 下面是修剪后的版本:

string[,] mineKategorier = new string[10, 6] 
{ 
    {"C#","Question one here", "Question two here", "Question three here"}, 
    {"Spil engines", "Question one here", "Question two here", "Question three here"}, 
    {"Sport", "Question one here", "Question two here", "Question three here"}, 
}; 

请您参考如下方法:

Random rnd = new Random(); 
int row = rnd.Next(mineKategorier.GetLength(0)); 
int column = rnd.Next(mineKategorier.GetLength(1)); 
 
string randomKategori = mineKategorier[row, column];