如何找到二维阵列中的产品收入

时间:2019-06-21 20:27:12

标签: c# arrays revenue

我正在尝试打印2D数组内部的总数。

我尝试执行代码中的操作,但是它给了我每一行的平均值,而不是行和列内所有数字的总数。

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}");

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

0 个答案:

没有答案