我如何使用for循环将变量分配给数组? [英] how do i assign variables to an array using a for loop?

查看:95
本文介绍了我如何使用for循环将变量分配给数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im试图分配一个变量(invoiceTotal),该变量是一个方程式的结果,并将每个结果最多5个放入可以容纳5个值的数组中.当用户单击计算"按钮时,我希望将生成的invoiceTotal放入第一个索引中的总计数组中.当用户输入其他信息然后再次单击计算"时,我希望结果进入第二个索引,依此类推.然后,当用户单击退出按钮时,我想在对话框中显示数组索引.我用来存储变量的for循环用斜体表示.发生的事情是,每当我单击计算"时,它将invoiceTotal存储到所有五个数组索引中.到目前为止,这是我的代码...

im trying to assign a variable (invoiceTotal) which is a result of an equation and placing each result up to five into an array that can hold 5 values. When the user clicks on the calculate button I want the resulting invoiceTotal to be placed into the array totals in the first index. When the user enters different info then clicks calculate again, I want the result to go into the second index and so on. Then I want to display the array indexes in a dialog box when the user clicks the exit button. The for loop I am using to store the variables is italicized. What is happening is it stores the invoiceTotal into all five of the array indexes every time I click calculate. Heres my code so far...

public partial class frmInvoiceTotal : Form
    {
        public frmInvoiceTotal()
        {
            InitializeComponent();
        }
        
        decimal[] totals = new decimal[5];
        string totalsString = "";
        
        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);
                        for (int i = 0; i < totals.Length; i++)
                            totals[i] = invoiceTotal;
                        txtDiscountPercent.Text = discountPercent.ToString("p1");
                        txtDiscountAmount.Text = discountAmount.ToString();
                        txtTotal.Text = invoiceTotal.ToString();

                    }
                    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)
        {
            

            foreach (decimal total in totals)
                totalsString += total + "\n";
            MessageBox.Show(totalsString + "\n" , "Order Totals - Array");
            this.Close();
        }
    }



这是家庭作业,Im使用的书没有说明如何执行此操作,因此我必须使用for循环.我非常沮丧,因为我觉得我应该知道这一点,但我无法弄清楚.另外,我的教练现在已经10天没有回复我的任何电子邮件了.



This is homework, the book Im using does not state how to do this and I have to use a for loop. I am so frustrated because I feel I should know this but I cant figure it out. Also, my instructor hasnt responded to any of my emails for 10 days now.

推荐答案

如果这不是家庭作业,那么最好使用键入列表并添加到其中.否则,您需要跟踪上次存储的项目的索引,以便知道下一个值的存储位置.
If this is not homework, then you''re better off using a typed List and just adding to it. Otherwise, you need to keep track of the index of the item you last stored, so you can know where to store the next value.


正如克里斯蒂安建议的那样,您可以跟踪最后一个项目的索引.使用的索引,例如,您可以从
更改
As Christian suggested, you may keep track of the last used index, for instance, you may change from
Optimist Prime写道:
Optimist Prime wrote:

decimal []总数=新的十进制[5];
字符串totalsString =";

decimal[] totals = new decimal[5];
string totalsString = "";




to

decimal[] totals = new decimal[5];
string totalsString = "";
int lastIndex = 0



和替换



and the replace

Optimist Prime写道:
Optimist Prime wrote:

for(int i = 0; i totals [i] = invoiceTotal;

for (int i = 0; i < totals.Length; i++)
totals[i] = invoiceTotal;






with

totals[lastIndex] = invoiceTotal;
lastIndex++;
if (lastIndex == 5) lastIndex = 0;


:)


这篇关于我如何使用for循环将变量分配给数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