簡體   English   中英

為什么單擊退出時我的C#代碼在消息框中出現零?

[英]Why is my C# code coming up with zero's in message box upon clicking exit?

我試圖讓我的消息框顯示存儲在我的數組中的發票小計...使用foreach方法在消息框中顯示其中的5個。 我應該輸入工資,然后進行一些計算並將小計的值存儲到數組中。 我聲明了一個數組和索引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();
        }
    }
}

您永遠不會分配給decArray (例如decArray[0] = n;

如果添加一個count變量來增加數組的數量,則可以添加多個。 您還希望允許根據需要調整數組的大小。

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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM