0

My while statement just doesn't seem to make sense to me, even though it works.
I want it to calculate the interest only as long as the countYears is less than the timeLimit....so if I set timeLimit to 5, it should only calculate 5 years worth of interest but the way I read the current while statement, it doesn't seem to say that. Maybe I am just reading it wrong?

public class RandomPractice {
public static void main(String[] args)
{
    Scanner Keyboard = new Scanner(System.in);
    double intRate, begBalance, balance;
    int countYears, timeLimit;

    System.out.println("Please enter your current investment balance.");
    begBalance = Keyboard.nextDouble();

    System.out.println("Please enter your YEARLY interest rate (in decimals).");
    intRate = Keyboard.nextDouble();

    System.out.println("Please enter how long (in years) you would like to let interest accrue.");
    timeLimit = Keyboard.nextInt();

    balance = begBalance * (1 + intRate);
    countYears = 0;

    /* The way I read this while statement is as follows 
     * "While countYears is GREATER than the timeLimit...calculate the balance"
     * This makes no logical sense to me but I get the correct output? 
     * I want this code to calculate the investment interest ONLY as long as
     * countYears is LESS than timeLimit **/
    while (countYears >= timeLimit)
    {
        balance = balance + (balance * intRate);
        countYears++;
    }       

    System.out.println(balance);

  }
}
4
  • I think something is wrong with your code. Commented Aug 18, 2015 at 5:03
  • are you sure the output is right? Commented Aug 18, 2015 at 5:04
  • 1
    You're causing a lot of trouble for yourself (and showing us a lot of irrelevant code) by dumping all of this code into the main method. Pull the math out into a function that doesn't do IO, and write tests for it. Commented Aug 18, 2015 at 5:07
  • Are you providing time limit as 0, that is the only case when while loop will be executed once, otherwise it will not be executed or will be an infinite loop. Commented Aug 18, 2015 at 5:08

2 Answers 2

1

That code you have, as it stands, does not generate the correct data, my transcript for eight years at one percent per annum follows:

Please enter your current investment balance.
100
Please enter your YEARLY interest rate (in decimals).
.01
Please enter how long (in years) you would like to let interest accrue.
8
101.0

In other words, only one year of interest is added rather than eight years.

So either your compiler is totally screwy, your code is not what you think it is, or whatever test data and/or method you're using to check the interest calculation is lacking somewhat.

First, as you foreshadowed, you need to change the condition to be countYears < timeLimit.

In addition, you also need to remove the initial interest calculation before the loop since this would mean you'd get a full year's interest as soon as you deposit the money. With those two changes:

balance = begBalance;
while (countYears < timeLimit) {
    balance = balance + (balance * intRate);
    countYears++;
}       

and you then get the correct value of:

Please enter your current investment balance.
100
Please enter your YEARLY interest rate (in decimals).
.01
Please enter how long (in years) you would like to let interest accrue.
8
108.28567056280801
Sign up to request clarification or add additional context in comments.

1 Comment

Ah ok thank you so much! I was testing only one scenario, which just HAPPENED to give me the correct answer even when the code was incorrect. Thanks again!
0

Your loop isn't being executed at all if you switch it to <= it will be correct.

Right now your output is what is calculated outside of the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.