【问题标题】:Displaying previous values显示以前的值
【发布时间】:2012-12-04 03:58:46
【问题描述】:
public partial class mainForm : Form
{
    public mainForm()
    {
        InitializeComponent();
    }
    private void btnCalculate_Click(object sender, EventArgs e)
    {
        string customerType = txtCustomerType.Text;
        decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
        decimal discountPercent = .0m;

        switch (customerType)
        {
            case "R":
                if (subtotal < 100)
                    discountPercent = .0m;
                else if (subtotal >= 100 && subtotal < 250)
                    discountPercent = .1m;
                else if (subtotal >= 250 && subtotal < 500)
                    discountPercent = .25m;
                else if (subtotal >= 500)
                    discountPercent = .30m;
                break;
            case "C":
                discountPercent = .2m;
                break;
            case "T":
                if (subtotal < 500)
                    discountPercent = .4m;
                else if (subtotal >= 500)
                    discountPercent = .5m;
                break;
            default:
                discountPercent = .1m;
                break;
        }

            decimal discountAmount = subtotal * discountPercent;
            decimal invoiceTotal = subtotal - discountAmount;

            txtDiscountPercent.Text = discountPercent.ToString("p1");
            txtDiscountAmount.Text = discountAmount.ToString("c");
            txtTotal.Text = invoiceTotal.ToString("c");

            txtCustomerType.Focus();            
        }


    private void btnExit_Click(object sender, EventArgs e)
    {

        int i = 0;
        string summaryString = txtTotal.Text;
        for (i = 0; i < 5; i++)
            summaryString += Environment.NewLine;

        MessageBox.Show(summaryString, "Order Totals");

        this.Close();
    }
}

一旦用户点击计算按钮,它就会计算发票总额。我需要做的就是存储前 5 个总值,并让它们在新行上一个接一个地显示在消息框中。一旦用户单击退出按钮,该消息框就会显示出来。

【问题讨论】:

  • 您应该将此标记为家庭作业,并告诉我们具体给您带来的问题。我们不是一个“复制粘贴让这项工作”的社区。​​span>
  • @SimonWhitehead 作业标签已被弃用。

标签: c# arrays loops


【解决方案1】:

这样的事情可能会为您指明正确的方向:

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

    private List<string> _totals = new List<string>();

    private void btnCalculate_Click(object sender, EventArgs e)
    {
            string customerType = txtCustomerType.Text;
            decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
            decimal discountPercent = .0m;

            switch (customerType)
            {
                case "R":
                    if (subtotal < 100)
                        discountPercent = .0m;
                    else if (subtotal >= 100 && subtotal < 250)
                        discountPercent = .1m;
                    else if (subtotal >= 250 && subtotal < 500)
                        discountPercent = .25m;
                    else if (subtotal >= 500)
                        discountPercent = .30m;
                    break;
                case "C":
                    discountPercent = .2m;
                    break;
                case "T":
                    if (subtotal < 500)
                        discountPercent = .4m;
                    else if (subtotal >= 500)
                        discountPercent = .5m;
                    break;
                default:
                    discountPercent = .1m;
                    break;
            }

            decimal discountAmount = subtotal * discountPercent;
            decimal invoiceTotal = subtotal - discountAmount;

            txtDiscountPercent.Text = discountPercent.ToString("p1");
            txtDiscountAmount.Text = discountAmount.ToString("c");
            txtTotal.Text = invoiceTotal.ToString("c");
            _totals.Add(txtTotal.Text);

            txtCustomerType.Focus();
        }


        private void btnExit_Click(object sender, EventArgs e)
        {
            MessageBox.Show(string.Join(Environment.NewLine, _totals.ToArray()), "Order Totals");
            this.Close();
        }

}

【讨论】:

    【解决方案2】:

    试试这个。这只会为您提供最后五个总数:

    public partial class mainForm : Form
    {
    public mainForm()
    {
        InitializeComponent();
    }
    
    private List<string> allTotals = new List<string>();
    
    private void btnCalculate_Click(object sender, EventArgs e)
    {
            string customerType = txtCustomerType.Text;
            decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
            decimal discountPercent = .0m;
    
            switch (customerType)
            {
                case "R":
                    if (subtotal < 100)
                        discountPercent = .0m;
                    else if (subtotal >= 100 && subtotal < 250)
                        discountPercent = .1m;
                    else if (subtotal >= 250 && subtotal < 500)
                        discountPercent = .25m;
                    else if (subtotal >= 500)
                        discountPercent = .30m;
                    break;
                case "C":
                    discountPercent = .2m;
                    break;
                case "T":
                    if (subtotal < 500)
                        discountPercent = .4m;
                    else if (subtotal >= 500)
                        discountPercent = .5m;
                    break;
                default:
                    discountPercent = .1m;
                    break;
            }
    
            decimal discountAmount = subtotal * discountPercent;
            decimal invoiceTotal = subtotal - discountAmount;
    
            txtDiscountPercent.Text = discountPercent.ToString("p1");
            txtDiscountAmount.Text = discountAmount.ToString("c");
            txtTotal.Text = invoiceTotal.ToString("c");
            allTotals.Add(txtTotal.Text);
    
        if (allTotals.Count > 5) allTotals.RemoveAt(0);
    
            txtCustomerType.Focus();
        }
    
    
        private void btnExit_Click(object sender, EventArgs e)
        {
            MessageBox.Show(string.Join(Environment.NewLine, allTotals.ToArray()), "Order Totals");
            this.Close();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 2016-11-26
      • 2020-06-11
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多