1.11M Members

Dice Roll Help

 
0
 

I've never felt like a total moron until I took a Computer Science class. I'm trying to create a program that rolls a pair of standard 6 sided dice 10,000 times and outputs how many times doubles show up, using a "while" loop.

I think what I'm trying to do is pretty clear, although I'm positive that I am going about it all wrong.

import java.util.Random;		//to use the random number generator 

/**
   This class simulates rolling a pair of dice 10,000 times and
   counts the number of times doubles of are rolled for each different
   pair of doubles.
*/

public class DiceSimulation
{
	public static void main(String[] args)
	{
		final int NUMBER = 10000;	//the number of times to roll the dice

		//a random number generator used in simulating rolling a dice
		Random generator = new Random();
		
		int die1Value;      	// number of spots on the first die
		int die2Value;      	// number of spots on the second die
		int count = 0;	     	// number of times the dice were rolled
		int snakeEyes = 0;  	// number of times snake eyes is rolled
		int twos = 0;		// number of times double two is rolled
		int threes = 0;		// number of times double three is rolled
		int fours = 0;		// number of times double four is rolled
		int fives = 0;		// number of times double five is rolled
		int sixes = 0;		// number of times double six is rolled

		//Get value of first die (I'm getting a "cannot find symbol" error here)
		die1Value = randomNumbers.nextInt(6);

		//Get value of second die (ditto here)
		die2Value = randomNumbers.nextInt(6);


		//"While" loop to to calculate number of times doubles rolled (getting incomparable types errors here)
		while (count <= 10000)
		{
		if (die1Value = die2Value == 1)
		{snakeEyes++;}
		else if (die1Value = die2Value == 2)
		{twos++;}
		else if (die1Value = die2Value == 3)
		{threes++;}
		else if (die1Value = die2Value == 4)
		{fours++;}
		else if (die1Value = die2Value == 5)
		{fives++;}
		else if (die1Value = die2Value == 6)
		{sixes++;}
		count++;
		}

		

		System.out.println ("You rolled snake eyes " + snakeEyes +
			" out of " + count + " rolls.");
		System.out.println ("You rolled double twos " + twos +
			" out of " + count + " rolls.");
		System.out.println ("You rolled double threes " + threes +
			" out of " + count + " rolls.");
		System.out.println ("You rolled double fours " + fours +
			" out of " + count + " rolls.");
		System.out.println ("You rolled double fives " + fives +
			" out of " + count + " rolls.");
		System.out.println ("You rolled double sixes " + sixes +
			" out of " + count + " rolls.");
	}
}
 
0
 

die1Value = die2Value == 1
You can't string together == tests like that, the = just copes the value on its RHS to the variable on its left.
You need something like
die1Value == 1 && die2Value == 1

 
0
 
public class MatchingPair
{
	public static void main(String [] args)
	{
		
		int dice = (int)(Math.random() * 6) + 1;
                int dice2 = (int)(Math.random() * 6) +1;
		int count = 1;

	
		while (dice != dice2)
		{
			System.out.println("Throw: " + dice +" and "+dice2);
			count = count + 1;
			dice = (int)(Math.random() * 6) + 1;
                        dice2 = (int) (Math.random() * 6) + 1;
		}
		System.out.println("It took " + count + " goes to throw " + dice +" and " + dice2);
	}
}
 
0
 

... and the question is?

 
0
 

... and the question is?

Sorry no I was trying to help with the actual post. Still fairly new to forum. No actual question.

 
0
 

OK, no problem.
As a newcomer you should understand that we try not to just give people answers to their homework - they don't learn much from copy/paste, and it's cheating. You help them more by explaining where and what their mistakes are, and giving some guidance as to what they should be doing next - but not by doing it for them.
Anyway, welcome to DaniWeb.

ps: Always post your code correctly indented and in CODE tags

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: