| Author |
while loop question
|
anthony dwizzledorf
Greenhorn
Joined: Mar 01, 2004
Posts: 2
|
|
I am just startin out with programming so bare with me. I wrote a simple program that allows a user to enter an amount of money and it will output an amount in change. I was wondering how I would incorporate a loop so that a user has the option of inputing a yes or no statement if they want to keep using the body of the program over and over again. Any suggestions?
|
 |
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
|
|
Use a while loop with a boolean condition and ask the user on each iteration if they would like to continue.
|
 |
anthony dwizzledorf
Greenhorn
Joined: Mar 01, 2004
Posts: 2
|
|
|
Thanks Craig. I'll give it a shot.
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Welcome to JavaRanch, anthony! As you're new around these parts and have asked a good beginner's type question, allow me to recommend the following list of resources that you might find useful to have around. Sun's Java TutorialIntroduction to Computer Science using Java by Bradley KjellIntroduction to Programming Using Java by David J. EckDick Baldwin's Java Programming TutorialsInteractive Programming In Java by Lynn Andrea SteinBruce Eckel's Thinking In JavaJavaRanch's own Campfire StoriesAllen B. Downey's How To Think Like A Computer Scientist Also, when you start to make use of the standard Java libraries a bit more, you'll likely find the API documentation nice to reference to learn just what things can do and how they do it. [ March 02, 2004: Message edited by: Dirk Schreckmann ]
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
sever oon
Ranch Hand
Joined: Feb 08, 2004
Posts: 268
|
|
Not to nitpick , but the only difference between the do/while and the while/repeat loop is that the do/while is guaranteed to run the loop body at least one time before exiting: The "do something else" is the only code that will run, and it will only run once in the above example. Since that's the only purpose for the do loop's existence, and you meet the criteria, I say give that poor do loop its shot at the limelight (it gets used so infrequently). You may want to consider defining your exit criteria according to a bunch of exit constants, that way you can have the program exit with information about why it's exiting: sev [ March 03, 2004: Message edited by: sever oon ]
|
 |
 |
|
|
subject: while loop question
|
|
|