Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble changing variable within loop

 
Kev Woo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic