我如何编写一个c#循环来计算未来5年Tution每年2%的增长

我如何编写一个c#循环来计算未来5年Tution每年2%的增长,c#,c#-4.0,C#,C# 4.0,全日制学生的学费是每年12000美元。已经宣布未来5年的学费将以每年2%的速度增长。我如何编写一个c#循环来计算未来5年每年2%的学费增长 到目前为止,我的代码是 private void button1_Click(object sender, EventArgs e) { string display; double initialfee = 12000.00; double increase,newfee;

全日制学生的学费是每年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);
private void按钮1\u单击(对象发送者,事件参数e)
{
字符串显示;
双倍初始费用=12000.00;
双倍增长,新费用;
倍率=0.02;
listBox1.Items.Clear();

对于(int year=1;year你不需要乘以year。 试试这个

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);

}

你不需要按年份乘以。 试试这个

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);

}

这里有一个计算方法

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++)
    {
       newfee = initialfee + (initialfee * 2/100 * year);
       display = "year " + year.ToString() + ": " + "  Amount " + "$" + newfee;
    }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
字符串显示;
双倍初始费用=12000.00;
双倍增长,新费用;
倍率=0.02;
listBox1.Items.Clear();

对于(int year=1;year,这里有一个要计算的解决方案

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++)
    {
       newfee = initialfee + (initialfee * 2/100 * year);
       display = "year " + year.ToString() + ": " + "  Amount " + "$" + newfee;
    }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
字符串显示;
双倍初始费用=12000.00;
双倍增长,新费用;
倍率=0.02;
listBox1.Items.Clear();
对于(int year=1;yearLinq解决方案:

using System.Linq;

...

// Please, see separation:

// Model: here we define condition

// initial conditions  
double initialfee = 12000.00;
double rate = 0.02;
int years = 5;

// Business logic: here we obtain data

// please, notice formatting (here is C# 6.0 string interpolation) 
// which is easier to read and maintain then contructing string from chunks added.
var data = Enumerable
  .Range(1, years)
  .Select(year => $"Year {year}: Amount: {Math.Pow(1.0 + rate, year - 1) * initialfee:f2}")
  .ToArray();

// Representation: UI representation

// Add items in one go to prevent redrawing and blinking
listBox1.Items.AddRange(data);
结果是

Year 1: Amount: 12000.00
Year 2: Amount: 12240.00
Year 3: Amount: 12484.80
Year 4: Amount: 12734.50
Year 5: Amount: 12989.19
我知道,您正在寻找循环解决方案,但在现实世界中,我们通常更喜欢使用数据查询。

Linq解决方案:

using System.Linq;

...

// Please, see separation:

// Model: here we define condition

// initial conditions  
double initialfee = 12000.00;
double rate = 0.02;
int years = 5;

// Business logic: here we obtain data

// please, notice formatting (here is C# 6.0 string interpolation) 
// which is easier to read and maintain then contructing string from chunks added.
var data = Enumerable
  .Range(1, years)
  .Select(year => $"Year {year}: Amount: {Math.Pow(1.0 + rate, year - 1) * initialfee:f2}")
  .ToArray();

// Representation: UI representation

// Add items in one go to prevent redrawing and blinking
listBox1.Items.AddRange(data);
结果是

Year 1: Amount: 12000.00
Year 2: Amount: 12240.00
Year 3: Amount: 12484.80
Year 4: Amount: 12734.50
Year 5: Amount: 12989.19

我知道,您正在寻找循环解决方案,但在现实世界中,我们通常更喜欢处理数据查询。

是复合增长还是非复合增长?即,增长总是“初始费用”的2%,还是每年都是“新费用”的2%换句话说,分母是什么?使用
newfee
来计算
增加量
,而不是总是再次计算
initalfee
。但是一定要正确初始化
newfee
。还有,你为什么还要用年份乘以?这不是一个C问题,这是一个基本的数学问题。增加2%就是一个乘法到1.02,效果很好。谢谢。增加的是复合物还是非复合物?例如,增加的总是“初始费用”的2%,还是每年增加的是“新费用”的2%换句话说,分母是什么?使用
newfee
来计算
增加量
,而不是总是再次计算
initalfee
。但是一定要正确初始化
newfee
。还有,你为什么还要用年份乘以?这不是一个C问题,这是一个基本的数学问题。增加2%就是一个乘法到1点02分,效果很好谢谢