函数Calculate会产生一百个随机数(不相同)存入数组,并将这个数组作为函数的返回值。我在主函数中调用了两次Calculate函数,主函数main中home数组和man数组用来接收,最后用循环输出这两个数组中值的时候,诡异的事情发生了:两个数组中存储的元素都一样!然后我加断点跟踪调试,惊人的的事又发生了……
home和man两个数组中的值是不一样的!!!!为神马会如此灵异!!!求解啊!!!高手有木有!!!
上代码:
class Calculate//产生随机数的类
{
public int[] produce()
{
Random random = new Random();
int[] temporary = new int[100];
bool[] tf=new bool[100];//bool数组中元素的值如果是true,说明其对应的数字数组里面还没有装数字
bool judge=true;//如果生成的随机
int kk=-1;//保存临时生成的随机数
int element=0;//统计数组中元素个数
int y = 0;
int ee = 0;
for (int a = 0; a < tf.Length;a++ )
{
tf[a] = true;
}
while (element != 100)
{
judge = true;
kk =random.Next(101);
for (int a = 0; a < temporary.Length;a++ )
{
if(temporary[a]==kk)//如果里面有这个数就赋为false
{
judge = false;
break;
}
}
for (int a = 0; a < temporary.Length; a++)
{
if (tf[a])//如果里面有这个数就赋为falsej
{
if (judge)
{//数组里面没有这个数才能把这个数添加进数组
temporary[y] = kk;
element++;
tf[y] = false;
y++;
break;
}
}
}
}
return temporary;
}
}
//*********************分割线*************************
class Program//主函数所在的类
{
static void Main(string[] args)
{
Calculate pr = new Calculate();
int[] home = pr.produce();
int[] man = pr.produce();
for (int a = 0; a < home.Length;a++ )
{
Console.WriteLine("家{0} 人{1}",home[a],man[a]);
}
}
}