So I keep receiving the answer of 0
regardless of the inputs to the method. When I input 6
months, 100
savings amount, and 0.05
interest rate it should give me 608.81
but the response is 0
. I feel like it could be my for
loop or the parameters that I have set that are messing with it. I have tried all I can think of regarding the for
loop. Here's the code:
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));
}
}
}
You are passing the following variables to your method :
public static double compoundValueMethod(double savingAmount,
double annualInterestRate,
int NumberOfMonth)
Note but use the static variable numberOfMonth
in your method instead of the passed NumberOfMonth
parameter (variable names in Java are case sensitive). numberOfMonth
is initialized to 0
by default, so your for loop is not entered and the method returns 0.
You should eliminate the static variables and only use local variables. Had you done that in the first place, you'd get a compilation error which would make it much easier to find your bug.
You dont need static variables. Update code as below
public class 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));
}
}
}
use NumberOfMonth
instead of numberOfMonth
for (int i = 0; i < NumberOfMonth; i++) {
sixthMonth = ((savingAmount+sixthMonth) * (1 + monthlyInterestRate));
}
or inside method you can assign
numberOfMonth=NumberOfMonth;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.