首页 > 解决方案 > 无法找到我的代码中的错误导致我的代码给出错误答案的位置

问题描述

我的代码应该找到贷款的每月支付。它没有给出正确的答案。我正在学习 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。

标签: java

解决方案


推荐阅读