0

我正在尝试打印出二维数组中的数字总数。

我尝试做我的代码中的事情,但它给了我每行的平均值,而不是行和列中所有数字的总和。

int[,] prices = new int[,]{ {475, 450, 450, 475 },

                          {425, 425, 425, 425 },

                          {410, 400, 400, 410 },

                          {400, 375, 375, 400 },

                          {350, 340, 340, 350 },

                          {325, 325, 325, 325 }};
int row;
int col;
int price;
int revSum;


Write("Enter a row number: ");
while (!(int.TryParse(ReadLine(), out row)) || !(row > 0 && row <= 6))
{
     Write("Invalid row number.  Please try again: ");
}

Write("Enter a col number: ");
while(!(int.TryParse(ReadLine(), out col)) || !(col >0 && col <= 4))
{
Write("Invalid col number. Please try again: ");
}

WriteLine($"\nThe price of row {row}, col {col} is ${prices[row - 1, col - 
1]}\n");

 revSum = 0;

for (int i = 0; i < prices.GetLength(0); i++)
{
revSum = 0;
for (int j = 0; j < prices.GetLength(1); j++)
{
revSum += prices[i, j];
}
WriteLine($"The total revenue is {i + 1} {(double)revSum / 7:N1}");

我真的很想打印出所有数字的总和。

4

0 回答 0