Question:
How would I loop my code in Java?
Josh
2013-04-27 12:08:42 UTC
I'm not talking about the correct loop in java. I mean I want my program to loop a code if a certain condition is not met. For Example, If the user does not input a number for "What is your age?" Then alert "Please enter a number" then the code loops. How do I do this?
Four answers:
godfatherofsoul
2013-04-27 14:11:28 UTC
When your loop depends on a condition, you want to use a while loop:



boolean gotMilk=true;

while(gotMilk==true)

{

foo.drink();

gotMilk=foo.hasMoreMilk();

}

System.out.println("No more milk!");
halrosser
2013-04-27 12:50:08 UTC
prompt the user for his input, using a Scanner object.

Check what he entered with an "if" statement.

Inside the If statement, use a "while" loop - to repeatedly prompt for input and check what he entered until he enters something.

tada!
donna s
2013-04-27 12:27:49 UTC
OMG...there are SOOOOO many ways to do this... :-)



Try this link, here are multiple examples to many ways to do it...

http://www.java-forums.org/blogs/java-tip/264-taking-inputs-users.html
Mary
2015-04-29 21:16:07 UTC
extremely tough point. look from google or bing. that could actually help!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...