Java Reference
In-Depth Information
do
{
die1 = (
int
) (Math.random() * 6) + 1;
die2 = (
int
) (Math.random() * 6) + 1;
sum = die1 + die2;
rollCount++;
}
while
(sum != num);
return
rollCount;
}
The following program shows how to use the method
rollDice
in a program:
//Program: Roll dice
public class
RollDice
{
public static void
main(String[] args)
{
System.out.println("The number of times the dice are "
+ "rolled to get the sum 10 = " + rollDice(10));
System.out.println("The number of times the dice are "
+ "rolled to get the sum 6 = " + rollDice(6));
}
public static int
rollDice(
int
num)
{
int
die1;
int
die2;
int
sum;
int
rollCount = 0;
do
{
die1 = (
int
) (Math.random() * 6) + 1;
die2 = (
int
) (Math.random() * 6) + 1;
sum = die1 + die2;
rollCount++;
}
while
(sum != num);
return
rollCount;
}
}
Sample Run:
The number of times the dice are rolled to get the sum 10 = 91
The number of times the dice are rolled to get the sum 6 = 7
We leave it as an exercise for you to modify this program so that it allows the user to
enter the desired sum of the numbers to be rolled. (See Programming Exercise 7 at the
end of this chapter.)
Search WWH ::
Custom Search