2016-02-25 226 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)); 
    } 

} 

}

相关问题