Question: Write the code needed to complete the program DiceGame. In the game, rolling a pair of dice is simulated by generating random numbers from 1

Write the code needed to complete the program DiceGame. In the game, rolling a pair of dice is simulated by generating random numbers from 1 to 6 twice (once for each die). If the total is under 7 the player wins. If the total is over 7 the player loses. If the total is 7 the player rolls again. Using the code below, finish the JAVA program.

import java.security.SecureRandom; // a random number generator [0 ~ arg)

class DiceGame

{ // create a random number generator (rng) object

private static SecureRandom rng = new SecureRandom();

public static int rollDice()

{ // roll the dice, print the result and return the sum

int die1 = 1 + rng.nextInt(6); // first die rolled

int die2 = 1 + rng.nextInt(6); // second die rolled

int sum = die1 + die2; // sum of die values

System.out.printf("Player rolled %d + %d = %d%n", die1, die2, sum);

return sum;

}

public static void main(String[] args)

{ // play the dice game

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!