Hi, i'm working on the last of five programs i need to hand in tonight for an assignment and i'm completely stuck. The instructions are to create a JFrame Form program that calculates the money for someone who has "been hired to a very dangerous job in the amazon rainforest, your pay starts at 0,01 the first day and then doubles to 0,02 next day, then 0,04 etc. How many days must you survive before becoming a millionaire? Create a GUI that shows in a text area how much you make each day. Also show how many days you survived before becoming a millionaire."
I'm supposed to do the calculation using a for loop, or if preferred, a while loop. The relevant part of the code i have at the moment is:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { NumberFormat currency = NumberFormat.getCurrencyInstance(); DecimalFormat pay = new DecimalFormat("0.00"); jTextArea1.setText("Day " + " Pay" +"\n"); double amount = 0; double initial = 0.01; double rate = 1; // calculate amount on deposit for each of ten years for ( double day = 1; amount <= 1000000; day++ ) { // calculate new amount for specified day amount = initial * Math.pow(1 + rate, day ); // display the day and the amount jTextArea1.append( day + " " + pay.format(amount) + "\n"); } // end for }
The output is supposed to start at 0.01 on day 1 like i said, but mine starts at 0.02 and therefore takes one day less than it is supposed to if it was correct. What am i missing here?
Output:
Day Pay 1.0 0,02 2.0 0,04 3.0 0,08 4.0 0,16 5.0 0,32 6.0 0,64 7.0 1,28 8.0 2,56 9.0 5,12 10.0 10,24 11.0 20,48 12.0 40,96 13.0 81,92 14.0 163,84 15.0 327,68 16.0 655,36 17.0 1310,72 18.0 2621,44 19.0 5242,88 20.0 10485,76 21.0 20971,52 22.0 41943,04 23.0 83886,08 24.0 167772,16 25.0 335544,32 26.0 671088,64 27.0 1342177,28