Help for assignment #2 page 159
Steps - I recommend compiling and running after every step:
First, figure out how to get 1 die to roll.
Then, make it 2 dice rolling together and add them up
Then, determine whether the roll won and add to a win/loss counter and
print the counter
Then, make the roll/count combination loop to 20 times and print the
total at the end. (Eventually you will change to 10,000, but that is
alot for testing.)
Then, Compute the probability at the end.
Here is some help with rolling a die, which uses Math.random().
Math.random's job is to give you a number between 0 and 1 -
that is what it knows how to do, so you just ask it for the random
number and put the respons into a double variable.
(If you put the response into an integer, it would truncate the decimal
and give you zero.)
If you just wanted to roll 1 die, you would do the following:
/** oneDie - generates one throw of one die
* uses a random number generator to do that
**/
public class oneDie
{
public static void main (String [] args)
{
double x; // holds the random number we will generate
int diceValue; // holds the value of the dice that we calculate from
the random number
x = Math.random(); // generates a number between 0 and 1
x = x *6; // multiplies that number times 6 so the number
will be from 0 to 5
diceValue = (int) x; // convert it to integer to chop off the decimal
portion
diceValue = diceValue +1; // with dice, we want 1-6, not 0-5, so we add
1
System.out.println ("The value of this die is " + diceValue);
}
}
Click here if you think you need a little more
help: