Method rollDice

In the rules of the game, the player must roll two dice on the first roll and must do the same on all subsequent rolls. We declare method rollDice (Fig. D.5, lines 68–81) to roll the dice and compute and print their sum. Method rollDice is declared once, but it’s called from two places (lines 26 and 50) in main, which contains the logic for one complete game of craps. Method rollDice takes no arguments, so it has an empty parameter list. Each time it’s called, rollDice returns the sum of the dice, so the return type int is indicated in the method header (line 68). Although lines 71 and 72 look the same (except for the die names), they do not necessarily produce the same result. Each of these statements produces a random value ...

Get Android™ How to Program, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.