Skip to content
Advertisement

While loop calculation issue

I’m currently working on a Java program (for school) that prints two statements when a user enters a starting balance for their account.

So for example, if the user inputs $10,000, there will be two statements that print at the same time. One tells them how long it will take for their account to reach $100,000, and the other when it reaches $1,000,000.

Now I have the code, which I’ll post below, and it works (it gets me a result that’s close to what is required), however, the issue I’m having is with the math itself. I’ll post the code and explain:

public static void main(String[] args) {
    int startBalance = 0;
    int storedBalance = 0;
    int years = 0;
    try (Scanner stdln = new Scanner(System.in)) {
        System.out.println("Please enter your starting balance: ");
        startBalance = stdln.nextInt();
    }
    
    while (storedBalance < 100000)
    {   
        storedBalance = startBalance + (storedBalance * 2) ;
        ++years;
    } 
    System.out.println("It will take " + years + " year(s) for your bank balance to reach " + storedBalance + ".");
    
    while (storedBalance < 1000000)
    {   
        storedBalance = startBalance + (storedBalance * 2);
        ++years;
    } 
    System.out.println("It will take " + years + " year(s) for your bank balance to reach " + storedBalance + ".");
}

}

So in theory, the startBalance is supposed to double until it reaches $100,000 or more, in which case the years it takes to reach over $100,000 is calculated and the statement gets printed (same with the second While loop but with $1,000,000). So if the user entered $10,000, the storedBalance variable for the first While loop should go $20k, $40k, $80k, $160k, but with the math I currently have it goes $10k, $30k, $70k, $150k.

I’ve also tried:

storedBalance = (startBalance * 2) + storedBalance;

and it works, but the issue is that instead of doubling the amount (10k to 20k, 20k to 40k, 40k to 80k, etc.) it just adds 20k to the previous number, which makes sense since the logic in that statement if the startBalance is 10k is: (10,000 * 2) + storedBalance, so the logic within the brackets remains constant and doesn’t adjust as the number changes but the stored balance increases with every loop.

So I know that something is missing because year 1 = $10k when it should be $20k but the years it takes to get there are correct. I have a feeling my math is incorrect as I’m not doubling the initial value and I’m just adding the storedBalance to it.

Any and all responses are greatly appreciated. Thank you in advance.

Advertisement

Answer

You should initialize the storedBalance equal to startBalance outside the while loop. Then inside the while-loops you simply double the storedBalance value on every loop-iteration (year) with storedBalance = storedBalance * 2;.

public static void main(String[] args) {
int startBalance = 0;
int years = 0;
try (Scanner stdln = new Scanner(System.in)) {
    System.out.println("Please enter your starting balance: ");
    startBalance = stdln.nextInt();
}
int storedBalance = startBalance;
while (storedBalance < 100000)
{   
    storedBalance *= 2;
    ++years;
} 
System.out.println("It will take " + years + " year(s) for your bank balance to reach " + storedBalance + ".");

storedBalance = startBalance;
years = 0;
while (storedBalance < 1000000)
{   
    storedBalance *= 2;
    ++years;
} 
System.out.println("It will take " + years + " year(s) for your bank balance to reach " + storedBalance + ".");

}

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement