【问题标题】:Investment balance in c#?c#中的投资余额?
【发布时间】: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);

我怎样才能使表格输出?

【问题讨论】:

  • 查看balanceyearsin your debugger的值。您只需一遍又一遍地重新计算相同的值,而无需存储或打印任何内容。
  • 你在公式中使用了years,而你应该使用i(或者可能是i + 1)。
  • 在处理货币/货币时,您几乎总是想使用decimal。还有principal不是'principle'
  • 这里提示一下:如果要打印表格,则需要为每一年的投资打印一行。让我强调一下,你必须每年打印一些东西。 :)

标签: c#


【解决方案1】:
        Console.Write("Enter the amount deposited: ");
        double principle = 0; 
        principle = double.Parse(Console.ReadLine());

        Console.Write("Enter number of years: ");
        int years = 0;
        years = int.Parse(Console.ReadLine());

        Console.Write("Enter the interest rate as a percentage of 1.0: ");

        double interest;
        interest = double.Parse(Console.ReadLine());

        double balance = 0;

        Console.WriteLine("Years \t Balance", years);
        for (int i = 0; i < years; i++)
        {
            balance = principle * Math.Pow((1 + interest), i);
            Console.WriteLine("{0} \t {1}", i, balance);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2022-08-16
    相关资源
    最近更新 更多