0

我烦了。我是一个正在做作业的新手,我似乎无法弄清楚为什么我的消息框只显示五次相同的数字(计算的最后一个小计数字)。我不明白如何使用 for 循环将我的值存储到数组中而不重置它。有人可以帮忙吗?先感谢您。

 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

0

Since its a homework, I can only give you a clue, your loop is currently taking only one invoiceTotal, You don't have to use loop at all, When your control enter btnCalculate_Click event then after calculating invoiceTotal, you can put it to the decArray using your class level intIndex. Increment the intIndex and before entering the value in decArray you must check if the intIndex is less then array length. Another idea would be to use List<decimal> instead of array if you are expecting unknown numbers of input.

于 2013-02-08T05:02:58.447 回答
0

Your for...loop sets every index of the array to the total.

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

Ask yourself: what is its purpose? You could safely change it to this:

decArray[intIndex++] = invoiceTotal;

I will leave it up to you to determine how that changes it (since it's homework).

于 2013-02-08T05:10:10.730 回答