c# - C#中如何将一行中的数字相加并显示?

标签 c# arrays

我正在使用 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。

最佳答案

老实说,您的代码中有很多错误。我只会提出我认为您正在寻找的解决方案:

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

关于c# - C#中如何将一行中的数字相加并显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37031522/

相关文章:

c# - 如何跨 Protobuf 发送多种类型的对象?

javascript - 表不是由多维数组构成的

python - 将值分配给 Numpy 数组中的不同索引位置

javascript - 检查数组中的值

c# - 当没有从 ddl 中选择值时插入空值

c# - 带有图标和复选框的 ToolStripControlHost。如何突出显示所选项目?在图标字段中添加图标?

c# - 在数据表中添加标签

c# - 如何用数组搜索数组?

python - """TypeError: only length-1 arrays can be converted to Python scalars"""尝试在具有 7 段显示的 Pi3 上运行计数器时

c# - 使用通用接口(interface)配置通用工作流程