i need help! this code is for intro to java, and i cant seem to get this to work
ID: 3853578 • Letter: I
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 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; } }
Explanation / Answer
DiceStats.java
import java.util.Random;
import java.util.Scanner;
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 diceHistogramArr[] = new int[11];
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 + ")");*/
diceHistogramArr[rollTotal-2]++;
}
// Print statistics on dice rolls
System.out.println(" Dice roll statistics:");
// FIXME 5: Complete printing the histogram
for(i=0;i<diceHistogramArr.length;i++){
System.out.print((i+2)+"s: ");
for(int j=0; j<diceHistogramArr[i];j++){
System.out.print("#");
}
System.out.println();
}
} else {
System.out.println("Invalid rolls. Try again.");
}
return;
}
}
Output:
Enter number of rolls:
15
Dice roll statistics:
2s:
3s: #
4s: #
5s: #
6s: ###
7s: ##
8s: #
9s: ###
10s:
11s: ##
12s: #
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.