猿问

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

我已经按照它应该的方式工作了,除了第一行的输出显示已经汇总的利息支付的运行总额,而不是与第一笔利息金额相同。因此,如果第一个月的利息为 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");

            }

        }


    }

}


Smart猫小萌
浏览 211回答 3
3回答

ITMISS

totalinterest在interestPaid计算新之前增加。&nbsp; &nbsp; //prints the amortization of Loan&nbsp; &nbsp; public void LoanTable()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; double monthlyPayment = GetMonthlyPayment();//calculates monthly payment&nbsp; &nbsp; &nbsp; &nbsp; double principalPaid = 0;&nbsp; &nbsp; &nbsp; &nbsp; double newBalance = 0;&nbsp; &nbsp; &nbsp; &nbsp; double interestPaid = 0;&nbsp; &nbsp; &nbsp; &nbsp; double principal = LoanAmount;&nbsp; &nbsp; &nbsp; &nbsp; double totalinterest = 0;&nbsp; &nbsp; &nbsp; &nbsp; //nonth, payment amount, principal paid, interest paid, total interest paid, balance&nbsp; &nbsp; &nbsp; &nbsp; 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");&nbsp; &nbsp; &nbsp; &nbsp; for (int month = 1; month <= LoanLength * 12; month++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Compute amount paid and new balance for each payment period&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; totalinterest += interestPaid;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interestPaid = principal * InterestRate;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; principalPaid = monthlyPayment - interestPaid;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newBalance = principal - principalPaid;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Output the data item&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("{0,-10}{1,10:N2}{2,10:N2}{3,10:N2}{4,10:N2}{5,10:N2}",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; month, monthlyPayment, interestPaid, principalPaid, newBalance, TotalInterestPaid(totalinterest, interestPaid));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Update the balance&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; principal = newBalance;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }这是正确的吗?Enter loan amount100Enter annual interest rate10Enter number of years1Payment NumberPayment AmtInterest PaidPrincipal paidBalance DueTotal Interest Paid1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8.79&nbsp; &nbsp; &nbsp; 0.83&nbsp; &nbsp; &nbsp; 7.96&nbsp; &nbsp; &nbsp;92.04&nbsp; &nbsp; &nbsp; 0.832&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8.79&nbsp; &nbsp; &nbsp; 0.77&nbsp; &nbsp; &nbsp; 8.02&nbsp; &nbsp; &nbsp;84.02&nbsp; &nbsp; &nbsp; 1.603&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8.79&nbsp; &nbsp; &nbsp; 0.70&nbsp; &nbsp; &nbsp; 8.09&nbsp; &nbsp; &nbsp;75.93&nbsp; &nbsp; &nbsp; 2.304&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8.79&nbsp; &nbsp; &nbsp; 0.63&nbsp; &nbsp; &nbsp; 8.16&nbsp; &nbsp; &nbsp;67.77&nbsp; &nbsp; &nbsp; 2.935&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8.79&nbsp; &nbsp; &nbsp; 0.56&nbsp; &nbsp; &nbsp; 8.23&nbsp; &nbsp; &nbsp;59.54&nbsp; &nbsp; &nbsp; 3.506&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8.79&nbsp; &nbsp; &nbsp; 0.50&nbsp; &nbsp; &nbsp; 8.30&nbsp; &nbsp; &nbsp;51.24&nbsp; &nbsp; &nbsp; 3.997&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8.79&nbsp; &nbsp; &nbsp; 0.43&nbsp; &nbsp; &nbsp; 8.36&nbsp; &nbsp; &nbsp;42.88&nbsp; &nbsp; &nbsp; 4.428&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8.79&nbsp; &nbsp; &nbsp; 0.36&nbsp; &nbsp; &nbsp; 8.43&nbsp; &nbsp; &nbsp;34.45&nbsp; &nbsp; &nbsp; 4.789&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8.79&nbsp; &nbsp; &nbsp; 0.29&nbsp; &nbsp; &nbsp; 8.50&nbsp; &nbsp; &nbsp;25.94&nbsp; &nbsp; &nbsp; 5.0710&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 8.79&nbsp; &nbsp; &nbsp; 0.22&nbsp; &nbsp; &nbsp; 8.58&nbsp; &nbsp; &nbsp;17.37&nbsp; &nbsp; &nbsp; 5.2811&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 8.79&nbsp; &nbsp; &nbsp; 0.14&nbsp; &nbsp; &nbsp; 8.65&nbsp; &nbsp; &nbsp; 8.72&nbsp; &nbsp; &nbsp; 5.4312&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 8.79&nbsp; &nbsp; &nbsp; 0.07&nbsp; &nbsp; &nbsp; 8.72&nbsp; &nbsp; &nbsp; 0.00&nbsp; &nbsp; &nbsp; 5.50

慕桂英3389331

