Random r = new Random();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
int num = r.Next(10, 60);
Console.Write(num.ToString()+" ");
if (j == 2)
{
//控制换行
Console.WriteLine();
}
}
}
Console.ReadLine();
Random r = new Random();
int[,] arr = new int[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i, j] = r.Next(10, 60);
Console.Write(arr[i, j].ToString() + " ");
}
Console.WriteLine();
}
Console.ReadLine();
二楼正解.....
二楼写的没有避免重复,有可能出现重复的数!
static void Main(string[] args)
{
int [,] arr=new int[3,3];
int[] index = new int[100];
Random r=new Random();
int[] result = new int[9];
int site = 100;
int id,i;
for (i = 0; i < 9; i++)
{
id = r.Next(1, site - 1);
result[i] = id;
index[id] = index[site- 1];
site--;
}
i = 0;
for (int m=0;m<3;m++)
{
for (int j=0;j<3;j++)
{
arr[j,m]=result[i];
i++;
Console.Write(arr[j, m].ToString() + " ");
}
Console.WriteLine();
}
Console.ReadKey();
}