Question
i need help! this code is for intro to java, and i cant seem to get this to work perfectly. here are the instructions. the
i need help! this code is for intro to java, and i cant seem to get this to work perfectly. here are the instructions. the code will be copied after the instructions. The purpose of this quiz is to reinforce the understanding of using loops and counting as well as reviewing the use of random numbers (in Chapter 2.14). 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. Example: Histogram showing total number of dice rolls for each possible value. Dice roll statistics (result varies): 2s: ###### 3s: #### 4s: ### 5s: ######## 6s: ################### 7s: ############# 8s: ############# 9s: ############## 10s: ########### 11s: ##### 12s: #### 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.
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 cariables 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 < numRolls; ++i) { die1 = randGen.nextInt(6) + 1; die2 = randGen.nextInt(6) + 1; rollTotal = die1 + die2; // 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; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started