Search code examples
javahistogramdice

Dice Roll Histogram


I have a class question that I cannot seem to understand the logic for.

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, as shown below.

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: ****

I do not understand what in the world this histogram is displaying. Its driving me insane and I cannot move forward with my logic if I do not understand what I am doing. I know this question has been asked before but it looks like they all have a specific number of dice roles they are going off of. I am not given a number and this is what is throwing me off. I know this is really a stupid question. But can anyone possibly explain to me what they mean by "Histogram showing total number of dice rolls for each possible value" what is defining this possible value? I am at a total loss... Any help is appreciated. Here is the code I have written so far.

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 = 0;            // Loop counter iterates numRolls times

      int numRolls = 0;     // User defined number of rolls

      int numOnes = 0;      // Tracks number of 1's found
      int numTwos = 0;      // Tracks number of 2's found
      int numThrees = 0;    // Tracks number of 3's found
      int numFours = 0;     // Tracks number of 4's found
      int numFives = 0;     // Tracks number of 5's found
      int numSixes = 0;     // Tracks number of 6's found
      int numSevens = 0;    // Tracks number of 7's found
      int numEights = 0;    // Tracks number of 8's found
      int numNines = 0;     // Tracks number of 9's found
      int numTens = 0;      // Tracks number of 10's found
      int numElevens = 0;   // Tracks number of 11's found
      int numTwelves = 0;   // Tracks number of 12's found

      int die1 = 0;         // Dice values
      int die2 = 0;         // Dice 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;

            // Count number of sixes and sevens
            if (rollTotal == 1) {
                numOnes = numOnes + 1;
            }

            if (rollTotal == 2) {
                numTwos = numTwos + 1;
            }

            if (rollTotal == 3) {
                numThrees = numThrees + 1;
            }

            if (rollTotal == 4) {
                numFours = numFours + 1;
            }

            if (rollTotal == 5) {
                numFives = numFives + 1;
            }

            if (rollTotal == 6) {
               numSixes = numSixes + 1;
            }

            if (rollTotal == 7) {
               numSevens = numSevens + 1;
            }


            if (rollTotal == 8) {
               numEights = numEights + 1;
            }


            if (rollTotal == 9) {
               numNines = numNines + 1;
            }

            if (rollTotal == 10) {
               numTens = numTens + 1;
            }

            if (rollTotal == 11) {
               numElevens = numElevens + 1;
            }

            else if (rollTotal == 12) {
               numTwelves = numTwelves + 1;
            }

            System.out.println("Roll " + (i+1) + " is " + rollTotal + " (" + die1 + 
                  "+" + die2 + ")");

         }

         // Prints a histogram of the number of dice rolls
         System.out.println("\nDice roll histogram:");
         System.out.println("1's: " + numOnes);
         System.out.println("2's: " + numTwos);
         System.out.println("3's: " + numThrees);
         System.out.println("4's: " + numFours);
         System.out.println("5's: " + numFives);
         System.out.println("6's: " + numSixes);
         System.out.println("7's: " + numSevens);
         System.out.println("8's: " + numEights);
         System.out.println("9's: " + numNines);
         System.out.println("10's: " + numTens);
         System.out.println("11's: " + numElevens);
         System.out.println("12's: " + numTwelves);
      }
      else {
         System.out.println("Invalid rolls. Try again.");
      }

     return;
   }
}

You can see where I entered the entry for histograms. I basically thought that I needed to edit my value given from the integers such as "numOnes" to asterisk but now I'm not certain... any help is appreciated!

EDITED CODE -

