java - 无法找到我的代码中的错误导致我的代码给出错误答案的位置
问题描述
我的代码应该找到贷款的每月支付。它没有给出正确的答案。我正在学习 java 课程的介绍,他们希望我们以某种方式去做,所以请保持一切不变,除非我搞砸了。
我们必须向用户询问他们的年利率(不是每月)、贷款期限和初始现金金额
等式是 P = (r * L) / (1-(1+r)^-n) 其中 r 是月利率,L 是贷款期限,n 是贷款月数。P 是贷款的每月还款额
我很难找到我的数学在哪里搞砸了,但我担心我有狭隘的视野,无法找到纠正它的地方。
import java.util.Scanner;
public class JavaProgram {
public static void main (String [] args){
Scanner input = new Scanner (System.in);
System.out.println ("This program calculates your monthly payment");
System.out.println();
System.out.print ("Please enter the loan amount:" );
double loanAmount = input.nextDouble();
System.out.print ("Please enter the annual interest amount as a percentage, e.g 0.055 for 5.5%:" );
double annualInterest = input.nextDouble();
System.out.print ("Please enter the length of the loan payback:" );
double lengthYears = input.nextInt();
System.out.println ("Your monthly payback is" +
divisionPart(multiplicationPart(monthlyInterest(annualInterest), lengthYears),
secondPartOfEquation(monthlyInterest(annualInterest), yearsToMonths(lengthYears))));
}
public static double secondPartOfEquation(double x, double y) {
double result = (1- Math.pow((1 + x), -y));
return result;
}
public static double yearsToMonths(double x) {
double result = (x * 12);
return result;
}
public static double multiplicationPart(double x, double y) {
double result = (x * y);
return result;
}
public static double divisionPart(double x, double y) {
double result = (x / y);
return result;
}
public static double monthlyInterest(double x) {
double result = (x / 12);
return result;
}
}
输入:
初始现金 10000,长度 5 年,年百分比 5.5% (0.055)
我应该得到 191.01,但得到 0.09550581085891031。
解决方案
推荐阅读
- php - 通过 Phonegap Build API 使用特定密钥构建 APP?
- node.js - Azure 文件共享 API - x-ms-version 的标头格式无效
- asp.net-core - 使用简单注入器和 .Net Core 的依赖注入
- python - API 定义的数据帧(动态生成)到表中(Dash_Table_Experiments)
- python - 用 Python 编写单元测试
- splunk - 设置 splunk 警报为每个主机发送警报
- xslt - WSO2 EI :
- scala - 如何在 Spark 中创建集值列?
- apache-nifi - 如何优化使用 nifi 等待处理器
- image - Unity Sprite 编辑器:您可以在设置 Pivot 时预览背景图像吗?