February 2014
Beginner
1248 pages
62h 25m
English
You might be wondering why we declared the sums of the dice as private static final int constants rather than as enum constants. The reason is that the program must compare the int variable sumOfDice (line 26) to these constants to determine the outcome of each roll. Suppose we declared enum Sum containing constants (e.g., Sum.SNAKE_EYES) representing the five sums used in the game, then used these constants in the switch statement (lines 29–45). Doing so would prevent us from using sumOfDice as the switch statement’s controlling expression, because Java does not allow an int to be compared to an enum constant. To achieve the same functionality as the current program, we would have to use ...
Read now
Unlock full access