System.out.println("\nDice roll histogram:");


     System.out.print("2's: ");
     for(i = 0; i < numTwos; i++){
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("3's: ");
     for (i = 0; i < numThrees; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("4's: " );
     for (i = 0; i < numFours; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("5's: ");
     for (i = 0; i < numFives; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("6's: ");
     for (i = 0; i < numSixes; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("7's: ");
     for (i = 0; i < numSevens; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("8's: ");
     for (i = 0; i < numEights; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("9's: ");
     for (i = 0; i < numNines; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("10's: ");
     for (i = 0; i < numTens; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("11's: ");
     for (i = 0; i < numElevens; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("12's: ");
     for (i = 0; i < numTwelves; i++);{
         System.out.print("*");
     }
     System.out.println("");

  }

OUTPUTS -

Enter number of rolls: 
5
Roll 1 is 8 (2+6)
Roll 2 is 9 (6+3)
Roll 3 is 9 (5+4)
Roll 4 is 6 (4+2)
Roll 5 is 9 (6+3)

Dice roll histogram:
2's: 
3's: *
4's: *
5's: *
6's: *
7's: *
8's: *
9's: *
10's: *
11's: *
12's: *

I can enter a larger number of rolls and I will get an asterisk for the 2's and it will increase like I need but it will not increase the rest of the numbers and they all only get one asterisk. What is keeping my code from properly increasing the amount of *'s? Iv been fighting with this for hours :/


Solution

  • "I do not understand what in the world this histogram is displaying."

    Well, as you said:

    "Histogram showing total number of dice rolls for each possible value"

    The histogram:

    2:  ****** 
    3:  **** 
    4:  *** 
    5:  ******** 
    6:  ******************* 
    7:  ************* 
    8:  ************* 
    9:  ************** 
    10: *********** 
    11: ***** 
    12: ****
    

    is a record of the rolls of a pair of six sided dice. The possible values of totaling a pair of dice range from 2 to 12. If you rolled them 100 times they might give these results.

    This histogram shows that the value 6 was most frequently rolled (19 times) and 4 the least frequently rolled (3 times). If you counted up every * you'd know how many times the dice have been rolled.

    You may think that a histogram must look like this:

    enter image description here

    But turn that sideways:

    enter image description here

    and imagine that the blue bars are *'s, the 100 - 150 label is a 2, and the 150 - 200 label is a 3, well then it starts to look like your histogram.

    Either form is a histogram. They let you compare quantities (how many) of things that fall in different categories (kinds) of things.

    A graphic chart is difficult to output from a program that is text based so instead it's asking you to display what is basicly ascii art:

    enter image description here

    If this was in a text file, and not program output, this could also be considered a form of tally. You can easily add new *'s as you receive new data. It's the same trick as this:

    enter image description here

    Hope that makes more sense.

    SPOILER ALERT: If that helped and you'd like to take a crack at modifying the program yourself read no further. Otherwise...

    This

         System.out.println("\nDice roll histogram:");
         System.out.println(" 1's: " + nManyStars(numOnes));
         System.out.println(" 2's: " + nManyStars(numTwos));
         System.out.println(" 3's: " + nManyStars(numThrees));
         System.out.println(" 4's: " + nManyStars(numFours));
         System.out.println(" 5's: " + nManyStars(numFives));
         System.out.println(" 6's: " + nManyStars(numSixes));
         System.out.println(" 7's: " + nManyStars(numSevens));
         System.out.println(" 8's: " + nManyStars(numEights));
         System.out.println(" 9's: " + nManyStars(numNines));
         System.out.println("10's: " + nManyStars(numTens));
         System.out.println("11's: " + nManyStars(numElevens));
         System.out.println("12's: " + nManyStars(numTwelves));
    

    and this

       static String nManyStarsOld(int n) {
           String result = "";
    
           for (int i = 0; i < n; i++) {
               result += "*";
           }
    
           return result;
       } 
    

    should, if dropped in the right places, get your program to stop printing what it prints now which is this:

    Enter number of rolls: 
    20
    Roll 1 is 5 (2+3)
    Roll 2 is 10 (4+6)
    Roll 3 is 7 (5+2)
    Roll 4 is 7 (5+2)
    Roll 5 is 5 (1+4)
    Roll 6 is 11 (6+5)
    Roll 7 is 8 (3+5)
    Roll 8 is 8 (2+6)
    Roll 9 is 7 (1+6)
    Roll 10 is 8 (3+5)
    Roll 11 is 12 (6+6)
    Roll 12 is 6 (2+4)
    Roll 13 is 8 (6+2)
    Roll 14 is 8 (4+4)
    Roll 15 is 7 (1+6)
    Roll 16 is 4 (2+2)
    Roll 17 is 6 (5+1)
    Roll 18 is 7 (6+1)
    Roll 19 is 6 (1+5)
    Roll 20 is 5 (1+4)
    
    Dice roll histogram:
    1's: 0
    2's: 0
    3's: 0
    4's: 1
    5's: 3
    6's: 3
    7's: 5
    8's: 5
    9's: 0
    10's: 1
    11's: 1
    12's: 1
    

    And instead, get it to print this:

    Dice roll histogram:
     1's: 
     2's: 
     3's: 
     4's: *
     5's: ***
     6's: ***
     7's: *****
     8's: *****
     9's: 
    10's: *
    11's: *
    12's: *
    

    Next time you post a question please include the current output along with the expected output.

    If you feel like being a smarty pants there is actually a one line version of nManyStars():

       static String nManyStars(int n) {
           return new String(new char[n]).replace("\0", "*");
       }
    

    Inspired by this: https://stackoverflow.com/a/4903603/1493294

    Also, if you have at least Java 11 you can trade in nManyStars(n) for "*".repeat(n)

    Hope it helps