Question: Question Modify the program below to print a histogram in which the total number of times the dice rolls equals each possible value is displayed
Question
Modify the program below to print a histogram in which the total number of times the dice rolls equals each possible value is displayed by printing a character like # that number of times. Two dices will be used in each roll.
Note:
Create a variable name seedVal and assign the value 11.
Call the Math.setSeed( ) method to set the seed for the Random number generator object randGen.
Declare the variables to track the number found for each dice roll.
For each dice roll, add up the occurrences for each number found. Use either if case or switch case (recommended) method.
Print the statistics similar to the example shown above. Use if else method.
Expected Output




Code
import java.util.Scanner; import java.util.Random;
public class DiceStats {
public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Random randGen = new Random(); // FIXME 1 and 2: Set the seed to the Random number generator int i = 0; // Loop counter iterates numRolls times int numRolls = 0; // User defined number of rolls // FIXME 3: Declare and initiate variables needed int numSixes = 0; // Tracks number of 6s found int numSevens = 0; // Tracks number of 7s found int die1 = 0; // Dice 1 values int die2 = 0; // Dice 2 values int rollTotal = 0; // Sum of dice values
System.out.println("Enter number of rolls: "); numRolls = scnr.nextInt();
if (numRolls >= 1) { // Roll dice numRoll times for (i = 0; i
// FIXME 4: Count number of sixs and sevens; complete the same for all other possible values if (rollTotal == 6) { numSixes = numSixes + 1; } if (rollTotal == 7) { numSevens = numSevens + 1; } System.out.println("Debugging: Roll " + (i+1) + " is " + rollTotal + " (" + die1 + "+" + die2 + ")"); }
// Print statistics on dice rolls System.out.println(" Dice roll statistics:"); // FIXME 5: Complete printing the histogram System.out.println("6s: " + numSixes); System.out.println("7s: " + numSevens); } else { System.out.println("Invalid rolls. Try again."); }
return; } }
Input 30 Enter number of rolls: Dice roll statistics: Your output6s: 5s: #### 6s : ### 8s: ### 9s: ## 10s: ### 11s: 12s: Enter number of rolls: Dice roll statistics: Expected output 7s: ### 10s: ### 11s: ## 12s
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
