Monday, 12 January 2015

// The "DiceRolls" class.

import java.awt.*;
import hsa.Console;
import java.lang.Math;

public class DiceRolls
{
    static Console c;           // The output console
   
    public static void main (String[] args)
    {
        c = new Console ();
        
        int [] outcomes = new int [13];  // array for storing outcomes
        int numRolls;  // number of rolls
        int outcome;  // counter (and element value)
        
        // prompt user for number of rolls
        c.print ("How many rolls? ");
        numRolls = c.readInt();
        
        // roll dice and add to outcomes
        for (int roll = 0; roll < numRolls; roll++)
        {
            outcome = (int)(6 * Math.random() + 1) +
                      (int)(6 * Math.random() + 1);
            outcomes [outcome] += 1; // add 1 to counter at index 'outcome'
        } // end of for loop # 1
        
        // show counts of outcomes
        for (int i = 2; i <= 12; i++)
        {
            System.out.println (i + ": " + outcomes [i]);
        }  // end of for loop # 2

    } // main method
} // DiceRolls class

No comments:

Post a Comment