Hello all. I will not try to skip around and just say it flat out. I need help with some homework. I need to write two loops for a program, and right now I am attempting to code a for loop for the program that will calculate the value of an investment after so many years.
I have it all written, and the sytnax is good ( I think, I get no errors) but something in the logic of my code is wrong, because when I change how many iterations my loop has I get the same amount, or zero.
I have been told in learning programming that many times a second opinion can help, so if anyone can lend a hand, it would be most appreciated.
here is the loop I am using
for ( month = 1; month < 59; month++) { answer1 = (amount + (amount * 1.05)); }
and here is the code
// This application performs a simple analysis on an growing investment import javax.swing.JOptionPane; import java.text.DecimalFormat; public class InvestGrowth { public static void main( String args[] ) { double answer1, amount; int answer2, month; DecimalFormat twoDigits = new DecimalFormat( "0.00" ); // Assuming an investment of $1000 at 5% per month, this // loop calculates the value of the investment after 5 years answer1 = 0.0; amount =(1000*1.05); for ( month = 1; month < 59; month++) { answer1 = (amount + (amount * 1.05)); } // Assuming an investment of $1000 at 5% per month, this // loop calculates how long it will take until the investment // reaches $1 million. answer2 = 0; JOptionPane.showMessageDialog(null, "$1000 compounding a 5% per month is valued at $" + twoDigits.format(answer1) + "\n" + "It takes " + answer2 + " months to reach $1 million"); System.exit(0); } // end main } // end class BactGrowth
To explain some things, I used only 59 months and started at 1 because I set amount at 1000 * 1.05 already, for the first months calculation instead of starting at 0 and 60.