如何修改代码以将总计存储到decArray中

本文关键字:存储 decArray 何修改 修改 代码 | 更新日期: 2023-09-27 17:58:21

我很沮丧。我是一个做作业的新手,我似乎不明白为什么我的消息框只显示了五次相同的数字(最后一次计算的小计数字)。我不知道如何在不重置的情况下使用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();
    }

}}

如何修改代码以将总计存储到decArray中

由于这是一个家庭作业,我只能给你一个线索,你的循环目前只占用一个invoiceTotal,你根本不必使用循环,当你的控件进入btnCalculate_Click事件时,在计算完invoiceTotal后,你可以使用你的类级别intIndex将其放入decArray。递增intIndex,在decArray中输入值之前,必须检查intIndex是否小于array length。另一个想法是,如果您期望未知数量的输入,则使用List<decimal>而不是数组。

您的for...loop将数组的每个索引设置为总和。

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

问问自己:它的目的是什么?你可以安全地将其更改为:

decArray[intIndex++] = invoiceTotal;

我将由你来决定如何改变它(因为这是家庭作业)。