java - 明确转换: java时发生未知错误

标签 java compiler-errors

我的代码有问题,我不确定为什么,有人可以帮我吗?我正在尝试重构我的主要方法,并且由于这个错误,我的想法也不允许我这样做。该错误位于第24行,我已在其中添加了注释。根据要求,我提供了错误消息和计算余额方法。
Error Message

import java.text.NumberFormat;
import java.util.Scanner;

public class Main {
final static int Months_In_Year = 12; 
final static int Percent = 100;

public static void main(String[] args) {
    int principal = (int) readNumber("Principal: ", 1000, 1_000_000);
    double annualInterest = (double) readNumber("Annual Interest Rate: ", 1, 30);
    byte years = (byte) readNumber("Period(years):  ", 1, 30);

    printMortgage(principal, annualInterest, years);

    System.out.println("PAYMENT SCHEDULE");
    System.out.println("--------------------");
    for( short month = 1; month < years * Months_In_Year; month++){
        double balance = calculateBalance(principal, annualInterest, years, month); // error here
        System.out.println(NumberFormat.getCurrencyInstance().format(balance));
    }

}

private static void printMortgage(int principal, double annualInterest, byte years) {
    double mortgage = calculateMortgage(principal, annualInterest, years);
    String mortgageCalculated = NumberFormat.getCurrencyInstance().format(mortgage);
    System.out.println("MORTGAGE");
    System.out.println("---------------------");
    System.out.println("Monthly Payments: " + mortgageCalculated);
}

public static double readNumber(String prompt, double min, double max) {
    Scanner scanner = new Scanner(System.in);
    double value;

    while (true) {
        System.out.println(prompt);
        value = scanner.nextDouble(); //change to float, don't need as much memory
        if ((value >= min) && (value <= max))
            break;

        System.out.println("Pick value between " + min + " and " + max);
    }
    return value;
}


public static double calculateMortgage(
        int principal,
        double annualInterest,
        byte years) {
    double monthlyInterest = annualInterest / (Percent * Months_In_Year);
    double numberOfPayments = years * Months_In_Year;
    double mortgage = principal
            * (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments)
            / Math.pow(1 + monthlyInterest, numberOfPayments));
    return mortgage;
}

public static double calculateBalance (
        int principal,
        double mortgage,
        double annualInterest,
        byte years,
        short numberOfPaymentsMade ){

    double monthlyInterest = annualInterest / (Percent * Months_In_Year);
    double numberOfPayments = years * Months_In_Year;
    double balance =(double) principal *
            (Math.pow(1 + monthlyInterest, numberOfPayments) - Math.pow(1 + monthlyInterest, numberOfPayments)
            / (Math.pow(1 + monthlyInterest, numberOfPayments) - 1));
                    return balance;

    }
}

最佳答案

在下面的代码中

public static double calculateBalance (
        int principal,
        double mortgage,
        double annualInterest,
        byte years,
        short numberOfPaymentsMade ){

    double monthlyInterest = annualInterest / (Percent * Months_In_Year);
    double numberOfPayments = years * Months_In_Year;
    double balance =(double) principal *
            (Math.pow(1 + monthlyInterest, numberOfPayments) - Math.pow(1 + monthlyInterest, numberOfPayments)
            / (Math.pow(1 + monthlyInterest, numberOfPayments) - 1));
                    return balance;

    }
}

删除双重抵押,因为我们不需要它。因此,最终答案:-
public static double calculateBalance (
        int principal,
        double annualInterest,
        byte years,
        short numberOfPaymentsMade ){

    double monthlyInterest = annualInterest / (Percent * Months_In_Year);
    double numberOfPayments = years * Months_In_Year;
    double balance =(double) principal *
            (Math.pow(1 + monthlyInterest, numberOfPayments) - Math.pow(1 + monthlyInterest, numberOfPayments)
            / (Math.pow(1 + monthlyInterest, numberOfPayments) - 1));
                    return balance;

    }
}

关于java - 明确转换: java时发生未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59097189/

相关文章:

java - Resilience4j 断路器环位缓冲区大小配置

java - 为什么 Jenkins 无法加载资源?

java - 列表未打印出其中一个变量

Java:扑克手牌

html - scss css编译在以下css上抛出错误

java - 设置具有相同的删除,但都不会覆盖另一个错误

c - 我在这里做错了什么?

Java 数据建模 - 将关系数据保存为可互操作的格式

c++ - 创建一个伪元组,一个存储在别处的数据的前端

haskell - 默认方法中对幻像类型的类约束的编译错误