string strTemp = ""; int times = 0; Random ran = new Random(); for (int i = 0; ; i++) { int j = ran.Next(0, 6);//因为数组长度是7,所以这里范围就用0到6 if (!strTemp.Contains(str[j])) { strTemp += str[j] + ","; times++; if (times == 4) { break; } } } return strTemp;
string[] str2 = (from a in str select a ).Distinct().Take(4).ToArray();用linq吧
string strTemp = "";
int times = 0;
Random ran = new Random();
for (int i = 0; ; i++)
{
int j = ran.Next(0, 6);//因为数组长度是7,所以这里范围就用0到6
if (!strTemp.Contains(str[j]))
{
strTemp += str[j] + ",";
times++;
if (times == 4)
{
break;
}
}
}
return strTemp;