找不到我的代码中的错误所在会导致我的代码给出错误的答案

时间:2019-07-10 09:02:45

标签: 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。

0 个答案:

没有答案