This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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?




 
Saloon Keeper
Posts: 2146
80
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
Saloon Keeper
Posts: 2146
80
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
Saloon Keeper
Posts: 2146
80
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
Saloon Keeper
Posts: 2146
80
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: 5482
55
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
Saloon Keeper
Posts: 2146
80
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:
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!