DiceRoller V 1.0.0
 
This is an example program that demonstates some of the topics covered in week 3 and prior weeks. See comments for the details of what it demonstrates.
 
Sample output:
 
 
 
Source Code:
 
/*=============================================
    Purpose:    Simulates the rolling of
                one die for two player and
                produces statistics
 
                Demonstrates:
                    1.  for flow structure
                    2.  if, else if, else flow structure
                    3.  accumulators
                    4.  Math.random for random numbers
                    5.  System.println
                    6.  System.print
                    7.  \n and \t escape characters
                    8.  ++ and += operators
                    9.  Nesting of flow structures
 
    Version history:
 
    Number:         1.0.0
    Date:           4/8/2003
    Programmers:    Lon Hosford
    Changes:        Original programming
 

=============================================*/
 

public class DiceRoller
{
 

    /*---------------------------------------------
        Program starts here. No arguments are expected.
    ---------------------------------------------*/
    public static void main(String [] args)
    {
        /*---------------------------------------------
        |   Declare variables private                 |
        |   to the main method                        |
        ---------------------------------------------*/
 
        /*---------------------------------------------
            Total rolls for the human
        ---------------------------------------------*/
        long    dieValuesTotalHuman = 0;
        /*---------------------------------------------
            Total wins for the human
        ---------------------------------------------*/
        long    winsCountHuman = 0;
        /*---------------------------------------------
            The value of the die for human
        ---------------------------------------------*/
        int     dieValueHuman;
        /*---------------------------------------------
            Total rolls for the computer
        ---------------------------------------------*/
        long    dieValuesTotalComputer = 0;
        /*---------------------------------------------
            Total wins for the computer
        ---------------------------------------------*/
        long    winsCountComputer = 0;
        /*---------------------------------------------
            The value of the die for computer
        ---------------------------------------------*/
        int     dieValueComputer;
        /*---------------------------------------------
            Total ties
        ---------------------------------------------*/
        long    tiesCount = 0;
        /*---------------------------------------------
            Winner name
        ---------------------------------------------*/
        String  winnerName;
 

        /*---------------------------------------------
            Display report detail report header
        ---------------------------------------------*/
        System.out.println("Roll #\tHuman\tComputer  Winner");
        System.out.println("=======\t=======\t=======   ======= ");
        /*---------------------------------------------
            Counter loop 1 to 10 for each die roll
        ---------------------------------------------*/
        for ( int rollNumber = 1; rollNumber <= 10; rollNumber++)
        {
            /*---------------------------------------------
                Random die roll for human player
            ---------------------------------------------*/
            dieValueHuman =     (int) (Math.random() * 6 + 1);  // 1- 6
            /*---------------------------------------------
                Random die roll for computer player
            ---------------------------------------------*/
            dieValueComputer =  (int) (Math.random() * 6 + 1);  // 1- 6
            /*---------------------------------------------
                Human die roll value exceeds computer die roll value
            ---------------------------------------------*/
            if (dieValueHuman > dieValueComputer)
            {
                /*---------------------------------------------
                    Set name for winner report column
                ---------------------------------------------*/
                winnerName = "Human";
                /*---------------------------------------------
                    Accumulate wins count for human
                ---------------------------------------------*/
                winsCountHuman++;
                /*---------------------------------------------
                    Accumulate die value total for human
                ---------------------------------------------*/
                dieValuesTotalHuman += dieValueHuman;
            }
            /*---------------------------------------------
                Computer die roll value exceed human die roll value
            ---------------------------------------------*/
            else if (dieValueHuman < dieValueComputer)
            {
                /*---------------------------------------------
                    Set name for winner report column
                ---------------------------------------------*/
                winnerName = "Computer";
                /*---------------------------------------------
                    Accumulate wins count for computer
                ---------------------------------------------*/
                winsCountComputer++;
                /*---------------------------------------------
                    Accumulate die value total for computer
                ---------------------------------------------*/
                dieValuesTotalComputer += dieValueComputer;
            }
            /*---------------------------------------------
                Human die roll value equals computer die roll value
            ---------------------------------------------*/
            else
            {
                /*---------------------------------------------
                    Set name for winner report column
                ---------------------------------------------*/
                winnerName = "Tied";
                /*---------------------------------------------
                    Accumulate tie count
                ---------------------------------------------*/
                tiesCount++;
            }
 
            /*---------------------------------------------
                Display data for each column in report
                    roll number
                    die value for human
                    die value for computer
                    name of the winner
            ---------------------------------------------*/
            System.out.println(     rollNumber          + "\t"
                                +   dieValueHuman       + "\t"
                                +   dieValueComputer    + "\t  "
                                +   winnerName);
 
        }
 
        /*---------------------------------------------
            Underscore detail report and display totals
        ---------------------------------------------*/
        System.out.println("=======\t=======\t=======");
        System.out.println( "Total" + "\t" + dieValuesTotalHuman  + "\t" + dieValuesTotalComputer);
 
        /*---------------------------------------------
            Start summary report
        ---------------------------------------------*/
        System.out.println("\nSummary:");
        System.out.print(   "Wins" + "\t"  + winsCountHuman       + "\t" + winsCountComputer);
        /*---------------------------------------------
            Ties count exceeds 0
        ---------------------------------------------*/
        if (tiesCount > 0 )
        {
            /*---------------------------------------------
                Add ties count stats to current
                console line
            ---------------------------------------------*/
            System.out.print("\tTied: "  + tiesCount);
 
        }
        /*---------------------------------------------
            New console line
        ---------------------------------------------*/
        System.out.println();
        /*---------------------------------------------
            Start report line for winner based on total of
            die values for all rolls
        ---------------------------------------------*/
        System.out.print("Winner based on total of all rolls... ");
        /*---------------------------------------------
            Die value total for human exceeds
            same for computer
        ---------------------------------------------*/
        if ( dieValuesTotalHuman  > dieValuesTotalComputer)
        {
            System.out.println("Human");
        }
        /*---------------------------------------------
            Die value total for computer exceeds
            same for human
        ---------------------------------------------*/
        else if (dieValuesTotalHuman  < dieValuesTotalComputer)
        {
            System.out.println("Computer");
        }
        /*---------------------------------------------
            Die value total for computer equals
            same for human
        ---------------------------------------------*/
        else
        {
            System.out.println("Tied");
        }
        /*---------------------------------------------
            Start report line for winner based on total of
            wins
        ---------------------------------------------*/
        System.out.print("Winner based on number of wins....... ");
        /*---------------------------------------------
            Win count for human exceeds
            same for computer
        ---------------------------------------------*/
        if ( winsCountHuman  > winsCountComputer)
        {
            System.out.println("Human");
        }
        /*---------------------------------------------
            Win count for computer exceeds
            same for human
        ---------------------------------------------*/
        else if (winsCountHuman  < winsCountComputer)
        {
            System.out.println("Computer");
        }
        /*---------------------------------------------
            Win count for computer equals
            same for human
        ---------------------------------------------*/
        else
        {
            System.out.println("Tied");
        }
    } // End of main method
 
 
 
}