This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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: 2215
92
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: 2215
92
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: 2215
92
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: 2215
92
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?
 
Bartender
Posts: 5774
56
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: 2215
92
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:
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!