2016-02-25 223 views
2

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

    } 
} 

回答

3

你傳入以下變量的方法:

public static double compoundValueMethod(double savingAmount, 
             double annualInterestRate, 
             int NumberOfMonth) 

注意,但在你的方法使用靜態變量numberOfMonth而不是通過NumberOfMonth參數(變量名在Java中是區分大小寫)。 numberOfMonth默認情況下初始化爲0,因此您的for循環未輸入且方法返回0.

您應該消除靜態變量並僅使用局部變量。如果你首先做到了這一點,那麼你會得到一個編譯錯誤,這會讓你更容易找到你的bug。

+0

哇。這是一個大寫字母的整個時間。非常感謝!! – josh102894

+0

@ josh102894不客氣 – Eran

+0

如果你不介意我問我如何格式化它只使用兩位小數。以前我用formatter.format – josh102894

0

使用numberOfMonth

for (int i = 0; i < NumberOfMonth; i++) { 
       sixthMonth = ((savingAmount+sixthMonth) * (1 + monthlyInterestRate)); 
      } 

NumberOfMonth代替或內部方法,你可以指定

numberOfMonth=NumberOfMonth; 
1

你不需要靜態變量。 更新代碼如下

公共類FifthAssignment2 {

public static double compoundValueMethod(double savingAmount, double annualInterestRate, int numberOfMonth) { 
    { 
     DecimalFormat formatter = new DecimalFormat(".00"); 
     double sixthMonth = 0.0; 
     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); 


    double savingAmount; 

    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 

    double annualInterestRate; 
    annualInterestRate = Double.parseDouble(AIR); 

    { 
     JOptionPane.showMessageDialog(null, "Your total compounded value after 6 months is " 
       + compoundValueMethod(savingAmount, annualInterestRate, numberOfMonth)); 
    } 

} 

}

相關問題