无法弄清楚如何将公式提高到幂。我也已经在java中导入了java.lang.Math。我只是不断在“ Math”上收到Sytax错误,请删除此令牌,并且无法在原始数据类型double错误上调用pow(double)
这是假设有30年贷款的公式
年金因子=(0.003125 *(1 + 0.003125)^ 360)/((((1 + 0.003125)^ 360)-1) 360是30年的时间12个月才能获得每月付款
import java.util.Scanner;
import java.lang.Math;
public class HW3Method {
public static void main(String[] args) {
// main method for user inputs
Scanner info = new Scanner(System.in);
System.out.println("Enter the starting annual rate as a percent (n.nnn)");
double startRate = info.nextDouble();
System.out.println("Enter the ending annual rate as a percent (n.nnn)");
double endRate = info.nextDouble();
System.out.println("Enter the annual intrest rate increments as a percent (n.nnn)");
double rateIncrease = info.nextDouble();
System.out.println("Enter the first term in years for calculating payments");
double firstTerm = info.nextDouble();
System.out.println("Enter the last term in years for calculating payments");
double lastTerm = info.nextDouble();
System.out.println("Enter the term increment in years");
int termIncrement = info.nextInt();
System.out.println("Enter the loan amount");
double loanAmount = info.nextDouble();
double mtp = firstTerm * 12;
}
public double calcAnnuity(double mtp ) {
double annuityFactor = (0.003125*(1+0.003125)Math.pow(mtp));
return annuityFactor;
}
}
答案 0 :(得分:1)
您使用的方法Math.pow
错误。它需要两个参数,即基数和指数。您写道:
0.003125 * (1 + 0.003125) Math.pow(mtp)
但是您需要写:
0.003125 * Math.pow(1.0 + 0.003125, mtp)
请注意,1.0 + 0.003125
可以简化为1.003125
,所以:
0.003125 * Math.pow(1.003125, mtp)
更好的办法是将该神奇数字存储为常量,然后只需要更改一个变量即可,而无需更改很多:
private static final int FACTOR = 0.003125;
然后使用该常量:
FACTOR * Math.pow(1.0 + FACTOR, mtp)
摘自Math.pow的官方文档:
public static double pow(double a, double b)
将第一个自变量的值返回第二个自变量的幂。特殊情况:[...]