2014-11-15 52 views
-1

我想從字符串列表中生成一個字符串。我這樣做,但它只能顯示陣列數量:字符串列表中的隨機字符串

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
Console.WriteLine(fruits[r1.Next(fruits.Length)]); 

例如水果[2]排在第三位,因爲它起始於0