我的编程存在问题。每月付款和总付款不会累计输出。意思是它像这样保持不变
Monthly Payment Total Payment
$188.71 $11322.74
以下是我的代码示例
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Loan Amount: ");
double loanAmount=in.nextDouble();
System.out.print("Number of Years: ");
int years=in.nextInt();
double annualInterestRate = 5;
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate /
(1-1 / Math.pow(1 + monthlyInterestRate, years * 12));
double totalPayment = monthlyPayment * 12 * years;
double i = 5.0;
int lastMonth = years * 12;
int month = 1;
while ( i <= 8.0)
{
monthlyPayment = (int) (monthlyPayment * 100) / 100.0;
totalPayment = (int) (totalPayment * 100) / 100.0;
i +=0.125;
System.out.println("Interest Rate Monthly Payment Total Payment");
System.out.println(i + "% $" + monthlyPayment + " $" + totalPayment);
}
}
输出必须像这样
Loan Amount: 10000
Number of Years: 5
Interest Rate Monthly Payment Total Payment
5.000% 188.71 11322.74
5.125% 189.29 11357.13
5.250% 189.86 11391.59
....
7.875% 202.17 12129.97
8.000% 202.76 12165.84
但我的输出结果是
Interest Rate Monthly Payment Total Payment
5.000% 188.71 11322.74
5.125% 188.71 11322.74
5.250% 188.71 11322.74
....
7.875% 188.71 11322.74
8.000% 188.71 11322.74
答案 0 :(得分:0)
在此处查看您的代码块是在每月付款和总付款类别中一遍又一遍地重复样本运行的原因
while ( i <= 8.0)
{
monthlyPayment = (int) (monthlyPayment * 100) / 100.0;
totalPayment = (int) (totalPayment * 100) / 100.0;
i +=0.125;
System.out.println("Interest Rate Monthly Payment Total Payment");
System.out.println(i + "% $" + monthlyPayment + " $" + totalPayment);
}
我猜测每次运行while循环时,totalPayment和monthlyPayment需要通过变化率i来实现
为了让这些付款随着费率的增加而变化,您需要在循环中加入新的计算,例如
monthlyPayment = loanAmount * i / (1-1 / Math.pow(1 + i, years * 12));
这组代码是在循环之前创建的,可以在内部移动以获得更精简的数量。
答案 1 :(得分:0)
您应该提及代码的意图。我认为这是以不同的利率打印每月付款和贷款总额。
这将完全回答这个问题:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Loan Amount: ");
double loanAmount = in.nextDouble();
System.out.print("Number of Years: ");
int years = in.nextInt();
double annualInterestRate = 5;
double i = 5.0;
int lastMonth = years * 12;
int month = 1;
while (i <= 8.0) {
double monthlyInterestRate = (annualInterestRate + i) / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, years * 12));
double totalPayment = monthlyPayment * 12 * years;
System.out.println("Interest Rate Monthly Payment Total Payment");
System.out.println(i + "% $" + monthlyPayment + " $" + totalPayment);
i += 0.125;
}
}
答案是你在while循环的每次迭代中都重新计算monthlyPayment和totalPayment。所以我把它移到了while循环中。
答案 2 :(得分:0)
您似乎正在进行正确的计算,但只是在循环之外,这可能只是一个for循环。
Scanner in = new Scanner(System.in);
System.out.print("Loan Amount: ");
double loanAmount = in.nextDouble();
System.out.print("Number of Years: ");
int years = in.nextInt();
// Unused variables...
// int lastMonth = years * 12;
// int month = 1;
for (double annualInterestRate = 5.0; annualInterestRate <= 8.0; annualInterestRate += 0.125) {
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate /
(1 - 1 / Math.pow(1 + monthlyInterestRate, years * 12));
double totalPayment = monthlyPayment * 12 * years;
System.out.println("Interest Rate\tMonthly Payment\tTotal Payment");
System.out.printf("%.03f%%\t$%.02f\t$%.02f\n", annualInterestRate, monthlyPayment, totalPayment);
}