Pergunta

I am writing a Java program. It supposed to be print out yearend account balance, invested this year, annual return, and number of the year. EX:

Year 1 – Invest $10,000 @ 10% you end up with $11,000 Year 2 – Invest another $10,000 on top of the $11,000 you already have so now you have $21,000 and you make $2,100 in annual return so you end up with $23,100 and keep going until reach 6 years.

My code printed all 6 years but with same value as year 1. So anything wrong with the loop? Thanks so much. Here my code:

import java.io.*;
import java.util.*;
import java.text.*;
public class ExamFive {
public static void main(String[] args) {
    final int MAX_INVESTMENT = 5000000;
    double goalAmount;
    double amountInvested;
    double annualRate;
    double interest;
    double total= 0;
    int year = 0;
    Scanner myScanner = new Scanner(System.in);
    System.out.println("Enter your investment goal amount: ");
    goalAmount = myScanner.nextDouble();
    if (goalAmount > MAX_INVESTMENT) {
        System.out.println("Your goal is outside the allowed 5,000,000 limit");
    }
    System.out.println("Enter the amount annually invested: ");
    amountInvested = myScanner.nextDouble();
    System.out.println("Enter the expected annual return rate (ex 6.50): ");
    annualRate = myScanner.nextDouble();

    do {
        interest = amountInvested * (annualRate / 100);
        total = amountInvested + interest;
        year++;
        System.out.println("Yearend account balance " + total + 
                " Invested this year " + amountInvested + " Annual  return " + interest
                + " number of years " + year);

    } while (total < MAX_INVESTMENT && year < 6);

    System.out.println(" ");
    System.out.println("Your investment goal " + goalAmount);
    System.out.println("Your annualinvestment amount " + amountInvested);
    System.out.println("Number of years to reach your goal " + year);
}

}

Here is the output:

Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 1 Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 2 Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 3 Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 4 Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 5 Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 6

Foi útil?

Solução

It seems that you're also calculating the interest on the annual savings only.

Change:

do {
    interest = amountInvested * (annualRate / 100);
    total = amountInvested + interest;
    year++;
    ...
} while (total < MAX_INVESTMENT && year < 6);

To:

do {
    total += amountInvested;
    interest = total * annualRate / 100;
    total += interest;
    year++;
    ...
} while (total < MAX_INVESTMENT && year < 6);

Outras dicas

You aren't keeping a running total.

Looks like maybe a 3rd or 4th week tutorial but it's a fair question at least you asked what's wrong.

Try: total += (amountInvested + interest);

parenthesis brackets aren't needed but I usually find them a great logical grouping device.

Check the loop below you are not adding 10,000 as you stated

do {
    interest = amountInvested * (annualRate / 100);
    total = amountInvested + interest;
    year++;
    System.out.println("Yearend account balance " + total + 
            " Invested this year " + amountInvested + " Annual  return " + interest
            + " number of years " + year);

}

Add following line

amountInvested += (total + 10000);

as the last line in do while loop

amountInvested, annualRate are always the same. you are not incrementing or decrementing these values in the loop. So, they will always remain the same. Also, the formula to caculate the interest is incorrect, you are not using year variable anywhere. it should be,

 do {
    interest = amountInvested * (annualRate / 100) * year;
    total = amountInvested + interest;
    year++;
    System.out.println("Yearend account balance " + total + 
            " Invested this year " + amountInvested + " Annual  return " + interest
            + " number of years " + year);

} while (total < MAX_INVESTMENT && year < 6);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top