This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 36453
    
  15
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: 3934
    
  17

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
 
Similar Threads
Displaying output for the given format
Java Help: NullPointerException
Java Static Problems
My Array
how to calculate the N to the power of N