Alice Community

Alice Community (
-   How do I...? (
-   -   Counting a while loop? (

rhys84au 06-27-2012 06:30 PM

Counting a while loop?
Hi all, New Alice user here... I must say fantastic program for introductions to java! Started Uni studies and this is our first stepping stone in programming.

Anyway I have the following problem, Iam required to build an interest calculator. The calculator gets 3 values from the user (initial balance, target balance, interest rate) it then performs the math and outputs yearly the new total balance.

I have it working by using a while loop that executes if the current balance is =< target it will do the math.. Update the current balance, prints the balance and does the check again and keeps going over n over... As it should until it's => target.

My problem lies in that right before I print the current balance in each loop I also want a counter to display what year it is (eg 1 thru to whatever not the actual year) so that you can see how many years elapsed to achieve your goal.

I have tried a normal loop and printing the index.. Did not work as I couldn't tell the loop when to stop as that variable changes
Tried a couple of other things as well but does anybody have any suggestions?

Hesitant to post up the code as this is an assignment so Iam after a nudge in he right direction rather than a solution?

Iam thinking maybe there is a way to creat a new method variable that updates each cycle through and print that? Warm?

Thanks everyone

MrMoke 06-27-2012 07:06 PM

If you create a number variable, and give it a value of zero, then you can increment it by one just before the print statement. Including the variable name in the print line shows the value it contains.

If the decimal in the year variable bothers you, use the "int as a string" function to get rid of it.

All times are GMT -5. The time now is 04:43 PM.

Copyright ©2020, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.