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

Help with Control Structures.

 
Philip Herzer
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to write my own Craps game. It is very limited, just to help me get the idea of control structures within java. I tried using nested ifs, but failed to do it properly because in my crap game, the 2nd and 3rd rolls still occur regardless if the player wins in the first rolls, second, or third.

My second attempt was trying to seperate the stuff using switch and using a while control loop. I would appreciate any help on the first attempt and suggestions for improving my second attempt.

First Attempt:


Second Attempt


Thanks,
Philip Herzer
[ September 23, 2004: Message edited by: Philip Herzer ]
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In both cases you do not have an proper exit to your program. Use: to exit the program after a game is complete. Your second version of the method rollDice, it should probably be renamed rollAndTallyTwoDice, because this will produce self-documenting code. The second version of the program has an int rollCounter but doesn't use it. I would use the rollCounter to limit the number of rolls that a player can have and I would also keep a record of the players status, along the lines of:
 
Kalai Selvan
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Philip,
You should put an return (or exit statement) statement after printing the winner, so no further execution will take place.

Regards,
Kalai Selvan T.
 
Kalai Selvan
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOps i am bit late and not as descriptive as Nigel Browne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic