我需要程序在每年之后显示结果

时间:2016-10-26 17:59:50

标签: c#

它只显示最终结果。

例如 - " 3年后的金额 - 1100欧元"

但我需要它来显示第1年和第2年的结果。

int i; 
Double sum;

Console.Write("Starting amount: ");  
Double start = Convert.ToDouble(Console.ReadLine());

Console.Write("Duration of deposit: ");  
int year = Convert.ToInt32(Console.ReadLine());

Console.Write("Annual interest rate: ");  
Double c = Convert.ToDouble(Console.ReadLine());

for (i = 1; i < g; i++) ;
{
    Console.Write("Amount after " + i + ". year - ");
    sum = a * (1 + i * c);
    Console.WriteLine(sum + " eur");
} 

1 个答案:

答案 0 :(得分:1)

;循环后删除for

// No `;` at the end of the line of the loop
for(i = 1; i < 10; i++)
{
    // Iterate until condition is met
}

你甚至收到了警告: enter image description here

MSDN说for loop的身体:

  

循环体由一个语句,一个空语句或一个语句块组成,你可以通过在括号中包含零个或多个语句来创建它们。

如果在;循环行的末尾放置for,则会插入empty语句。这就像写作:

for(i = 1; i < 10; i++) ; // The empty scope is iterated until condition is met
for(i = 1; i < 10; i++) {} // Same as above

{
    //Now after loop ends the scope is executed and actually the {} is not needed  
}