我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*P4Q5
*Name:Wang Chaoran
*Description:
* (Comparing loans with various interest rates) Write a program that lets the user enter the loan amount
* and loan period in number of years and displays the monthly and total payments for each interest rate
* starting from 5% to 8%, with an increment of 1/8. Suppose you enter the loan amount 10,000 for five years;
* display a table as follows:
Loan Amount: 10000
Number of Years: 5
Interest Rate Monthly Payment Total Payment
5% 188.71 11322.74
5.125% 189.28 11357.13
5.25% 189.85 11391.59
...
7.85% 202.16 12129.97
8.0% 202.76 12165.83
*/
import java.util.Scanner;
import java.text.DecimalFormat;
public class P4Q5
{
public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat("0.000");
//Create Scanner
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the loan amount(eg.10000):");
//Read in the numbers
Double loan = scan.nextDouble();
System.out.println("Please enter the number of years(eg.5):");
double numberOfyears = scan.nextInt();
//Display the first line
System.out.printf("%-14s%-14s%-14s\n","Interest Rate","Monthly Payment","Total Payment");
double interestRate = 0;
//Display the other lines
for(interestRate = 0.05000;interestRate<=0.080000001;interestRate=interestRate+0.01000/8)
{
double sum =0,monthlyPayment = 0,totalPayment=0;
for(double i=numberOfyears*12;i>=1;i--)
{
sum+=1/Math.pow((1+interestRate/12),i);
}
monthlyPayment=loan/sum;
totalPayment=monthlyPayment*12*numberOfyears;
String display=df.format(interestRate*100)+"%";
System.out.printf("%-14s%14.2f%14.2f\n",display,monthlyPayment,totalPayment);
}
}
}
导入论坛查看(106)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing