There is a certain carnival game that is played this way the player rolls one 6
ID: 3710738 • 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. Name your program Carnival.java.
Please dont use : DiceRolls d = new DiceRolls(); ( Its one chapter after the present asignement.)
Explanation / Answer
Below is your code. Let me know in Comments if you have any issue..
Carnival.java
//class declaration
public class Carnival {
//method to return random number between 1 and x(inclusive)
private static int dieRoll(int x) {
//Use Random to generate random
Random rand = new Random();
//Adding 1 to include x
return 1 + rand.nextInt(x);
}
//main method to run the code
public static void main(String[] args) {
//Variable to hold number of wins
int totalWins = 0;
//Loop to run game 100 times
for (int i = 0; i < 100; i++) {
//variable to hold sum of die rolls
int sumOfGame = 0;
//Rolling the 5 dies
sumOfGame = sumOfGame + dieRoll(6); //Rolling Die with 6 sides
sumOfGame = sumOfGame + dieRoll(20);//Rolling Die with 20 sides
sumOfGame = sumOfGame + dieRoll(8);//Rolling Die with 8 sides
sumOfGame = sumOfGame + dieRoll(4);//Rolling Die with 4 sides
sumOfGame = sumOfGame + dieRoll(12);//Rolling Die with 12 sides
//If sum is less than 20 or greater than 35
//The game is win, so number of wins are incremented
if(sumOfGame < 20 || sumOfGame > 35) {
totalWins++;
}
}
//Printing the number of times the game is Won.
System.out.println("The player wins "+totalWins+" out of 100.");
}
}
Output
The player wins 24 out of 100.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.