This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Do while loop not executing properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Do while loop not executing properly" Watch "Do while loop not executing properly" New topic
Author

Do while loop not executing properly

Daniel Hoang
Ranch Hand

Joined: Apr 22, 2013
Posts: 44
Hi, i made a program that asks the user to input a number for the amount of times they want to roll a die. The die will roll a certain amount of times and count the number of times each face is landed on. Then display a chart that has the number of times the number was landed on in asterisks, like so
Please enter the number of times you would like to throw the die: 100
The chart showing 100 throws of the die:
1 ******************
2 ****************
3 **************
4 *******************
5 **************
6 *******************
Would you like to play the game again?
Please enter (yes/no) no

My problem is that when I input "yes" to play again, the variables in the code dont reset, forcing me to only input a higher number than the last time playing, and the asterisks save between each play. Here is my code. Can anybody clue me in on how i can get the game to reset properly? Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Which is the loop which is repeated if the user says they want to play again? Put the initialization of the variables at the beginning of that loop, not in the class-level declarations where you have it now.

And by the way, I find it hard to identify that loop. That's partly because your code is inconsistently indented, but more to the point it's because you have all of your code dumped into the main() method. Nested loops are hard to follow at the best of times, i.e. even if the indentation makes sense. What I'm suggesting here is that you should put the main processing code of the innermost loop into a separate method and call it from the main() method.
Daniel Hoang
Ranch Hand

Joined: Apr 22, 2013
Posts: 44
The entire game is looped once the player wants to play again, thanks your advice worked. My code is indented incorrectly? I dont get it, i tried to make all the indents even. And im not allowed to make another method.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Daniel Hoang wrote:My code is indented incorrectly? I dont get it, i tried to make all the indents even.


Here's an example:



The second and third lines look as if they should work the same, but the second line is part of the for-loop and the third line isn't. That could be misleading.

Here's another:



The first four lines are part of the do-while loop, so they should be indented one step farther than the "} while" line which terminates the loop.

And im not allowed to make another method.


Really? Not your fault, then. I'm always amazed at the capability of teachers to teach bad habits. Sometimes I wonder how anybody ever learns anything.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do while loop not executing properly