How can a simple program be kept running until a defined control character, say "x" or menu choice 4 is entered? I can prompt a menu, offer choices, take keyboard input, perform a calculation but would like it to keep "alive" re-prompting the choices again for more input and so on until my control character or number is entered, then to quit. Is it as simple as a while loop and while != "x"?
Question re keeping a program running
Collapse
X
-
Tags: None
-
Essentially, yes. I'd suggest a do...while loop (since I assume you want your program to prompt with the menu at least once), but that's preference and won't affect the overall functionality. Also, you can't say "while != "x"", though I'm sure you knew that - you'll need a control variable, like a char.Originally posted by sosoatjavaIs it as simple as a while loop and while != "x"? -
Thanks for that- I can get the "engine" of a problem OK- calculations, etc but the structure is still something I need work on. I would also like to "filter" on anything other than my set options, ie 1, 2, or 3, I would try treating the input as a string, then while input== 1 &&2&&3 route to my various methods; if not, echoing out a warning message to the effect that "please pick 1, 2 or 3"Comment
-
OK, OK. All this is do-able. Now, using a string, you'd have to use the .equals() method rather than ==, and you'd have to test for each possibility (of 1, 2, or 3) separately, but all of this is just technical advice.
Basically, for input validation (making sure the user enters a proper value), you need a loop. The loop will tell the user that they made a mistake, and remind them of what information they are entering. This loop should continue executing until the information entered is valid.Comment
Comment