0

I have a problem in my class that I just can't figure out.

This is the question:

The purpose of this quiz is to reinforce the understanding of using loops and counting as well as reviewing the use of random numbers.

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

~~~~~~~~~~~~~~~~~~~~~

I haven't been able to get the program to print the histogram in the example above.

And this is what I have 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 seedVal = 11;

          randGen.setSeed(seedVal);

          // 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 numOnes = 0;
          int numTwos = 0;
          int numThrees = 0;
          int numFours = 0;
          int numFives = 0;
          int numSixes = 0;   // Tracks number of 6s found
          int numSevens = 0;  // Tracks number of 7s found
          int numEights = 0;
          int numNines = 0;
          int numTens = 0;
          int numElevens = 0;
          int numTwelves = 0;
          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 == 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("Debugging: Roll " + (i+1) + " is " + rollTotal + " (" + die1 + 
              "+" + die2 + ")");
            }

             // Print statistics on dice rolls
             System.out.println("\nDice roll statistics:");

             // FIXME 5: Complete printing the histogram
             System.out.println("1s: " + numOnes);
             System.out.println("2s: " + numTwos);
             System.out.println("3s: " + numThrees);
             System.out.println("4s: " + numFours);
             System.out.println("5s: " + numFives);
             System.out.println("6s: " + numSixes);
             System.out.println("7s: " + numSevens);
             System.out.println("8s: " + numEights);
             System.out.println("9s: " + numNines);
             System.out.println("10s: " + numTens);
             System.out.println("11s: " + numElevens);
             System.out.println("12s: " + numTwelves);
          }
          else {
             System.out.println("Invalid rolls. Try again.");
          }

         return;
       }
    }

Any help would be very appreciated.

  • First glance: it would be good to replace `numOnes`, `numTwos`, etc. with an array. Also: could you be more explicit about what problem you are having? I'm guessing printing the correct number of "#" in the output. – markspace Nov 30 '16 at 20:55
  • 1
    If you're problem is generating a repeating string of #, then this is a duplicate of http://stackoverflow.com/questions/7107297/what-is-the-easiest-way-to-generate-a-string-of-n-repeated-characters – sprinter Nov 30 '16 at 21:02

3 Answers3

0

Have a loop like this where you have your print statements.

Modify your code so that instead of taking new variables every time have them in a array so that you can loop through them.

 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 seedVal = 11;

      randGen.setSeed(seedVal);

      // 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[] numValues=new int[12];
      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
           numValues[rollTotal]++;
            System.out.println("Debugging: Roll " + (i+1) + " is " + rollTotal + " (" + die1 + 
          "+" + die2 + ")");
        }

         // Print statistics on dice rolls
         System.out.println("\nDice roll statistics:");

         // FIXME 5: Complete printing the histogram
        for(int i=2;i<=12;i++)
        {
           System.out.print(i+"s: ");
           for(int j=0;j<numVales[i];j++)
           {
               System.out.print("#");
           }
           System.out.println();
      }
      else {
         System.out.println("Invalid rolls. Try again.");
      }

     return;
   }
}

Let me know if you need clarification on the problem.

Coder
  • 2,153
  • 1
  • 16
  • 21
0

You can do something like this:

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    //You can directly set the seed during the object creation.
    Random random = new Random(System.currentTimeMillis());
    // This array is used to keep the value of your dice (2 - 12)
    int [] histogram = new int[13];

    while(true) {
        System.out.println("Enter number of rolls: ");
         int numberOfRolls = scanner.nextInt();

         //If you enter 0, you can simply terminate the program
         if(numberOfRolls == 0) break;

         for(int i = 0; i < numberOfRolls; i++) {
             int rolledValue = (random.nextInt(6) + 1) + (random.nextInt(6) + 1);
             histogram[rolledValue]++;
         }

         //Print the result to your console. 
         for(int i = 2; i < histogram.length; i++) {
            System.out.print("Total: " + i + " ");
            for(int j = 0; j <histogram[i]; j++) {
                System.out.print("#");
            }
            System.out.println();
         } 
    }
}

That code will have a result as below:

Enter number of rolls: 7

Total: 2

Total: 3 #

Total: 4

Total: 5 ##

Total: 6

Total: 7 ###

Total: 8

Total: 9

Total: 10 #

Total: 11

Total: 12

Saber Alex
  • 1,544
  • 3
  • 24
  • 39
0

Looks like you're really close. You just need to print the number of # for each int variable you have. The following will do that for the numTwos:

         char[] chars = new char[numTwos];
         Arrays.fill(chars, '#');
         String result = new String(chars);
         System.out.println(result);

You can put the whole thing in a loop of 12 to print it for all of them.

Abdulgood89
  • 359
  • 2
  • 9
  • Where would I put this? – Kristen Soriano Dec 01 '16 at 15:21
  • Sorry about that....... Instead of this line: System.out.println("1s: " + numOnes); You can replace it with the code I listed. This will print out the number of hashes (#####) instead of the actual int. Then, you can put the whole thing in a loop so you can print out the second, third, fourth, etc. – Abdulgood89 Dec 01 '16 at 17:03
  • Please let me know if you need further clarification. – Abdulgood89 Dec 01 '16 at 17:05