Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Dice Program Help

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Thanked 0 Times in 0 Posts

    Default Dice Program Help

    I have to roll a pair of dice and count the number of box cars (two sixes) that occur, here is what I have:

     import java.util.*;
     public class BoxCar{
         public static void main (String [] args){
             //Declare two dice, and a counter
             PoD die1, die2;
             int counter; // To tell how many times to roll the dice
             int setNumDie1; //Set number to be used in die1.setFaceValue
             int setNumDie2; //Set number to be used in die2.setFaceValue
             int sum;
             int totalBox;
             Scanner scan = new Scanner (System.in);
             //Declares di1 and die2 as dice
             die1 = new PoD();
             die2 = new PoD();
             //Set the counter to 10, able to count down to 0
             counter = 1000;
             //This section of code demonstrates the setFaceValue mothod 
             //by asking the user to input of the each die they would 
             //like to start at.
             System.out.println ("Please enter a number between 1 and 6:");
             System.out.print ("Die 1: ");
             setNumDie1 = scan.nextInt();
             System.out.print ("Die 2: ");
             setNumDie2 = scan.nextInt();
             //This shows the dice are set at the user inputed number,
             //and show the sum just becuase
             System.out.println ("You set the value of Die #1 at: " +
                die1.getFaceValue() + " and Die #2 at: " + die2.getFaceValue());
             sum = die1.getFaceValue() + die2.getFaceValue();
             System.out.println ("The sum of the two dice are: " + sum);
             //The reason counter is there and not the number ten,
             //is that a simple change in code by allowing the user to 
             //enter counter, it will count that many times.
             System.out.println ("Now we will roll the dice " + 
                counter + " times.");
             //This statement means that as long as counter is less than ten
             //it will keep rolling, Once it reaches ten it will stop.
             while (counter > 0){
                 die1.roll(); //Rolls die1
                 die2.roll(); //Rolls die2
                  if ((die1 == 6) && (die2 == 6)){
                    totalBox ++;
                 System.out.println ("Die one: " + die1 + ", Die two: " + die2); 
                 counter-- ; //Adds 1 to the value to counter

    __________________________________________________ __________________________________

    public class PoD
       private final int MAX = 6;  // maximum face value
       private int faceValue;  // current value showing on the die
       //  Constructor: Sets the initial face value.
       public PoD()
          faceValue = 1;
       //  Rolls the die and returns the result.
       public int roll()
          faceValue = (int)(Math.random() * MAX) + 1;
          return faceValue;
       //  Face value mutator.
       public void setFaceValue (int value)
          faceValue = value;
       //  Face value accessor.
       public int getFaceValue()
          return faceValue;
       //  Returns a string representation of this die.
       public String toString()
          String result = Integer.toString(faceValue);
          return result;

    Where i have the code line " if ((die1 == 6) && (die2 == 6)){
    totalBox ++;"

    all i get is that PoD and int are incomparable types. What am i doing wrong. I cant get the program to count the number of times the dice roll two sixes

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,465 Times in 2,416 Posts

    Default Re: Dice Program Help

    PoD and int are incomparable types
    The die variables are NOT int. You need to add a method to the PoD class that takes an int and tests the die's face value against it.