Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

how to loop program  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help i made this program but when the game is over you have to restart the whole program.  how do I loop it to where it ask if the user wants to play again, and keeps going until the user says no?




 
Bartender
Posts: 1983
57
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget the above code, can you write a simple program that simply loops and prints "hi" 10 times ? (Hint, this would use a for loop).
 
colten Short
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:Forget the above code, can you write a simple program that simply loops and prints "hi" 10 times ? (Hint, this would use a for loop).





 
salvin francis
Bartender
Posts: 1983
57
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect, now, can you change your code to use a do-while loop from a for loop ?
 
salvin francis
Bartender
Posts: 1983
57
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a small nitpick in your for loop code, while it technically works, we usually wrap our loops in brackets "{ ... }" . Also the initialization of "i" can be done within the loop:

 
colten Short
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
salvin francis
Bartender
Posts: 1983
57
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats not do-while, you have a look here for "do-while": https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
 
colten Short
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what would I add to the original code to make it loop?
 
Saloon Keeper
Posts: 5146
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make another method "playGame()" and put your current game playing code inside. Here's a pseudo-code-ish outline.
Because you'll need the Scanner in multiple methods I would make it a class constant...
 
salvin francis
Bartender
Posts: 1983
57
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

colten Short wrote:so what would I add to the original code to make it loop?


You need to understand the do-while loop first. Did you understand how your while loop prints "Hi" 10 times ?
As per java tutorials:

java tutorials wrote:The while statement continually executes a block of statements while a particular condition is true


Similarly the do-while loop has the feature that the block is executed at least once (the first time) and then the condition is checked. Here's a simple code:
output:
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!