我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*P4Q6
*Name:Wang Chaoran
* 6 (Displaying the loan amortization schedule)
The monthly payment for a given loan pays the principal and the interest.
The monthly interest is computed by multiplying the monthly interest rate and the balance (the remaining principal).
The principal paid for the month is therefore the monthly payment minus the monthly interest.
Write a program that lets the user enter the loan amount, number of years, and interest rate,
and displays the amortization schedule for the loan. Suppose you enter the loan amount 10,000 for one year with an interest rate of 7%;
display a table as follows:
Loan Amount: 10000
Number of Years: 1
Annual Interest Rate: 7%
Monthly Payment: 865.26
Total Payment: 10383.21
Payment# Interest Principal Balance
1 58.33 806.93 9193.07
2 53.62 811.64 8381.43
...
11 10.0 855.26 860.27
12 5.01 860.25 0.01
Note: The balance after the last payment may not be zero. If so, the last payment should be the normal monthly payment plus the final balance.
*/
import java.util.Scanner;
public class P4Q6
{
public static void main(String[] args)
{
//Create Scanner
Scanner scan = new Scanner(System.in);
//Read in all the values
System.out.println("Please enter the loan amount(eg.10000):");
Double loanAmount=scan.nextDouble();
System.out.println("Please enter the number of years(eg.1):");
Double numberOfyears=scan.nextDouble();
System.out.println("Please enter the annul interest rate(eg.0.07 for 7%):");
Double annualInterestrate=scan.nextDouble();
System.out.println("Loan Amount: "+loanAmount);
System.out.println("Number of Years: "+numberOfyears);
System.out.println("Annual Interest Rate: "+annualInterestrate*100+"%");
double monthlyPayment=0,sum=0,totalPayment=0,monthlyInterstrate=annualInterestrate/12,interest=0,principal=0,balance=loanAmount;
//Print the first half
for(double i=numberOfyears*12;i>=1;i--)
{
sum+=1/(Math.pow(monthlyInterstrate+1,i));
}
monthlyPayment = loanAmount/sum;
System.out.printf("%-16s%-30.2f\n","Monthly Payment: ",monthlyPayment);
totalPayment = monthlyPayment*12*numberOfyears;
System.out.printf("%-15s%-30.2f\n","Total Payment: ",totalPayment);
//Print the second half(the table)
System.out.println("Payment# Interest Principal Balance");
for(int i=1;i<=numberOfyears*12;i++)
{
interest=balance*monthlyInterstrate;
principal=monthlyPayment-interest;
balance-=principal;
System.out.printf("%-13d%-13.2f%-13.2f%-13.2f\n",i,interest,principal,balance);
}
}
}
导入论坛查看(69)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing