File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Trouble changing variable within loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble changing variable within loop" Watch "Trouble changing variable within loop" New topic
Author

Trouble changing variable within loop

Kev Woo
Greenhorn

Joined: Nov 03, 2010
Posts: 11
When I run this Craps program my int variable "total" will go back to its original value (chips) every time the do loop reiterates. I want the total to keep track of all of the iteration until it reaches 0. Any ideas why? It's probably something simple, but I really can't figure it out right now.

Thanks in advance.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Why have you made all your methods static?
Go through your loop with a pencil and paper, writing down the values of the point and rolls after each iteration. Or alternatively, add some System.out.println() statements which display the values as you run your application.

Never write == true or ==false. That is not only poor style, but may cause nasty errors if you mistakenly write = instead of ==.
Don't create a new Random object every time. If you are testing, use the Random constructor which takes a seedThat way, you will get the same sequence of numbers out of it (I think), which might make testing easier.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

When I run this Craps program my int variable "total" will go back to its original value (chips) every time the do loop reiterates.

What makes you think the "total" is not accumulating - and that it is getting re-set each iteration?


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble changing variable within loop