-1

我试图让我的消息框显示存储在我的数组中的发票小计...其中 5 个使用 foreach 方法显示在消息框中。我应该输入工资,然后它会进行一些计算并将小计值存储到数组中。我声明了一个名为 decArray 和 intIndex 的数组和索引。谁能告诉我我错过了什么或做错了什么?先感谢您!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InvoiceTotal
{

    public partial class frmInvoiceTotal : Form
    {
        public frmInvoiceTotal()
        {
            InitializeComponent();
        }

        // TODO: declare class variables for array and list here
        decimal[] decArray = new decimal[5];
        int intIndex = 0;

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtSubtotal.Text == "")
                {
                    MessageBox.Show(
                        "Subtotal is a required field.", "Entry Error");
                }
                else
                {
                    decimal subtotal = Decimal.Parse(txtSubtotal.Text);
                    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();

                        for (intIndex = 0; intIndex <= decArray.Length - 1; intIndex++)
                        {
                             DecArray[intIndex] = InvoiceTotal
                        }

                    }
                    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)
        {
            // TODO: add code that displays dialog boxes here
            string totalstring = "";
            foreach (decimal value in decArray)
            {
                totalstring += value + "\n";
                MessageBox.Show(totalstring + "\n", "Order Totals");
            }
            this.Close();
        }
    }
}
4

2 回答 2

4

您永远不会分配给您的decArray(例如decArray[0] = n;

于 2013-02-07T17:58:40.093 回答
1

如果您添加一个计数变量来增加您的数组计数,那么您可以添加不止一个数量。您还希望允许数组根据需要调整大小。

decimal[] decArray = new decimal[5];
int _indexCount = 0;

private void btnCalculate_Click(object sender, EventArgs e)
{
    ...
    if (decArray.Count() == _indexCount)
    {
        var elementHolder = decArray;
        decArray = new T[(decArray.Length + 1) * 2];

        for (int i = 0; i < elementHolder.Length; i++)
        {
            decArray[i] = elementHolder[i];
        }
    }

    decArray[_indexCount] = invoiceTotal;
    _indexCount++;

}

像这样的东西应该工作。

编辑:你收到这么多消息的原因是因为MessageBox.Show()在 foreach 循环中只是把它放在循环之外,你只会看到一个。

    private void btnExit_Click(object sender, EventArgs e)
    {
        // TODO: add code that displays dialog boxes here
        string totalstring = "";
        foreach (decimal value in decArray)
        {
            totalstring += value + "\n";
        }
        MessageBox.Show(totalstring + "\n", "Order Totals");
        this.Close();
    }
于 2013-02-07T18:59:58.893 回答