数组不接受多个输入

时间:2017-07-21 21:07:17

标签: c# arrays

因此我购买了Murach的C#2015书籍,试图开始学习一些代码。我现在在一个关于阵列的部分,我被卡住了。数组似乎只接受一个输入。我需要它来接收用户的每个输入并保存,然后在消息框中反刍。我是否正确放置了阵列并正确循环?

这是代码批量

public partial class frmInvoiceTotal : Form
{
    public frmInvoiceTotal()
    {
        InitializeComponent();
    }
    decimal[] totals = new decimal[5];
    int index = 0;


    // TODO: declare class variables for array and list here

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        try
        {              
                if (txtSubtotal.Text == "")
                {
                    MessageBox.Show(
                        "Subtotal is a required field.", "Entry Error");
                }
                else
                {
                for (index = 0; index < totals.Length; index++)    //this the correct spot for the for loop?
                {

                    decimal subtotal = Decimal.Parse(txtSubtotal.Text);
                    totals[index] = subtotal;
                    if (subtotal > 0 && subtotal < 10000)
                    {
                        decimal discountPercent = 0m;
                        if (subtotal >= 500)
                            discountPercent = .2m;
                        else if (subtotal >= 250 & subtotal < 500)
                            discountPercent = .15m;
                        else if (subtotal >= 100 & subtotal < 250)
                            discountPercent = .1m;
                        decimal discountAmount = subtotal * discountPercent;
                        decimal invoiceTotal = subtotal - discountAmount;


                        discountAmount = Math.Round(discountAmount, 2);
                        invoiceTotal = Math.Round(invoiceTotal, 2);

                        txtDiscountPercent.Text = discountPercent.ToString("p1");
                        txtDiscountAmount.Text = discountAmount.ToString();
                        txtTotal.Text = invoiceTotal.ToString();
                    }



                    else
                    {
                        MessageBox.Show(
                            "Subtotal must be greater than 0 and less than 10,000.",
                            "Entry Error");
                    }
                }
            }
        }
        catch (FormatException)
        {
            MessageBox.Show(
                "Please enter a valid number for the Subtotal field.",
                "Entry Error");
        }
        txtSubtotal.Focus();

    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        MessageBox.Show("totals\n" + totals[0]  + "\n"+ totals[1] + "\n" + totals[2] + 
            "\n" + totals[3] + "\n" + totals[4], "test");
        //string totalsString = "";
        //foreach (int index in totals)            
            //totalsString += subtotal + "\n";
            //MessageBox.Show("The totals are:\n" +
                //totalsString + "\n", "Order Totals");

        // TODO: add code that displays dialog boxes here

        this.Close();
    }

}

}

1 个答案:

答案 0 :(得分:0)

我在你的循环中看到你总是实例化索引&#39;因此,整个数组得到当前输入而不是下一个单元格。 如果我理解正确,你的for循环应该是这样的:

for(; index&lt; totals.Length; index ++){}

请注意,您将被限制为5个输入(阵列的初始大小)。