The game of Craps is a dice game that involves rolling a pair of dice. At the st
ID: 3915924 • Letter: T
Question
The game of Craps is a dice game that involves rolling a pair of dice. At the start of each turn if the players roll a 7 or 11 they win.
Write a java program that simulates how often a player would win if they rolled the dice 100 times.
The program will output a message such as "The player rolled 14 sevens and 5 elevens for a total of 19 wins out of 100."
The program will ask the user if they wish to run another simulation or quit.
Create a random number for each dice using either the API Random class or the Math classes' random method.
Create a loop that will loop 100 times.
Within the loop, store the random number output
If it is a seven or eleven increment the value of the appropriate variable.
Print out the message stating the number of sevens and elevens.
Use the appropriate type of loop to prompt the user if they like to try again or quit.
Remember to provide javadoc comments and appropriate in code comments.
Explanation / Answer
Main.java
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//create two dice
Dice dice1=new Dice();
Dice dice2=new Dice();
//variables two trace events
int sevens=0,elevens=0,r1,r2;
//roll two dices 100 time
for(int i=0;i<100;i++){
r1=dice1.roll();
r2=dice2.roll();
//if adds to seven count the event
if((r1+r2)==7)
sevens++;
//if adds to eleven count the event
if((r1+r2)==11)
elevens++;
}
System.out.println("The player rolled "+sevens+" sevens and "+elevens
+" elevens for a total of "+(sevens+elevens)+" wins out of 100.");
}
}
/*represent a dice*/
class Dice{
private Random rand;
Dice(){
//initialize random object
rand=new Random();
}
//this method return a integer between 1 and 6
public int roll(){
return rand.nextInt(7)+1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.