0

我想创建一个循环,该循环具有给定的初始资本和固定的年利率(利息),该循环打印出当年的当前资本,包括资本翻倍的那一年。

例如初始资本为3000,利息为12。

//3000*(1 + 12 / 100) = 3360

output
year 1 = 3360
year 2 = 3763.2
year 3 = 4214.78
year 4 = 4720.55
year 5 = 5287,02
year 6 = 5921,46
//end (when the initial capital(3000) has doubled (6000))

我需要帮助的是创建一个循环,该循环将计算并显示输出,直到资本通过 for 循环或 while 循环翻倍。输出不必像示例但类似。

这是我到目前为止创建的代码:

double initialcapital = 0;
double interest = 0;
int year = 0;
double capital = 0;

Console.Write("Initial capital: ");
initialcapital = int.Parse(Console.ReadLine());

Console.Write("Interest: ");
interest = int.Parse(Console.ReadLine());

capital = initialcapital * (1 + interest / 100);
year = year + 1
Console.Writeline(capital);
4

2 回答 2

1
 void displayCapital(double initalcapital, double interest){
     int year = 1;
     double capital = initialcapital;

     while(initialcapital > capital / 2) {
         Console.Write("Initial capital: ");
         initialcapital = int.Parse(Console.ReadLine());

         Console.Write("Interest: ");
         interest = int.Parse(Console.ReadLine());

         capital = initialcapital * (1 + interest / 100);
         year = year + 1
         Console.Writeline(capital);
     }
}

只需调用 displayCapital(3000, 12); 你完成了

于 2013-02-18T10:52:50.747 回答
1

如果您想在金额翻倍之前找到确切的年数,您可以使用:

double exactYears = Math.Log(2) / Math.Log((100 + interest) / 100);

否则,以下内容将为您工作:

double initialCapital = 0;
double interest = 0;
int year = 0;
double capital = 0;

Console.Write("Initial capital: ");
initialCapital = int.Parse(Console.ReadLine());

Console.Write("Interest: ");
interest = int.Parse(Console.ReadLine());
capital = initialCapital;

while (capital < initialCapital * 2)
{
    capital = capital * (1 + interest / 100);
    year = year + 1;

    Console.WriteLine("Years: " + year);
    Console.WriteLine("Capital: " + capital);
}

Console.WriteLine("Years: " + year);
Console.WriteLine("Capital: " + capital);

double exactYears = Math.Log(2) / Math.Log((100 + interest) / 100);
Console.WriteLine(string.Format("Capital doubled in exactly: {0:0.000} years", exactYears));
于 2013-02-18T11:07:13.783 回答