Ask coding questions

← Back to all posts
How to repeat the program without running it
huntercanttell (1)

I was looking around but there isn't very much solid information for JAVA, I am trying to create a scanner question that asks if the user wants to restart the program, like "would you like to restart? Yes/No" kinda thing, does anyone have an idea or code that I can learn from? I want the program to run lines from ex. 3-50. I don't want the code to go ex. 1,2,3,4,5,6 etc. If the user says yes the program will re-run itself and all the scanners in between

Comments
hotnewtop
sweetmango88 (2)

haha everyone is saying a while loop

sweetmango88 (2)

you could use a for or while loop. for your purpose, you would probably want to use the 'while' loop. here is an example of the code:
while(var i=0) {
var ask= prompt("want to repeat?");
if(ask='yes') {
//you put nothing here lol
}
else {
i++;
//and then it stops repeating because i is no longer 0
};
};

Coder100 (16733)

Yes called while loops

while (true) {
  if (... condition to break...) break;
}
BooleanBean (2)

YOu can either do a while loop:
while (i<3){ // run this code in block i++; }alert(i);
or for loop:
for (initial-expression; condition; second-expression){ // run this code in block }alert(i);

or do while:
do { // run this code in block i++; } while (i<3); alert(i);

Hope this helps. PS use an if statement with a button/ prompt(); for that restart thing. I would be really interested if you sent me the project once ur finished!