C# 如何修改代码以将总计存储到decArray?

C# 如何修改代码以将总计存储到decArray?,c#,C#,我很沮丧。我是一个做家庭作业的新手,我似乎不明白为什么我的messagebox只显示了五次相同的数字(上次计算的小计数字)。我不明白如何使用for循环将我的值存储到数组中而不重置它。有人能帮忙吗?先谢谢你 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; usi

我很沮丧。我是一个做家庭作业的新手,我似乎不明白为什么我的messagebox只显示了五次相同的数字(上次计算的小计数字)。我不明白如何使用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();
    }
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间InvoiceTotal
{
公共部分类frmInvoiceTotal:表单
{
公共财政收入总额
{
初始化组件();
}
//TODO:声明数组的类变量并在此处列出
十进制[]十进制=新的十进制[5];
int intIndex=0;
私有void btnCalculate\u单击(对象发送者,事件参数e)
{
尝试
{
如果(txtSubtotal.Text==“”)
{
MessageBox.Show(
“小计是必填字段。”,“输入错误”);
}
其他的
{
decimal subtotal=decimal.Parse(txtSubtotal.Text);
如果(小计>0&&小计<10000)
{
小数折扣百分比=0米;
如果(小计>=500)
贴现率=20万;
否则如果(小计>=250&小计<500)
折扣百分比=0.15米;
否则如果(小计>=100&小计<250)
折扣率=100万美元;
小数折扣额=小计*折扣百分比;
十进制发票总额=小计-折扣金额;
折扣量=数学圆(折扣量,2);
invoiceTotal=Math.Round(invoiceTotal,2);
txtDiscountPercent.Text=discountPercent.ToString(“p1”);
Text=discountAmount.ToString();
txtTotal.Text=invoiceTotal.ToString();

对于(intIndex=0;intIndex因为这是一个家庭作业,我只能给你一个线索,你的循环目前只占用一个
invoiceTotal
,你根本不需要使用循环,当你的控件进入
btnCalculate\u单击
事件,然后在计算
invoiceTotal
之后,你可以使用你的clas将它放到
decArray
s级
intIndex
。增加
intIndex
,在
decArray
中输入值之前,您必须检查
intIndex
是否小于
数组长度
。另一种方法是,如果需要未知数量的输入,则使用
List
而不是数组。

您的
for…循环
将数组的每个索引设置为总索引

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

我会让你们来决定这是如何改变的(因为这是家庭作业)。

哇,你们太棒了!我现在明白了。谢谢哈比布和西蒙。
decArray[intIndex++] = invoiceTotal;