There is a certain carnival game that is played this way the player rolls one 6
ID: 3700782 • Letter: T
Question
There is a certain carnival game that is played this way
the player rolls one 6 sided die
the player rolls one 20 sided die
the player rolls one 8 sided die
the player rolls one 4 sided die
the player rolls one 12 sided die
The player wins if the total of the 5 die is greater than 35 or less than 20.
Write a program that simulates this game being played 100 times and prints to the screen the number of times a player wins.
Your program must include a method: int dieRoll(int x) that returns a random number between 1 and x (inclusive), you are to use this method in your program.
Explanation / Answer
DiceRolls.java
import java.util.Random;
public class DiceRolls {
Random r = new Random();
public static void main(String[] args) {
DiceRolls d = new DiceRolls();
int sides[] = {6,20,8,4,12};
int wins = 0;
for(int i=1;i<=100;i++) {
int sum = 0;
for(int j=0;j<sides.length;j++) {
sum+=d.dieRoll(sides[j]);
}
if(sum<20 || sum>35) {
wins++;
}
}
System.out.println("The number of times wins: "+wins);
}
public int dieRoll(int x) {
return r.nextInt(x)+1;
}
}
Output:
The number of times wins: 33
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.