2024-09-07 00:03:02
int[,] arr = new int[4, 4];
int row,col;
Random rand = new Random();
int sum1 = 0;
int sum2 = 0;
Console.Write("随机二维数组:\n");
for (row = 0; row < 4; row++)
{
for (col = 0; col < 4; col++)
{
arr[row,col]=rand.Next (10,100);
Console.Write(arr[row, col] + " ");
}
Console.Write("\n");
}
Console.Write("\n正对角线元素之和:");
for (row = 0; row < 4; row++)
{
sum1+=arr[row, row];
}
Console.Write(sum1);
Console.Write("\n斜对角线元素之和:");
for (col = 0; col < 4; col++)
{
sum2+=arr[3 - col, col];
}
Console.Write(sum2);
Console.ReadKey();