C# 我正在尝试集成一个数组

C# 我正在尝试集成一个数组,c#,C#,我试图从程序中提取总数,并将其放入一个数组中,以便在程序关闭时显示,但当前代码有一个问题,因为它获取最后输入的数字,并将其与数组插槽0中的数字一起挤压。我该如何解决这个问题 string[] Sales = new string[5]; int i = 0; string Invoice = ""; int numberOfInvoices = 0; decimal totalOfInvoices = 0m; decimal invoiceAverage = 0m; string Total =

我试图从程序中提取总数,并将其放入一个数组中,以便在程序关闭时显示,但当前代码有一个问题,因为它获取最后输入的数字,并将其与数组插槽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();
}
string[]Sales=新字符串[5];
int i=0;
字符串Invoice=“”;
int numberOfInvoices=0;
十进制总长度=0米;
十进制发票平均值=0m;
字符串总数=”;
私有void btnCalculate\u单击(对象发送者,事件参数e)
{
//设置变量
十进制小计=Convert.ToDecimal(txtEnterSubtotal.Text);
小数折扣百分比=0.25百万;
小数折扣额=数学四舍五入(小计*折扣百分比,2);
十进制发票总额=小计-折扣金额;
//从表单中检索数据
txtSubtotal.Text=subtotal.ToString(“c”);
txtDiscountPercent.Text=discountPercent.ToString(“p1”);
txtdecentamount.Text=discountAmount.ToString(“c”);
txtTotal.Text=invoiceTotal.ToString(“c”);
//预执行各种数学函数
numberOfInvoices++;
TotalOfVoices+=发票总额;
发票平均数=发票总数/发票数量;
总计=转换为字符串(invoiceTotal);
for(int i=0;i
这是我输入100、200、300、400和500时得到的结果

375.0075.00, 150.00, 225.00, 300.00, 375.00

我会使用一个列表

创建列表

var Sales = new List<int>();
您可以继续添加到列表中

看起来你不需要将数字转换成字符串,从外观上看,它只需要处理小数和整数


希望这有帮助。

使用列表和
string.Join()
方法存储总计的值,它将正常工作,不要使用数组。使用列表,以空开头,然后向其中添加元素。它会自动调整列表的大小,并在末尾添加新项。它必须保留一个数组,如果它不符合该要求,则我将使用列表。如果您出于任何原因需要保留它。。。也许您可以将数组转换为列表并使用它?
Sales.Add(total);