gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:10:08 28 4
gpt4 key购买 nike

我的代码有问题,我不确定为什么,有人可以帮我吗?我正在尝试重构我的主要方法,并且由于这个错误,我的想法也不允许我这样做。该错误位于第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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com