字符串列表中的随机字符串

时间:2014-11-15 17:38:16

标签: c# random

我想从字符串列表中生成一个字符串。我做了这个,但它只显示了数组的数量:

Random r1 = new Random();
string[] fruits = new string[7];
fruits[0] = "cherry";
fruits[1] = "blueberry";
fruits[2] = "banana";
fruits[3] = "melon";
fruits[4] = "apple";
fruits[5] = "pear";
fruits[6] = "watermelon";

Console.WriteLine(r1.Next(fruits.Length));

1 个答案:

答案 0 :(得分:1)

    Console.WriteLine(fruits[r1.Next(fruits.Length)]);

E.g。 Fruits [2]是数组中的第3个,因为它从0开始