public class Balance {
public static void main(String[] args) {
System.out.printf("%.2f\n", balance(0.0, 0.0, 0.0));
}
/**
* @param principal
* @param rate
* @param years
* @return
*/
public static double balance(double principal, double rate, double years) {
double amount = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the initial investment amount: ");
principal = sc.nextDouble();
System.out.print("Enter the interest rate: ");
rate = sc.nextDouble();
System.out.print("Enter the number of years: ");
years = sc.nextDouble();
for (int i = 1; i < years; i++) {
amount = principal * Math.pow(1.0 + rate, years);
amount += principal;
}
return amount - principal;
}
}
My problem is with the printf line that I am using within the main method. Eclipse wants me to change the method balance from void to Object[]. When I do this I must return a value from balance. So I guess my question is, how would I return the proper value? Am I on the right track? Thank you for your time and constructive criticism. 🙂
EDIT – Thanks for the help everyone, much appreciated 🙂 My math is off. I end up with 1000 more than I should have. hmmm.
So should I just take a 1000 from amount like so:
return amount - 1000;
Or this:
return amount - principal;
EDIT this is what I am going with since it is due tonight. Thanks to all for the assistance. 🙂
A few points:
balance()cannot be void, because you use its return value inS.out.printf(). Do you want balance to print to the screen, or do you want it to yield a number?Your loop
for (years = 0; years > 10; years++)won’t run. Think about why. It might help to convert theforinto awhile, to visualize why.You read in years as a
double, but then use it as a counter in your loop. What type should it actually be?Your
balance()function takes three parameters, then immediately gets input and obliterates them. Do you wantbalance()to be provided these numbers, or do you want it to fetch them?Otherwise, you seem to be on the right track.