所以我不断收到0
的答案,不管方法的输入如何。当我输入6
个月,100
储蓄金额,和0.05
利率它应该给我608.81
,但答复是0
。我觉得它可能是我的for
循环或我设置的参数与它混淆。我试过所有我能想到的有关for
循环。下面的代码:为什么这个方法总是产生0作为它的返回值?
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class FifthAssignment2 {
static double savingAmount = 0.0; // the 2 given static doubles
static double annualInterestRate = 0.0;
static int numberOfMonth;
static double sixthMonth = 0.0;;
public static double compoundValueMethod(double savingAmount, double annualInterestRate, int NumberOfMonth) {
{
DecimalFormat formatter = new DecimalFormat(".00");
double monthlyInterestRate = annualInterestRate/12;
if (savingAmount < 0)
if (annualInterestRate < 0) {
JOptionPane.showMessageDialog(null, "Error. Both of your values are negative!");
System.exit(0);
}
if (savingAmount < 0) {
JOptionPane.showMessageDialog(null, "Error. Your savings amount is negative!");
}
else if (annualInterestRate < 0) {
JOptionPane.showMessageDialog(null, "Error. Your annual interest rate is negative!");
}
else {
for (int i = 0; i < numberOfMonth; i++) {
sixthMonth = ((savingAmount+sixthMonth) * (1 + monthlyInterestRate));
}
}
return sixthMonth;
}
}
public static void main(String[] args) {
DecimalFormat formatter = new DecimalFormat(".00");
int numberOfMonth;
String NOM = JOptionPane.showInputDialog("How many months? ");
numberOfMonth = Integer.parseInt(NOM);
String SA = JOptionPane.showInputDialog("What is your savings amount? ");
savingAmount = Double.parseDouble(SA);
String AIR = JOptionPane.showInputDialog("What is the annual interest rate? "); // Window pops up
annualInterestRate = Double.parseDouble(AIR);
{
JOptionPane.showMessageDialog(null, "Your total compounded value after 6 months is "
+ compoundValueMethod(savingAmount, annualInterestRate, numberOfMonth));
}
}
}
哇。这是一个大写字母的整个时间。非常感谢!! – josh102894
@ josh102894不客气 – Eran
如果你不介意我问我如何格式化它只使用两位小数。以前我用formatter.format – josh102894