贷款C#控制台应用程序,利息总额不会从零开始
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了贷款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,101,102,103,104,105,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
interestPaid = principal * InterestRate;
principalPaid = monthlyPayment - interestPaid;
newBalance = principal - principalPaid;
totalinterest += interestPaid;
// Output the data item
Console.WriteLine("0,-101,10:N22,10:N23,10:N24,10:N25,10:N2",
month, monthlyPayment, interestPaid, principalPaid, newBalance, TotalInterestPaid(totalinterest,interestPaid));
// Update the balance
principal = newBalance;
主要计划
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Loan_Program
class LoanTest
static void Main(string[] args)
//declare variables
double amount;
double rate;
int years;
//prompt loan amount
Console.WriteLine("Enter loan amount");
amount = Convert.ToDouble(Console.ReadLine());//accepts console input and assigne to variable
//prompt for rate
Console.WriteLine("Enter annual interest rate");
rate = Convert.ToDouble(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();
Console.ReadKey();
答案
在计算新的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,101,102,103,104,105,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,-101,10:N22,10:N23,10:N24,10:N25,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
另一答案
只是为了帮助使用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,101,102,103,104,105,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,-101,10:N22,10:N23,10:N24,10:N25,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
方法。
另一答案
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
以上是关于贷款C#控制台应用程序,利息总额不会从零开始的主要内容,如果未能解决你的问题,请参考以下文章