So I have to make this investment calculator which is posted down below me. After I compile and run it. The file seems trk perfectly fine except for one error. If I told it 100 for Initial Investment and then 500 for Investment goal. It says it would take 499 years. What did I do wrong?
PHP Code:
/**
* @(#)Investment 2.java
*
*
* @author
* @version 1.00 2011/1/16
*/
import java.util.*;
public class Investment2 {
public static void main(String[] args) {
double principal = 0;
double interest = 0;
double rate = 0.10;
int years = 0;
double goal = 0;
double total = 0;
int userinput = -1;
Scanner myScanner = new Scanner(System.in);
// Welcome message
System.out.println("Welcome to the Investment Calculator Beta");
// Enter initial amount
System.out.println ("Enter initial investment amount. If you want to exit enter 0.");
int input1 = myScanner.nextInt();
principal = input1;
// Decision for exit
if (input1 == 0){
System.exit(0);
}
// Enter goal amount
System.out.println ("Enter your goal investment amount: ");
int input2 = myScanner.nextInt ();
goal = input2;
System.out.println ("The fixed interest rate is 10%"); //Change if you have different rate
// Loop to get total years
total= principal;
total= (interest+1)*total;
for (total = 0; total < goal; total++){
System.out.println("To reach your $" + goal + "0 goal it will take " + years + " years.");
years++;
// Continue play
if (total == goal){
System.out.println("Would you like to enter another amount?");
int input3 = myScanner.nextInt ();
} while (userinput == 1);
}
}
}
Results 1 to 3 of 3
Thread: Java Help
- 17 Jan. 2011 01:13am #1
Java Help
Last edited by Chad; 17 Jan. 2011 at 09:33pm.
- 17 Jan. 2011 06:33pm #2
Just from skimming over it:
Code:// Loop to get total years total= principal; total= (interest+1)*total; for (total = 0; total < goal; total++){ System.out.println("To reach your $" + goal + "0 goal it will take " + years + " years."); years++;
- 23 Jan. 2011 02:43am #3
^ somewhat of what he just said. Asking for user input is easy. The algorithm is the tricky part. Clean up your un-needed variables. Your loop basically does nothing but count.
Code:/** * @(#)Investment 2.java * * * @author * @version 1.00 2011/1/16 */ import java.util.*; public class Investment2 { public static void main(String[] args) { double principle = 0; double goal = 0; double total = 0; int userinput = -1; Scanner myScanner = new Scanner(System.in); // Welcome message System.out.println("Welcome to the Investment Calculator Beta"); // Enter initial amount System.out.println ("Enter initial investment amount. If you want to exit enter 0."); principle = myScanner.nextInt(); // Decision for exit if (principle == 0){ System.exit(0); } // Enter goal amount System.out.println ("Enter your goal investment amount: "); goal = myScanner.nextInt (); //System.out.println ("The fixed interest rate is 10%"); //Change if you have different rate // Loop to get total years total = principle; for (int j = 0; total <= goal; j++) { total = total + total*.1; if(total >= goal) { System.out.println("To reach your $" + goal + "0 goal it will take " + j + " years."); } } // Continue play if (total >= goal){ System.out.println("Would you like to enter another amount?"); int input3 = myScanner.nextInt (); } while (userinput == 1); } }
posting nonsensical crap everyday.