为了账号安全,请及时绑定邮箱和手机立即绑定

贷款 C# 控制台应用程序,利息总额不是从零开始的

贷款 C# 控制台应用程序,利息总额不是从零开始的

C#
Smart猫小萌 2021-10-09 09:30:16
我已经按照它应该的方式工作了,除了第一行的输出显示已经汇总的利息支付的运行总额,而不是与第一笔利息金额相同。因此,如果第一个月的利息为 1.05,则第一个月的总利息应为 1.05。第二个月将是 1.05 + 新的利息金额。现在它显示了上面的例子,2.10 作为第一个月的总数。我的逻辑哪里出错了?using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Loan_Program{    class Loan    {        //declare variables        private double LoanAmount, InterestRate;        private int LoanLength;        //Constructor of Loan class that takes amount, rate and years        public Loan(double amount, double rate, int years)        {            this.LoanAmount = amount;            this.InterestRate = (rate / 100.0) / 12.0;            this.LoanLength = years;        }        //returns the monnthly payment        public double GetMonthlyPayment()        {            int months = LoanLength * 12;            return (LoanAmount * InterestRate * Math.Pow(1 + InterestRate, months)) / (Math.Pow(1 + InterestRate, months) - 1);        }        //Calculates totl interterest paid and doubles it, then returns the amount        public double TotalInterestPaid(double number1,double number2)        {            double TotalInterest = number1+number2;            return  TotalInterest;        }        //prints the amortization of Loan        public void LoanTable()        {            double monthlyPayment = GetMonthlyPayment();//calculates monthly payment            double principalPaid = 0;            double newBalance = 0;            double interestPaid = 0;            double principal = LoanAmount;            double totalinterest = 0;            //nonth, payment amount, principal paid, interest paid, total interest paid, balance            Console.WriteLine("{0,10}{1,10}{2,10}{3,10}{4,10}{5,10}", "Payment Number", "Payment Amt", "Interest Paid", "Principal paid","Balance Due","Total Interest Paid");            }        }    }}
查看完整描述

3 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

totalinterest在interestPaid计算新之前增加。


    //prints the amortization of Loan

    public void LoanTable()

    {

        double monthlyPayment = GetMonthlyPayment();//calculates monthly payment

        double principalPaid = 0;

        double newBalance = 0;

        double interestPaid = 0;

        double principal = LoanAmount;

        double totalinterest = 0;

        //nonth, payment amount, principal paid, interest paid, total interest paid, balance

        Console.WriteLine("{0,10}{1,10}{2,10}{3,10}{4,10}{5,10}", "Payment Number", "Payment Amt", "Interest Paid", "Principal paid", "Balance Due", "Total Interest Paid");

        for (int month = 1; month <= LoanLength * 12; month++)

        {

            // Compute amount paid and new balance for each payment period

            totalinterest += interestPaid;

            interestPaid = principal * InterestRate;

            principalPaid = monthlyPayment - interestPaid;

            newBalance = principal - principalPaid;

            // Output the data item              

            Console.WriteLine("{0,-10}{1,10:N2}{2,10:N2}{3,10:N2}{4,10:N2}{5,10:N2}",

                month, monthlyPayment, interestPaid, principalPaid, newBalance, TotalInterestPaid(totalinterest, interestPaid));

            // Update the balance

            principal = newBalance;

        }

    }

这是正确的吗?


Enter loan amount

100

Enter annual interest rate

10

Enter number of years

1

Payment NumberPayment AmtInterest PaidPrincipal paidBalance DueTotal Interest Paid

1               8.79      0.83      7.96     92.04      0.83

2               8.79      0.77      8.02     84.02      1.60

3               8.79      0.70      8.09     75.93      2.30

4               8.79      0.63      8.16     67.77      2.93

5               8.79      0.56      8.23     59.54      3.50

6               8.79      0.50      8.30     51.24      3.99

7               8.79      0.43      8.36     42.88      4.42

8               8.79      0.36      8.43     34.45      4.78

9               8.79      0.29      8.50     25.94      5.07

10              8.79      0.22      8.58     17.37      5.28

11              8.79      0.14      8.65      8.72      5.43

12              8.79      0.07      8.72      0.00      5.50


查看完整回答
反对 回复 2021-10-09
?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

    static void Main(string[] args)

    {

        float Amount, Year;

        float Rate;

        double total = 0;

        Console.Write("Enter Amount Per Year:");

        Amount = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter Rate :");

        Rate = Convert.ToSingle(Console.ReadLine());

        Console.Write("Enter Time :");

        Year = Convert.ToInt32(Console.ReadLine());

        for (int i = 1; i <= Year; i++)

        {

            var current = Amount * (Rate / 100) * i;

            total += current;

        }

        Console.WriteLine("Simple Interest is :{0}", total);

        Console.ReadKey();

    }

The using another method i can find Total Interest


查看完整回答
反对 回复 2021-10-09
?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

只是为了帮助使用decimal而不是double这里是您需要的代码:


void Main()

{

    //declare variables

    decimal amount;

    decimal rate;

    int years;

    //prompt loan amount

    Console.WriteLine("Enter loan amount");

    amount = Convert.ToDecimal(Console.ReadLine());//accepts console input and assigne to variable

                                                   //prompt for rate

    Console.WriteLine("Enter annual interest rate");

    rate = Convert.ToDecimal(Console.ReadLine());//accepts console input and assigne to variable

                                                 //prompt for monhts

    Console.WriteLine("Enter number of years");

    years = Convert.ToInt32(Console.ReadLine());//accepts console input and assigne to variable


    Loan loan = new Loan(amount, rate, years);//create  new instance, send values to the class


    loan.LoanTable();

}


class Loan

{

    //declare variables

    private decimal LoanAmount, InterestRate;

    private int LoanLength;


    //Constructor of Loan class that takes amount, rate and years

    public Loan(decimal amount, decimal rate, int years)

    {

        this.LoanAmount = amount;

        this.InterestRate = DecPow(1m + rate / 100m, 1m / 12m) - 1m;

        this.InterestRate.Dump();

        this.LoanLength = years;

    }

    //returns the monnthly payment

    public decimal GetMonthlyPayment()

    {

        int months = LoanLength * 12;

        return (LoanAmount * InterestRate * DecPow(1m + InterestRate, months)) / (DecPow(1m + InterestRate, months) - 1m);

    }

    //Calculates totl interterest paid and doubles it, then returns the amount

    public decimal TotalInterestPaid(decimal number1, decimal number2)

    {

        decimal TotalInterest = number1 + number2;


        return TotalInterest;

    }


    //prints the amortization of Loan

    public void LoanTable()

    {

        decimal monthlyPayment = GetMonthlyPayment();//calculates monthly payment

        decimal principalPaid = 0m;

        decimal newBalance = 0m;

        decimal interestPaid = 0m;

        decimal principal = LoanAmount;

        decimal totalinterest = 0m;

        //nonth, payment amount, principal paid, interest paid, total interest paid, balance

        Console.WriteLine("{0,10}{1,10}{2,10}{3,10}{4,10}{5,10}", "Payment Number", "Payment Amt", "Interest Paid", "Principal paid", "Balance Due", "Total Interest Paid");

        for (int month = 1; month <= LoanLength * 12; month++)

        {

            // Compute amount paid and new balance for each payment period

            totalinterest += interestPaid;

            interestPaid = principal * InterestRate;

            principalPaid = monthlyPayment - interestPaid;

            newBalance = principal - principalPaid;

            // Output the data item              

            Console.WriteLine("{0,-10}{1,10:N2}{2,10:N2}{3,10:N2}{4,10:N2}{5,10:N2}",

                month, monthlyPayment, interestPaid, principalPaid, newBalance, TotalInterestPaid(totalinterest, interestPaid));

            // Update the balance

            principal = newBalance;

        }

    }


    private decimal DecPow(decimal x, decimal y) => (decimal)System.Math.Pow((double)x, (double)y);

    private decimal DecPow(decimal x, int p)

    {

        if (p == 0) return 1m;

        decimal power = 1m;

        int q = Math.Abs(p);

        for (int i = 1; i <= q; i++) power *= x;

        if (p == q)

            return power;

        return 1m / power;

    }

}

特别注意DecPow方法。


查看完整回答
反对 回复 2021-10-09
  • 3 回答
  • 0 关注
  • 210 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信