Hi, I\'m having a few problems with this Java DiceStats program. I seem to have
ID: 3915945 • Letter: H
Question
Hi, I'm having a few problems with this Java DiceStats program. I seem to have the imput almost right, but it isn't quite the output that my online-interactive book is going for. I'm not sure where I'm going wrong.
The instructions are "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: 1). Create a variable name seedVal and assign the value 11. 2.) Call the Math.setSeed( ) method to set the seed for the Random number generator object randGen 3). Declare the variables to track the number found for each dice roll. 4. )For each dice roll, add up the occurrences for each number found. 5.) Use either if case or switch case (recommended) method.
I've copy and pasted my input, and took screenshots of my results (in the imgur link at the bottom of the oage)
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();
int diceHistogramArr[] = new int[11];
int i = 0;
int numRolls = 0;
int numSixes = 0;
int numSevens = 0;
int die1 = 0;
int die2 = 0;
int rollTotal = 0;
System.out.println("Enter number of rolls: ");
numRolls = scnr.nextInt();
if (numRolls >= 1) {
for (i = 0; i < numRolls; ++i) {
die1 = randGen.nextInt(6) + 1;
die2 = randGen.nextInt(6) + 1;
rollTotal = die1 + die2;
if (rollTotal == 6) {
numSixes = numSixes + 1;
}
if (rollTotal == 7) {
numSevens = numSevens + 1;
}
diceHistogramArr[rollTotal-2]++;
}
System.out.println(" Dice roll statistics:");
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;
}
}
the expected output vs. the output I actually got is shown on this imgur link. https://imgur.com/a/zpQ7ZQA
Explanation / Answer
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(); int diceHistogramArr[] = new int[11]; int i = 0; int numRolls = 0; int numSixes = 0; int numSevens = 0; int die1 = 0; int die2 = 0; int rollTotal = 0; int seedVal = 11; System.out.println("Enter number of rolls: "); numRolls = scnr.nextInt(); randGen.setSeed(seedVal); if (numRolls >= 1) { for (i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.