&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; float Amount, Year;&nbsp; &nbsp; &nbsp; &nbsp; float Rate;&nbsp; &nbsp; &nbsp; &nbsp; double total = 0;&nbsp; &nbsp; &nbsp; &nbsp; Console.Write("Enter Amount Per Year:");&nbsp; &nbsp; &nbsp; &nbsp; Amount = Convert.ToInt32(Console.ReadLine());&nbsp; &nbsp; &nbsp; &nbsp; Console.Write("Enter Rate :");&nbsp; &nbsp; &nbsp; &nbsp; Rate = Convert.ToSingle(Console.ReadLine());&nbsp; &nbsp; &nbsp; &nbsp; Console.Write("Enter Time :");&nbsp; &nbsp; &nbsp; &nbsp; Year = Convert.ToInt32(Console.ReadLine());&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <= Year; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var current = Amount * (Rate / 100) * i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total += current;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Simple Interest is :{0}", total);&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadKey();&nbsp; &nbsp; }The using another method i can find Total Interest

明月笑刀无情

只是为了帮助使用decimal而不是double这里是您需要的代码:void Main(){&nbsp; &nbsp; //declare variables&nbsp; &nbsp; decimal amount;&nbsp; &nbsp; decimal rate;&nbsp; &nbsp; int years;&nbsp; &nbsp; //prompt loan amount&nbsp; &nbsp; Console.WriteLine("Enter loan amount");&nbsp; &nbsp; amount = Convert.ToDecimal(Console.ReadLine());//accepts console input and assigne to variable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//prompt for rate&nbsp; &nbsp; Console.WriteLine("Enter annual interest rate");&nbsp; &nbsp; rate = Convert.ToDecimal(Console.ReadLine());//accepts console input and assigne to variable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//prompt for monhts&nbsp; &nbsp; Console.WriteLine("Enter number of years");&nbsp; &nbsp; years = Convert.ToInt32(Console.ReadLine());//accepts console input and assigne to variable&nbsp; &nbsp; Loan loan = new Loan(amount, rate, years);//create&nbsp; new instance, send values to the class&nbsp; &nbsp; loan.LoanTable();}class Loan{&nbsp; &nbsp; //declare variables&nbsp; &nbsp; private decimal LoanAmount, InterestRate;&nbsp; &nbsp; private int LoanLength;&nbsp; &nbsp; //Constructor of Loan class that takes amount, rate and years&nbsp; &nbsp; public Loan(decimal amount, decimal rate, int years)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.LoanAmount = amount;&nbsp; &nbsp; &nbsp; &nbsp; this.InterestRate = DecPow(1m + rate / 100m, 1m / 12m) - 1m;&nbsp; &nbsp; &nbsp; &nbsp; this.InterestRate.Dump();&nbsp; &nbsp; &nbsp; &nbsp; this.LoanLength = years;&nbsp; &nbsp; }&nbsp; &nbsp; //returns the monnthly payment&nbsp; &nbsp; public decimal GetMonthlyPayment()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int months = LoanLength * 12;&nbsp; &nbsp; &nbsp; &nbsp; return (LoanAmount * InterestRate * DecPow(1m + InterestRate, months)) / (DecPow(1m + InterestRate, months) - 1m);&nbsp; &nbsp; }&nbsp; &nbsp; //Calculates totl interterest paid and doubles it, then returns the amount&nbsp; &nbsp; public decimal TotalInterestPaid(decimal number1, decimal number2)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; decimal TotalInterest = number1 + number2;&nbsp; &nbsp; &nbsp; &nbsp; return TotalInterest;&nbsp; &nbsp; }&nbsp; &nbsp; //prints the amortization of Loan&nbsp; &nbsp; public void LoanTable()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; decimal monthlyPayment = GetMonthlyPayment();//calculates monthly payment&nbsp; &nbsp; &nbsp; &nbsp; decimal principalPaid = 0m;&nbsp; &nbsp; &nbsp; &nbsp; decimal newBalance = 0m;&nbsp; &nbsp; &nbsp; &nbsp; decimal interestPaid = 0m;&nbsp; &nbsp; &nbsp; &nbsp; decimal principal = LoanAmount;&nbsp; &nbsp; &nbsp; &nbsp; decimal totalinterest = 0m;&nbsp; &nbsp; &nbsp; &nbsp; //nonth, payment amount, principal paid, interest paid, total interest paid, balance&nbsp; &nbsp; &nbsp; &nbsp; 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");&nbsp; &nbsp; &nbsp; &nbsp; for (int month = 1; month <= LoanLength * 12; month++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Compute amount paid and new balance for each payment period&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; totalinterest += interestPaid;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interestPaid = principal * InterestRate;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; principalPaid = monthlyPayment - interestPaid;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newBalance = principal - principalPaid;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Output the data item&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("{0,-10}{1,10:N2}{2,10:N2}{3,10:N2}{4,10:N2}{5,10:N2}",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; month, monthlyPayment, interestPaid, principalPaid, newBalance, TotalInterestPaid(totalinterest, interestPaid));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Update the balance&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; principal = newBalance;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private decimal DecPow(decimal x, decimal y) => (decimal)System.Math.Pow((double)x, (double)y);&nbsp; &nbsp; private decimal DecPow(decimal x, int p)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (p == 0) return 1m;&nbsp; &nbsp; &nbsp; &nbsp; decimal power = 1m;&nbsp; &nbsp; &nbsp; &nbsp; int q = Math.Abs(p);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <= q; i++) power *= x;&nbsp; &nbsp; &nbsp; &nbsp; if (p == q)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return power;&nbsp; &nbsp; &nbsp; &nbsp; return 1m / power;&nbsp; &nbsp; }}特别注意DecPow方法。
随时随地看视频慕课网APP
我要回答