Hello!
I have been trying to figure out exactly what is wrong with the code I copied from an ebook. I have tried everything I can think of and haven't got a clue!
It has to do with my Class Constructor and it isn't making any sense to me because it is just another section of the code where a variable is re-used when it's
public throughout the whole Class.
import java.math.*; import java.util.*; public class PairOfDice { public int die1; // Number showing on the first die. public int die2; // Number showing on the second die . public PairOfDice() { // Constructor. Rolls the dice, so that they initially // show some random values . roll(); //Call the roll() method to roll the dice. } public PairOfDice(int val1, int val2) { // Constructor. Creates a pair of dice that // are initially showing the values val1 and val2. die1 = val1; // Assign specified values die2 = val2; // to the instance variables. } public void roll() { // Roll the dice by setting each of the dice to be // a random number between 1 and 6. die1 = (int)(Math.random()∗6) + 1; //These variables are giving me die2 = (int)(Math.random()∗6) + 1; // the following error below //Multiple markers at this line // - Syntax error on token "Invalid Character", invalid // AssignmentOperator // - The left-hand side of an assignment must be a variable } // end class Pair Of Dice package; } public class RollTwoPair { /** * @param args */ public static void main(String[] args) { PairOfDice firstDice; // Refers to the first pair of dice. firstDice = new PairOfDice(); PairOfDice secondDice; // Refers to the second pair of dice. secondDice = new PairOfDice(); int countRolls; // Counts how many times the two pairs of // dice have been rolled. int total1; // Total showing on first pair of dice. int total2; // Total showing on second pair of dice. countRolls = 0; do { // Roll the two pairs of dice until totals are the same. firstDice.roll(); // Roll the first pair of dice. total1 = firstDice.die1 + firstDice.die2; // Get total. System.out.println( " F i r s t pair comes up " + total1); secondDice.roll(); // Roll the second pair of dice. total2 = secondDice.die1 + secondDice.die2; // Get total. System.out.println( " Second pair comes up " + total2); countRolls++; // Count this roll . System.out.println(); // Blank line. } while (total1 != total2); System.out.println( " It took " + countRolls + " rolls until the totals were the same. " ); } // end main () } // end class RollTwoPairs.