【发布时间】:2019-06-18 00:14:41
【问题描述】:
我在使用 for 循环时遇到了很多麻烦。这真的让我很困惑,这是我的代码:
Console.WriteLine("Enter the amount deposited: ");
double principle = 0; //amount deposited
principle = double.Parse(Console.ReadLine());
Console.WriteLine("Enter number of years: ");
int years = 0;
years = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the interest rate as a percentage of
1.0: ");
double interest;
interest = double.Parse(Console.ReadLine());
double balance = 0;
for (int i = 0; i < years; i++)
{
balance = principle * Math.Pow((1 + interest),years);
}
Console.WriteLine("Years {0}", years);
Console.WriteLine("Balance {0}", balance);
我怎样才能使表格输出?
【问题讨论】:
-
查看
balance和yearsin your debugger的值。您只需一遍又一遍地重新计算相同的值,而无需存储或打印任何内容。 -
你在公式中使用了
years,而你应该使用i(或者可能是i + 1)。 -
在处理货币/货币时,您几乎总是想使用
decimal。还有principal不是'principle' -
这里提示一下:如果要打印表格,则需要为每一年的投资打印一行。让我强调一下,你必须每年打印一些东西。 :)
标签: c#