AmberA
AmberA

Reputation: 1

Dice Simulation java

I'm writing a dice simulation program, however I'm having problems with the results because it just prints:

You rolled a ...1 and 1

You rolled double ones 0 out of 10000 rolls.
You rolled double twos 0 out of 10000 rolls.
You rolled double threes 0 out of 10000 rolls.
You rolled double fours 0 out of 10000 rolls.
You rolled double fives 0 out of 10000 rolls.
You rolled double sixes 0 out of 10000 rolls.

it always prints this and I don't understand why, since it should print different numbers each time.

    public class Dice
{
    private int die1;
    private int die2;

    public Dice()
    {
        die1 = 1;
        die2 = 1;
    }

    public void rollDice()
    {
        Random myRan = new Random();

        die1 = myRan.nextInt(6) + 1;
        die2 = myRan.nextInt(6) + 1;
    }

   public int getDie1()
   {
   return die1;
   }


   public int getDie2()
   {
    return die2;   
   }


    public String toString()
    {
         return "You rolled a ..." + die1 + " and " + die2 + "\n";   }
}

and...

 import java.util.Random;
public class DiceSimulation
{
    static final int NUMBER = 10000; 
    static Random generator = new Random();

    static int die1Value; 
    static int die2Value; 
    static int count = 0; 
    static int ones = 0; 
    static int twos = 0; 
    static int threes = 0; 
    static int fours = 0; 
    static int fives = 0; 
    static int sixes = 0; 
    static int c=1;

public static void main(String[] args)
{

    rollDiceAndTabulateWhile(); 
    rollDiceAndTabulteDoWhile();   
    rollDiceAndTabulteFor();   
    summarizeResults();
}

public static void rollDiceAndTabulateWhile()
{

        Dice d = new Dice();
        while(c<= NUMBER)
        {
        d.rollDice();
        die1Value = d.getDie1();
        die2Value = d.getDie2();
        if(die1Value == die2Value)
        count++;
        else if(die1Value == 1 && die2Value == 1)
        ones++;
        else if(die1Value == 2 && die2Value == 2)
        twos++;
        else if(die1Value == 3 && die2Value == 3)
        threes++;
        else if(die1Value == 4 && die2Value == 4)
        fours++;
        else if(die1Value == 5 && die2Value == 5)
        fives++;
        else if(die1Value == 6 && die2Value == 6)
        sixes++;

        c++;
        }
}

public static void rollDiceAndTabulteDoWhile()
{

     DiceSimulation dw = new DiceSimulation();

        Dice d = new Dice();
        do
        {
        d.rollDice();
        dw.die1Value = d.getDie1();
        dw.die2Value = d.getDie2();
        if(die1Value == die2Value)
        count++;
        else if(dw.die1Value == 1 && dw.die2Value == 1)
        ones++;
        else if(dw.die1Value == 2 && dw.die2Value == 2)
        twos++;
        else if(dw.die1Value == 3 && dw.die2Value == 3)
        threes++;
        else if(dw.die1Value == 4 && dw.die2Value == 4)
        fours++;
        else if(dw.die1Value == 5 && dw.die2Value == 5)
        fives++;
        else if(dw.die1Value == 6 && dw.die2Value == 6)
        sixes++;
        c++;
        } while(c <= NUMBER);
}  

public static void rollDiceAndTabulteFor()
  {
    DiceSimulation dw = new DiceSimulation();
    Dice d = new Dice();

    for(c=1;c<=NUMBER;c++)
    {
    d.rollDice();
    dw.die1Value = d.getDie1();
    dw.die2Value = d.getDie2();
    if(die1Value == die2Value)
    count++;
    else if(dw.die1Value == 1 && dw.die2Value == 1)
    ones++;
    else if(dw.die1Value == 2 && dw.die2Value == 2)
    twos++;
    else if(dw.die1Value == 3 && dw.die2Value == 3)
    threes++;
    else if(dw.die1Value == 4 && dw.die2Value == 4)
    fours++;
    else if(dw.die1Value == 5 && dw.die2Value == 5)
    fives++;
    else if(dw.die1Value == 6 && dw.die2Value == 6)
    sixes++;
  }
  }
public static void summarizeResults()
{
    Dice d = new Dice();
    System.out.println(d.toString());
    System.out.println("You rolled double ones " + ones
            + " out of " + NUMBER + " rolls.");
    System.out.println("You rolled double twos " + twos
            + " out of " + NUMBER + " rolls.");
    System.out.println("You rolled double threes " + threes
            + " out of " + NUMBER + " rolls.");
    System.out.println("You rolled double fours " + fours
            + " out of " + NUMBER + " rolls.");
    System.out.println("You rolled double fives " + fives
            + " out of " + NUMBER + " rolls.");
    System.out.println("You rolled double sixes " + sixes
            + " out of " + NUMBER + " rolls.");
}

}

Upvotes: 0

Views: 633

Answers (2)

Blasanka
Blasanka

Reputation: 22437

You are creating Random object when you call the rollDice() each and everytime. It is not good, make it instance variable.

It is not the problem in your code. The problem is your if and else if conditions, Because if one condition is true not reach to other else if and else, that is the flow of the if, else if and else. Since, you need to execute all the incrementation you need to change them to separate if conditions.

Change else if to if:

    if(die1Value == die2Value)
      count++;
    if(die1Value == 1 && die2Value == 1)
      ones++;
    if(die1Value == 2 && die2Value == 2)
      twos++;
    if(die1Value == 3 && die2Value == 3)
      threes++;
    if(die1Value == 4 && die2Value == 4)
      fours++;
    if(die1Value == 5 && die2Value == 5)
      fives++;
    if(die1Value == 6 && die2Value == 6)
      sixes++;

Also make sure to indent your code.

Output:

You rolled double ones 286 out of 10000 rolls.
You rolled double twos 280 out of 10000 rolls.
You rolled double threes 265 out of 10000 rolls.
You rolled double fours 262 out of 10000 rolls.
You rolled double fives 278 out of 10000 rolls.
You rolled double sixes 303 out of 10000 rolls.

UPDATE:

To get

You rolled a ...4 and 3

Change:

  //add static keyword
  private static int die1 = 1;
  private static int die2 = 1;

  public Dice()
  {
     //remove die1 = 1; die2 = 1;
  }

Upvotes: 0

Eric
Eric

Reputation: 2076

Here is your problem

if(die1Value == die2Value)
    count++;

None of your else if statements run when this statement is true.

For instance, if you roll a 4 and 4, then count will increment, and the following else if will never run:

else if(die1Value == 4 && die2Value == 4)
        fours++; // Never runs

Upvotes: 1

Related Questions