Ваша формула сложных процентов была неправильной. Формула :
final amount = initial amount times
1 + interest rate / number of times interest applied
raised to the power of number of times interest applied
times number of time periods elapsed
Процентная ставка, которая обычно определяется как процент в год, должна быть преобразована в число за период времени путем деления на 100 и деления на число. периодов времени в году.
Что касается вашего кода, я создал метод для получения входных значений.
Я также разбил расчет сложных процентов, чтобы я мог проверить результаты каждая часть расчета.
Вот исправленный код.
import java.text.NumberFormat;
import javax.swing.JOptionPane;
public class CICalc {
public static void main(String[] args) {
NumberFormat nf = NumberFormat.getCurrencyInstance();
double principal = getValue("Enter the principal amount");
double rate = getValue("Enter the yearly interest rate");
double timesapplied = getValue("Enter the number of times "
+ "the principal is compounded per year");
double elapsedmonths = getValue("Enter the amount of time "
+ "in months the principal is invested");
double temp1 = 1.0d + rate * 0.01d / timesapplied;
double temp2 = timesapplied * elapsedmonths / 12d;
double finalAmount = principal * Math.pow(temp1, temp2);
JOptionPane.showMessageDialog(null,
"The total amount returned after " +
elapsedmonths +
" months is " + nf.format(finalAmount) +
" and the the interest gained is " +
nf.format(finalAmount - principal));
}
private static double getValue(String prompt) {
String response = JOptionPane.showInputDialog(prompt);
return Double.parseDouble(response);
}
}