0
我想知道如何將多維數組中的所有數據求和。多維數組中所有產品的總和
這是我的add函數,用於對多維數組中的所有數據進行求和。
private int sum2D(int[,] week)
{
int sum = 0;
foreach (int d in week)
sum += d;
return sum;
}
這只是返回一個零量,我很好奇,爲什麼它不工作,這是我的數組:
int[,] week = new int[4, 5];
,這是什麼決定了我的數組中的值:
for (int Row = 0; Row < week.GetLength(0); Row++)
{
if (Row == 0)
{
Week = "Week 1 ";
}
else if (Row == 1)
{
Week = "Week 2 ";
}
else if (Row == 2)
{
Week = "Week 3 ";
}
else if (Row == 3)
{
Week = "Week 4 ";
}
Output += "\r\n" + Week + ": ";
for (int Col = 0; Col < week.GetLength(1); Col++)
{
if (Col == 0)
{
Day = "Monday";
}
else if (Col == 1)
{
Day = "Tuesday";
}
else if (Col == 2)
{
Day = "Wednesday";
}
else if (Col == 3)
{
Day = "Thursday";
}
else if (Col == 4)
{
Day = "Friday";
}
string value = Microsoft.VisualBasic.Interaction.InputBox("Enter the amount of products made on " + Day + " for " + Week, "Product Amount");
Output += " ";
Output += Int32.Parse(value) + " ";
txtOutput.Text = Output;
}
不是'new int [4,5]'一維數組而不是多維? – tomasbasham
你在哪裏填充陣列? – Valentin
@tomasbasham nope,[C#語言規範](https://www.microsoft.com/download/details.aspx?id=70299)聲明*「數組類型的等級由最左邊的等級說明符在數組類型中:一個等級說明符指示該數組是一個等級爲1的數組加上等級說明符中的「,」標記的數量。[...]類型int [] [,,] [, ]是int的二維數組的三維數組的一維數組。「* – Albireo