3 回答
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
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
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方法。
- 3 回答
- 0 关注
- 210 浏览
添加回答
举报