d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Question > Do While Loops
Add Reply New Topic New Poll
Retired Moderator
Posts: 16,251
Joined: Aug 25 2011
Gold: 2,555.75
Trader: Trusted
Jun 28 2012 08:18am
I've done Do While loops but not with variables... Do i need to use boolean in order to use variables in the loop? I've done C++ and QBasic (sadly), but i'm still new to java.

I'm not sure how to set up a do while loop so it asks the person "Do you want to run the program?" and if their answer is No, the program should close if the answer is yes, the program should run. Once the program is run, it should loop to the top and re-ask if they want to run it.

any answers (that are remotely accurate) will be helpful!

Member
Posts: 649
Joined: May 28 2012
Gold: 14.00
Jun 28 2012 08:23am
you could do it like this :

Code

               Scanner sc = new Scanner(System.in);
 String answer="";
 do{
  System.out.println("Do you want to run the program ?");
  answer = sc.nextLine();
  if ( answer.equals("No") ) return;
 } while(true);


or you could use a boolean (run), treat answer and if it's yes your boolean will be true, if it's no the boolean will be false and instead of while(true) you will have while(run) for exemple.
Retired Moderator
Posts: 16,251
Joined: Aug 25 2011
Gold: 2,555.75
Trader: Trusted
Jun 29 2012 05:32am
thanks man! helped a TON! :)
Member
Posts: 789
Joined: Dec 25 2009
Gold: 0.00
Jun 30 2012 04:14am
no
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll