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(); die1.setFaceValue(setNumDie1); die2.setFaceValue(setNumDie2); //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