我正在使用C#和Microsoft Visual Studio。我可以通过以下代码显示我的数组:
private void btnDisplay_Click(object sender, EventArgs e)
{
double[,] initialArray = new double[3, 4] { { 5, 1, 9, 3 }, { 7, 8, 6, 4 }, { 2, 4, 9, 5 } };
string rowOfInts = "";
string columnsAndRow = "";
for (int r = 0; r < initialArray.GetLength(0); r++)
{
string tempString = "";
for (int c = 0; c < initialArray.GetLength(1); c++)
{
rowOfInts = tempString + " " + initialArray[r, c];
tempString = rowOfInts;
}
columnsAndRow = columnsAndRow + rowOfInts + "\n";
lblDisplay.Text = Convert.ToString(columnsAndRow);
}
// displays Display = new displays(initialArray, rowOfInts, columnsAndRow);
if (chkRowTotals.Checked == true)
{
for (int r = 0; r < initialArray.GetLength(0); r++)
{
int intTotal = 0;
string tempString = "";
for (int c = 0; c <initialArray.GetLength(1); c++)
{
rowOfInts = tempString + " " + initialArray[r, c];
tempString = rowOfInts;
}
columnsAndRow = columnsAndRow + rowOfInts;
intTotal += Convert.ToInt32(columnsAndRow);
lblDisplay.Text = Convert.ToString(intTotal);
}
}
}
但我不知道如何按行添加数字。有什么方法可以按行累加数组中的数字,然后在我的标签中显示它们(lblDisplay)?
编辑:我不希望整个数组加起来 - 只是行。因此,输出将是18,25和20。
答案 0 :(得分:2)
您的代码中存在许多错误,完全诚实。我会把我认为是您正在寻找的解决方案:
private void Button_Click(object sender, RoutedEventArgs e)
{
double[,] initialArray = new double[3, 4] { { 5, 1, 9, 3 }, { 7, 8, 6, 4 }, { 2, 4, 9, 5 } };
string rowOfInts = "";
string columnsAndRow = "";
for (int r = 0; r < initialArray.GetLength(0); r++)
{
string tempString = "";
double inttotal = 0;
for (int c = 0; c < initialArray.GetLength(1); c++)
{
rowOfInts = tempString + " " + initialArray[r, c];
tempString = rowOfInts;
inttotal += initialArray[r, c];
}
columnsAndRow = columnsAndRow + rowOfInts + " row total of = " + inttotal.ToString() + "\n";
}
txtbx.Text = Convert.ToString(columnsAndRow);
}
答案 1 :(得分:0)
产生效果:
const int numRows = 3;
const int numColumns = 4;
double[,] initialArray = new double[numRows, numColumns] { { 5, 1, 9, 3 }, { 7, 8, 6, 4 }, { 2, 4, 9, 5 } };
double[] rowTotal = new double[numRows];
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numColumns; j++)
{
rowTotal[i] += initialArray[i, j];
}
Console.WriteLine("Current row total: {0}", rowTotal[i]);
}
答案 2 :(得分:0)
试试这个:
var initialArray = new double[3, 4] { { 5, 1, 9, 3 }, { 7, 8, 6, 4 }, { 2, 4, 9, 5 } };
// List that will hold the sum of each row
List<double> rowSums = new List<double>();
for (int i = 0; i < initialArray.GetLength(0); i++)
{
// accumulator
double rowSum = 0;
for (int j = 0; j < initialArray.GetLength(1); j++)
{
rowSum += initialArray[i, j];
}
// add the row sum to our list
rowSums.Add(rowSum);
}
// will put "18, 25, 20" in your label's Text
lblDisplay.Text = string.Join(", ", rowSums);
您应该考虑使用flattening your array,因为它有更好的效果。
答案 3 :(得分:0)
您可以使用以下Linq:
var numberOfRows = initialArray.GetLength(0);
var numberOfColumns = initialArray.GetLength(1);
var sumOfRows = Enumerable
.Range(0, numberOfRows)
.Select(r => Enumerable.Range(0, numberOfColumns).
Aggregate(0d, (a, i) => a += initialArray[r, i]));
lblDisplay.Text = string.Join(", ", sumOfRows);
但是,使用Jagged数组可能要容易得多。 Linq表达式会容易得多。