创建一个使用数组的方法

Creating a method that uses an Array

我正在创建一个表格来存储每月的销售额。用户将点击提交按钮,这些金额将存储在一个数组中。

然后将单击一个总计按钮,并调用一个方法来计算数组中的总金额。

到目前为止,我有两个问题(据我所知):首先,我仍然不确定调用我的方法的正确格式。现在 VS 不喜欢我的变量 "x"。我不知道该用什么,所以我就放了一个在那里。其次,对我的数组求和的代码也不正确。 VS 不喜欢我的 "index" 变量。

我已尝试记录代码中的问题区域。很抱歉冗长的 post 和无知的问题。

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

        private void submitButton_Click(object sender, EventArgs e)
        {
            //creating an array
            const int SIZE = 12;
            decimal[] amountsArray = new decimal[SIZE];
            amountsArray[0] = decimal.Parse(JaninputBox.Text);
            amountsArray[1] = decimal.Parse(FebinputBox.Text);
            amountsArray[2] = decimal.Parse(MarinputBox.Text);
            amountsArray[3] = decimal.Parse(AprinputBox.Text);
            amountsArray[4] = decimal.Parse(MayinputBox.Text);
            amountsArray[5] = decimal.Parse(JuninputBox.Text);
            amountsArray[6] = decimal.Parse(JulinputBox.Text);
            amountsArray[7] = decimal.Parse(AuginputBox.Text);
            amountsArray[8] = decimal.Parse(SepinputBox.Text);
            amountsArray[9] = decimal.Parse(OctinputBox.Text);
            amountsArray[10] = decimal.Parse(NovinputBox.Text);
            amountsArray[11] = decimal.Parse(DecinputBox.Text);

            TotsalesButton.Enabled = true;

        }

        private void TotsalesButton_Click(object sender, EventArgs e)
        {
            // calling the method to total amounts from array
           TotalIt(decimal x); **//<-- VS doesn't like this**

            AvgsalesButton.Enabled = true; //enabling avg sales button

        }

        private void TotalIt(decimal[] amountsArray)
        {
            decimal sum;  // variable to hold total 

            // method for totaling array data
            for (decimal index = 0; index < amountsArray.Length; index++)
            {
                sum += amountsArray[index]; **//<-- Doesn't like "index"** here.
            }

        }

    }

}

几件事。除非你在别处处理输入,否则你应该使用 TryParse,这样如果有人没有输入可以解析为小数的值(即 1.a03),就不会导致异常。 所以

decimal parsedDecimal;
amountsArray[0] = decimal.TryParse(JaninputBox.Text, out parsedDecimal) ? parsedDecimal : 0.00;

该部分。

TotsalesButton_Click 正在调用一个具有十进制数组参数的方法 (TotalIt),但是您使用的是单个小数 'x',它甚至在任何地方都不是变量。

最简单的方法是在顶部声明 amountsArray 一个 public 变量。

public partial class Form1 : Form
{
    private decimal[] _amountsArray;
    public Form1()
    {
        InitializeComponent();
    }
 }

然后在您的 TotsalesButton 中您不需要传递参数,只需检查该数组是否为 null(不会为 null,因为必须调用 submitButton 并在其中初始化数组)

private void TotsalesButton_Click(object sender, EventArgs e)
{
     var sum = TotalIt();
     if (sum > 0.0) AvgsalesButton.Enabled = true; 
}

总算可以这样调用了

private decimal TotalIt()
{
    if (_amountsArray == null) return;
    decimal sum;  // variable to hold total 

    //since you're iterating through an array, an index is always an int
    for (int index = 0; index < amountsArray.Length; index++)
    {
         sum += amountsArray[index];
    }
    return sum;
}

您现在遇到的根本问题与 scope 有关。您的某些变量的范围仅限于单个方法,但需要作为整个 class:

的成员可用
public partial class Form1 : Form
{
   //creating an array
    const int SIZE = 12;
    decimal[] amountsArray = new decimal[SIZE];

    public Form1()
    {
        InitializeComponent();
    }

   //...

}

然后就可以调用TotalIt()方法了。此外,根本不需要将数组传递给方法,因为它已经作为 class:

的成员可用
    private void TotsalesButton_Click(object sender, EventArgs e)
    {
       TotalIt();
       AvgsalesButton.Enabled = true; //enabling avg sales button
    }

下面是 TotalIt() 方法的样子。同样,由于该方法已经可以使用数组,因此您无需声明为参数:

    private void TotalIt()
    {
        amountsArray.Sum(); 
    }

我不确定您打算如何处理该结果...它不在您的原始代码中。但是,您可以看到 Sum() 方法使整个事情变得更容易。但是,您的原始代码几乎是正确的。您所要做的就是将 index 变量声明为 int 而不是 decimal。您很快就会发现,在使用代码时,使用正确的数据类型非常重要。