贷款 C# 控制台应用程序,利息总额不从零开始
Loan C# console app, interest total doesn't start at zero
除了第一行的输出显示已汇总的 运行 利息支付总额,而不是与第一个利息金额相同,我已经按照它应该的方式工作了。因此,如果第一个月的利息是 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");
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,-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;
}
}
}
}
主程序
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();
}
}
}
在计算新的 interestPaid
之前增加 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");
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
只是为了帮助使用 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
方法。
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
除了第一行的输出显示已汇总的 运行 利息支付总额,而不是与第一个利息金额相同,我已经按照它应该的方式工作了。因此,如果第一个月的利息是 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");
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,-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;
}
}
}
}
主程序
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();
}
}
}
在计算新的 interestPaid
之前增加 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");
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
只是为了帮助使用 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
方法。
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