mercredi 30 novembre 2016

Dice Roll Histogram with Loops

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.




Aucun commentaire:

Enregistrer un commentaire