The game of Craps is a dice game that involves rolling a pair of dice. At the st
ID: 3672442 • 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 rolls a 7, or 11 they win. Write a 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. Possible Program structure 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 to 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. Complete Security Injection Lab Input Validation. See Previous Security Injection Lab Instruction but do Input Validation lab and do not retake the Pre Survey. For The Laboratory Section Write a short program call BMI that completes the requirements for the Laboratory Section and zip the BMI.java file with the Craps.java file in the approriate Dropbox folder. See Dropbox for due date. For the Discussion Section complete all the discussion questions 1 - 7. Random Number Generator Sample code: Random randomGenerator = new Random();//Create Random number generator objects randomGenerator.nextInt(4);//Creates a random integer between 0 and 3. or Math.random(); // creates a random double that is >= 0 and < 1.0
Explanation / Answer
import java.util.Scanner;
import java.util.Random;
public class Craps {
//Instatiate the scanner
private static Scanner scannerObject = new Scanner(System.in);
public static void playGame() {
//Instantiate the random number generator
Random randomGenerator = new Random();
//Set up die 1
int die1 = 2;
//Set up die 2
int die2 = 2;
//A variable to hold the dice total
int diceTotal = 0;
//A variable to track the number of sevens rolled
int numberOfSevens = 0;
//A variable to track the number of elevens rolled
int numberOfElevens = 0;
//Loop 100 times (100 rolls)
for(int count = 0; count < 100; count++) {
//Roll die number 1 (get a random number between 1 and 6)
die1 = randomGenerator.nextInt(6) + 1;
//Roll die number 2 (get a random number between 1 and 6)
die2 = randomGenerator.nextInt(6) + 1;
//Add up the two dice
diceTotal = die1 + die2;
if (diceTotal == 7) {
//Increment the number of sevens
numberOfSevens = numberOfSevens + 1;
} else if (diceTotal == 11) {
//Increment the number of elevens
numberOfElevens = numberOfElevens + 1;
}
}
//Get the total number of winds
int total = numberOfSevens + numberOfElevens;
//Output the results
System.out.printf("The player rolled " + numberOfSevens + " sevens and "
+ numberOfElevens + " elevens for a total of " + total
+ " wins out of 100 ");
//Ask the user if they want to play again
System.out.print("Do you want to play again? Y or N: ");
String play = scannerObject.next();
//Check if we want to play again
if (play.equals("Y") || play.equals("y")) {
playGame();
} else {
System.out.println("Game Over ");
scannerObject.close();
}
}
public static void main(String[] args) {
//Initial game play
playGame();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.