Skip to content
Sign upLog in
← Back to Community

How to repeat the program without running it

Profile icon

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

Profile icon
Profile icon

haha everyone is saying a while loop

Profile icon

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 {
//and then it stops repeating because i is no longer 0

Profile icon

Yes called while loops

while (true) { if (... condition to break...) break; }
Profile icon

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!

Profile icon

A while loop