我正在尝试集成一个数组

时间:2017-03-27 22:06:33

标签: c#

我试图从程序中拉出总数并将其放入一个数组中,以便在程序关闭时显示,但是当前代码有问题,因为它输入了最后一个数字并将其与数组中的数字一起压扁插槽0.如何解决此问题?

string[] Sales = new string[5];
int i = 0;
string Invoice = "";
int numberOfInvoices = 0;
decimal totalOfInvoices = 0m;
decimal invoiceAverage = 0m;
string Total = "";

private void btnCalculate_Click(object sender, EventArgs e)
{
    //sets the variables
    decimal subtotal = Convert.ToDecimal(txtEnterSubtotal.Text);
    decimal discountPercent = .25m;
    decimal discountAmount = Math.Round(subtotal * discountPercent, 2);
    decimal invoiceTotal = subtotal - discountAmount;

    //retrieves the data from the form
    txtSubtotal.Text = subtotal.ToString("c");
    txtDiscountPercent.Text = discountPercent.ToString("p1");
    txtDiscountAmount.Text = discountAmount.ToString("c");
    txtTotal.Text = invoiceTotal.ToString("c");

    //preforms the various math functions
    numberOfInvoices++;
    totalOfInvoices += invoiceTotal;
    invoiceAverage = totalOfInvoices / numberOfInvoices;

    Total = Convert.ToString(invoiceTotal);
    for (int i = 0; i < Sales.Length; i++) ;
    {
        Sales[i] += Total + "\n";
    }

    txtNumberOfInvoices.Text = numberOfInvoices.ToString();
    txtTotalOfInvoices.Text = totalOfInvoices.ToString("c");
    txtInvoiceAverage.Text = invoiceAverage.ToString("c");

    txtEnterSubtotal.Text = "";

    //sets the focus on Enter Subtotal
    txtEnterSubtotal.Focus();

}
private void btnClearTotals_Click(object sender, EventArgs e)
{
    numberOfInvoices = 0;
    totalOfInvoices = 0m;
    invoiceAverage = 0m;

    txtNumberOfInvoices.Text = "";
    txtTotalOfInvoices.Text = "";
    txtInvoiceAverage.Text = "";

    txtEnterSubtotal.Focus();
}
private void btnExit_Click(object sender, EventArgs e)
{
    for (int i = 0; i < 5; i++) ;
    { 
    foreach (string Tot in Sales)

        {
            Total += Tot;
        }
    }
    MessageBox.Show(Total, "Invoices");
    //closes the program
    this.Close();
}

这是我输入100,200,300,400和500时的结果。

  

375.0075.00,   150.00,   225.00,   300.00,   375.00

1 个答案:

答案 0 :(得分:0)

我会用一个清单......

创建列表

var Sales = new List<int>();

然后添加到列表

Sales.Add(total);

您可以继续添加到列表中。

看起来你正在不必要地将数字转换为字符串,从它的外观看起来只是使用小数和整数。

希望这有帮助。