嗨尝试使应用程序随机数,我尝试返回数组值,但我得到错误
只有赋值,调用,递增,递减,等待和新对象表达式才能用作语句
on b[group.Key + group.Count()];
public int[] twodigi(int number)
{
Random random = new Random();
int var = Convert.ToInt32(number);
int[] a = new int[var];
for (int i = 0; i < var; i++)
{
int randomnumber = random.Next(10, 100);
a[i] = randomnumber;
}
var groups = a.GroupBy(v => v).OrderByDescending(g => g.Count());
int[] b;
foreach (var group in groups)
{
b[group.Key + group.Count()];
}
return b;
}
答案 0 :(得分:1)
希望你能这样试试:
return a.GroupBy(v => v).Select(x=> x.Key + x.Count()).ToArray();
如果您希望结果数组中每个键的总和以及与该键相关联的元素数。请完成此example,并告诉我您的结果与此不同:
方法签名将如下更改:
public static int[] twodigi(int number)
{
Random random = new Random();
int[] a = new int[number];
for (int i = 0; i < number; i++)
{
a[i] = random.Next(10, 100);
}
return a.GroupBy(v => v).Select(x=> x.Key + x.Count()).ToArray();
}
答案 1 :(得分:0)
正如其他人所说,你的问题就在这里:
int[] b;
foreach (var group in groups)
{
// What do you actually want to do with this?
b[group.Key + group.Count()];
}
编译器告诉你,目前还不清楚你打算用它做什么。
此外,b
永远不会被初始化。