所以我不斷收到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