Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
Counting a while loop?
Old
rhys84au
Junior Member
 
Status: Offline
Posts: 1
Join Date: Jun 2012
Default Counting a while loop? - 06-27-2012, 06:30 PM

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
   
Reply With Quote
Possibility
Old
MrMoke
Senior Member
 
MrMoke's Avatar
 
Status: Offline
Posts: 501
Join Date: Sep 2007
Location: Austin, Texas
Default Possibility - 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.

Last edited by MrMoke; 06-27-2012 at 08:27 PM.
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



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