C#中的投资余额?

时间:2019-06-18 00:14:41

标签: c#

在使用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);

如何制作表格以便输出?

1 个答案:

答案 0 :(得分:0)

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