Question: Main topics: Loops Input validation Random numbers Exercise This is designed to give you practice implementing loops, input validation, and also introduces you to the
Main topics:
Loops
Input validation
Random numbers
Exercise
This is designed to give you practice implementing loops, input validation, and also introduces you to the Math classes random() method.
Problem Description
In this lab you will create and use a dice rolling simulator by using Javas random number generator:
int die = (int)(Math.random() * 4) + 1;
will result in either 1, 2, 3 or 4. Make sure you understand why.
Your program must do the following:
1. Prompt the user to enter the number of times to roll a four-sided die. The user can roll a die no less than 100 times. If the user inputs a value less than 100 or greater than 100000, the user should be prompted again until an valid input value is read.
2. Declare and initialize (to zero) four int variables v1, v2, v3, v4 that will be used to keep track of how many times each of the possible die roll outcomes 1, 2, 3, 4 occurred.
3. Use a loop that iterates the appropriate number of times and does the following:
rolls the die by getting a random value between 1 and 4
adds one to the appropriate variable - based on the random number (die outcome) from above.
increments a counter variable used to keep track of how many times the loop has iterated
4. After all rolls have completed displays each possible die roll outcome and the number of times it occurred.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
