In: Computer Science
Analyzing dice rolls is a common example in understanding probability and statistics. The following calculates the number of times the sum of two dice (randomly rolled) equals six or seven.
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();
int i; // Loop counter iterates numRolls times
int numRolls; // User defined number of rolls
int numSixes; // Tracks number of 6s found
int numSevens; // Tracks number of 7s found
int die1; // Dice values
int die2; // Dice values
int rollTotal; // Sum of dice values
numSixes = 0;
numSevens = 0;
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;
// Count number of sixs and sevens
if (rollTotal == 6) {
numSixes = numSixes + 1;
}
else if (rollTotal == 7) {
numSevens = numSevens + 1;
}
System.out.println("Roll " + (i+1) + " is " +
rollTotal + " (" + die1 +
"+" + die2 + ")");
}
// Print statistics on dice rolls
System.out.println("\nDice roll statistics:");
System.out.println("6s: " + numSixes);
System.out.println("7s: " + numSevens);
}
else {
System.out.println("Invalid rolls. Try again.");
}
return;
}
}
Create different versions of the program that:
Figure 9.17.1: Histogram showing total number of dice rolls for each possible value.
Dice roll histogram: 2: ****** 3: **** 4: *** 5: ******** 6: ******************* 7: ************* 8: ************* 9: ************** 10: *********** 11: ***** 12: ****
import java.util.Scanner;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Random randGen = new Random();
int numRolls; // User defined number of rolls
int numTwo = 0;
int numThree = 0;
int numFour = 0;
int numFive = 0;
int numSix = 0;
int numSeven = 0;
int numEight = 0;
int numNine = 0;
int numTen = 0;
int numEleven = 0;
int numTwelve = 0;
int rollTotal; // Sum of dice values
int die1;
int die2;
do
{
System.out.println("Enter number of rolls: ");
numRolls = scnr.nextInt();
if (numRolls >= 1) {
// Roll dice numRoll times
for (int i = 0; i < numRolls; ++i) {
die1 = randGen.nextInt(6) + 1;
die2 = randGen.nextInt(6) + 1;
rollTotal = die1 + die2;
//Switch case to increase specific variable as per rollTotal
value
switch(rollTotal)
{
case 2:
numTwo = numTwo + 1;
break;
case 3:
numThree = numThree + 1;
break;
case 4:
numFour = numFour + 1;
break;
case 5:
numFive = numFive + 1;
break;
case 6:
numSix = numSix + 1;
break;
case 7:
numSeven = numSeven + 1;
break;
case 8:
numEight = numEight + 1;
break;
case 9:
numNine = numNine + 1;
break;
case 10:
numTen = numTen + 1;
break;
case 11:
numEleven = numEleven + 1;
break;
case 12:
numTwelve = numTwelve+ 1;
break;
}
}
}
else{
System.out.println("Invalid rolls. Try again.");
}
}while(numRolls>1);
// Print statistics on dice rolls
System.out.println();
System.out.println();
for(int i=2;i<=12;i++)
{
System.out.print(i+"s: ");
switch(i)
{
case 2:
while(numTwo>0)
{
System.out.print("*");
numTwo--;
}
System.out.println();
break;
case 3:
while(numThree>0)
{
System.out.print("*");
numThree--;
}
System.out.println();
break;
case 4:
while(numFour>0)
{
System.out.print("*");
numFour--;
}
System.out.println();
break;
case 5:
while(numFive>0)
{
System.out.print("*");
numFive--;
}
System.out.println();
break;
case 6:
while(numSix>0)
{
System.out.print("*");
numSix--;
}
System.out.println();
break;
case 7:
while(numSeven>0)
{
System.out.print("*");
numSeven--;
}
System.out.println();
break;
case 8:
while(numEight>0)
{
System.out.print("*");
numEight--;
}
System.out.println();
break;
case 9:
while(numNine>0)
{
System.out.print("*");
numNine--;
}
System.out.println();
break;
case 10:
while(numTen>0)
{
System.out.print("*");
numTen--;
}
System.out.println();
break;
case 11:
while(numEleven>0)
{
System.out.print("*");
numEleven--;
}
System.out.println();
break;
case 12:
while(numTwelve>0)
{
System.out.print("*");
numTwelve--;
}
System.out.println();
break;
}
}
return;
}
}
Output: