223k views
5 votes
/* Return the sum of two 6-sided dice rolls, each in the range 1..6. However,

* if noDoubles is true, if the two dice show the same value, increment one
* die to the next value, wrapping around to 1 if its value was 6.
*/
public int withoutDoubles(int die1, int die2, boolean noDoubles) {
if(noDoubles && die1 == die2) {
die1++;
if(die1 == 7)
die1 = 1;
}

return die1 + die2;
}

User Tobs
by
7.7k points

1 Answer

2 votes

Final answer:

The given code returns the sum of two 6-sided dice rolls, with the constraint that if noDoubles is true and the two dice show the same value, one of the dice is incremented by 1, wrapping around to 1 if its value was 6.

Step-by-step explanation:

The given code returns the sum of two 6-sided dice rolls, with the constraint that if noDoubles is true and the two dice show the same value, one of the dice is incremented by 1, wrapping around to 1 if its value was 6. The sum of the dice is then returned.

For example, if the input is (4, 4, true), the code will increment the second die to 5 and return the sum of 4 and 5, which is 9.

Sample Java code:

public int withoutDoubles(int die1, int die2, boolean noDoubles) {
if (noDoubles && die1 == die2) {
die1++;
if (die1 == 7) die1 = 1;
}
return die1 + die2;
}

User Torben G
by
8.2k points