0
    public mainForm()
    {
        InitializeComponent();
    }
    string[,] summary = new string[10, 4];


    private void exitButton_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void calculateButton_Click(object sender, EventArgs e)
    {
            decimal monthlyInvestment =
                    Convert.ToDecimal (monthlyInvestmentTextBox.Text);
            decimal yearlyInterestRate =
                    Convert.ToDecimal (interestRateTextBox.Text);
            int years = 
                Convert.ToInt32(yearsTextBox.Text);

            int months = years * 12;
            decimal monthlyInterestRate = yearlyInterestRate / 12 / 100;

            decimal futureValue = 0;
            for (int i = 0; i < months; i++)
            {
                futureValue = (futureValue + monthlyInvestment)
                        * (1 + monthlyInterestRate);
            }
            futureValueTextBox.Text = futureValue.ToString("c");
            monthlyInvestmentTextBox.Focus();
    }

该程序根据利率和年份计算投资的未来价值。一旦用户点击计算,我希望程序在一个 10x4 的数组中存储多达 10 个计算。4 投资、利率、年限和未来价值。单击退出按钮后,我希望显示一个包含 10 个先前计算的消息框。我该怎么做呢?谢谢。

4

2 回答 2

0
public class FutureInvestments
{
public decimal monthlyInvestment {get;set;}
public decimal yearlyInterestRate {get;set;}
public decimal futureValue {get;set;}
public decimal monthlyInterestRate {get;set;}
public decimal monthlyInvestment {get;set;}

  public string Calculate()
  {
    int months = years * 12;
    monthlyInterestRate = yearlyInterestRate / 12 / 100;
    for (int i = 0; i < months; i++)
    {
     futureValue = (futureValue + monthlyInvestment)  * (1 + monthlyInterestRate);
    }
    return futureValue.ToString("c");  
  }  
}

public mainForm()
{
    InitializeComponent();
}

List<FutureInvestments> summary = new List<FutureInvestments>();

private void calculateButton_Click(object sender, EventArgs e)
{
   //Instantiate class - passing in ReadOnly parameters
   var futureInv = new FutureInvestment() {monthlyInvestment = Convert.ToDecimal (monthlyInvestmentTextBox.Text), monthlyInvestment = Convert.ToDecimal(monthlyInvestmentTextBox.Text), yearlyInterestRate = Convert.ToDecimal (interestRateTextBox.Text) years =  Convert.ToInt32(yearsTextBox.Text));

   futureInv.Calculate();

   //Store the calculation for showing user when application closes
   summary.Add(futureInv);
 }

private void exitButton_Click(object sender, EventArgs e)
{
    foreach(var item in summary)
    {
        MessageBox.Show("Future value is: " + item.FutureValue.ToString());
    }
    this.Close();
}
于 2012-12-04T04:37:00.650 回答
0

@Jeremy Thompson 是对的;让您的课程专注于他们所做的事情 - 表单用于 UI,Investment 课程处理其他所有事情。

    public partial class mainForm : Form
{
    private Stack<Investment> Investments;

    public mainForm()
    {
        InitializeComponent();
        Investments = new Stack<Investment>();
    }

    private void calculateButton_Click(object sender, EventArgs e)
    {
        Investment thisInvestment = new Investment(Convert.ToDecimal(monthlyInvestmentTextBox.Text),
                                                   Convert.ToDecimal(interestRateTextBox.Text),
                                                   Convert.ToInt32(yearsTextBox.Text));
        Investments.Push(thisInvestment);
        futureValueTextBox.Text = thisInvestment.futureValue.ToString("c");
        monthlyInvestmentTextBox.Focus();

    }

    private void exitButton_Click(object sender, EventArgs e)
    {
        int count = Math.Min(10, Investments.Count);
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= count; i++)
        {
            sb.AppendLine(Investments.Pop().ToString());
        }
        MessageBox.Show(sb.ToString());
    }
}

public class Investment
{
    public Investment(decimal monthlyInvestment, decimal yearlyInterestRate, int years)
    {
        this.monthlyInvestment = monthlyInvestment;
        this.yearlyInterestRate = yearlyInterestRate;
        this.years = years;
    }

    public decimal monthlyInvestment { get; set; }
    public decimal yearlyInterestRate { get; set; }
    public decimal monthlyInterestRate
    {
        get
        {
            return yearlyInterestRate / 12 / 100;
        }
    }
    public int years { get; set; }
    public int months
    {
        get
        {
            return years * 12;
        }
    }
    public decimal futureValue
    {
        get
        {
            decimal retVal = 0;
            for (int i = 0; i < months; i++)
            {
                retVal = (futureValue + monthlyInvestment)
                        * (1 + monthlyInterestRate);
            }
            return retVal;
        }
    }

    public override string ToString()
    {
        return string.Format("Investment of {0:c} for {1:n} years at {2:p} pa yields {3:c}", monthlyInvestment,years,monthlyInterestRate,futureValue);
    }
}
于 2012-12-04T05:11:18.530 回答