Question: Task #1 Loops 1. Copy the file DiceSimulation.java into NetBeans or other Java IDE tools. 2. The double roll of 1s and 2s are given
Task #1 Loops
1. Copy the file DiceSimulation.java into NetBeans or other Java IDE tools.
2. The double roll of 1s and 2s are given in the code. You can run the program to get a result like:
You rolled snake eyes 282 out of 10000 rolls. You rolled double twos 282 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.
3. You follow the style to code for double roll value for 3 6 so all the doubles should be around 278.
4. Change the while loop to do-while loop and run the program. Result should be similar.
5. Change the while loop to a for loop and run the program. Result should be similar.
/**
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.
*/
import java.util.Random; //to use the random number generator
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
//ENTER YOUR CODE FOR THE ALGORITHM HERE
while(count < NUMBER)
{
die1Value = generator.nextInt(6) + 1; //returns 1,2,3,4,5,or 6
die2Value = generator.nextInt(6) + 1; //returns 1,2,3,4,5,or 6
if(die1Value == die2Value)
{
if (die1Value == 1)
snakeEyes++;
else if (die1Value == 2)
twos++;
// Task #1 step 3: To do - code for die1Value = 3, 4, 5, and 6
}
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.");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
