全日制学生的学费为每年12000美元。已经宣布未来5年的学费将以每年2%的速度增长。我如何编写C#循环来计算未来5年的学费以每年2%的速度增长

到目前为止,我的代码是..

private void button1_Click(object sender, EventArgs e)
    {
        string display;
        double initialfee = 12000.00;
        double increase,newfee;
        double rate = 0.02;
        listBox1.Items.Clear();

        for (int year = 1; year <= 5; year++)
        {
            increase = initialfee * rate * year;
            newfee = increase + initialfee;


            display = "year " + year.ToString() + ": " + "  Amount " + "$" + newfee;

            listBox1.Items.Add(display);

最佳答案

您不需要乘以一年。
尝试这个

string display;
double initialfee = 12000.00;
double increase=0,newfee;
double rate = 0.02;


for (int year = 1; year <= 5; year++)
{
    if(year>1)
    {
        increase = initialfee * rate;
    }

    initialfee = increase + initialfee;


    display = "year " + year.ToString() + ": " + "  Amount " + "$" + initialfee;
    Console.WriteLine(display);

}


输出:

year 1:   Amount $12000
year 2:   Amount $12240
year 3:   Amount $12484.8
year 4:   Amount $12734.496
year 5:   Amount $12989.18592

关于c# - 我如何编写C#循环来计算 future 5年每年的学费增长2%,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42273808/

10-09 09:04