Question

In: Computer Science

2. Dice rolling (15 pts) Problem Description: Write a program that rolls a pair of six-sided...

2. Dice rolling (15 pts) Problem Description: Write a program that rolls a pair of six-sided dice, then displays their values sum.

• You can use the random method of the Math class to generate a random number for a die like this: (int) (Math.random() * 6) + 1;

• The application should display special messages for two ones (snake eyes) and two sixes (box cars).

• The application should use static methods (at least two) to organize its code.

• The application should continue only if the user enters “y” or “Y” at the “Roll again?” prompt.

Here is a sample run:

Dice Roller

Roll the dice? (y/n): y

Die 1: 3

Die 2: 1

Total: 4

Roll again? (y/n): y

Die 1: 1

Die 2: 1

Total: 2 Snake eyes!

Roll again? (y/n): y

Die 1: 6

Die 2: 6

Total: 12 Boxcars!

Roll again? (y/n): n

Good bye!

This is for java please check for debugging ! also please leave comments so I could follow steps and better understand

Solutions

Expert Solution

public class DiceRoll {


public static void main(String[] args) {
  
Scanner sc = new Scanner(System.in);
char ch;
System.out.println("Dice Roller");
System.out.print("Roll the dice?(y/n): ");
ch = sc.next().charAt(0); // take input
while(ch=='y'||ch == 'Y'){ // run until user enter y
int x = (int)(Math.random()*6)+1; // generate die 1
int y = (int)(Math.random()*6)+1; // generate die 2
System.out.println("Die 1: "+x); // print die
System.out.println("Die 2: "+y); // print die 2
System.out.print("Total: "+(x+y));// print total
if((x+y)==2) // check for snake eyes
System.out.print(" Snake eyes!");
else if((x+y)==12) // check for boxcars
System.out.print(" Boxcars!");
System.out.print("\nRoll again?(y/n): "); // ask user to run again
sc = new Scanner(System.in); // clear buffer
ch = sc.next().charAt(0); // input
}
System.out.println("Good bye!"); // msg
}   
}

/* OUTPUT */

/* PLEASE UPVOTE */


Related Solutions

PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided...
PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided dice that number of times. The program should keep track of how many times each possible sum comes up using a list. The list's first element should contain how many times a total of 2 was rolled, the second should contain how many times a total of 3 was rolled, and so on all the way through to 12. When all rolls are complete,...
Alice rolls a pair of fair six-sided dice until a sum of 6 appears for the...
Alice rolls a pair of fair six-sided dice until a sum of 6 appears for the first time. Independently, Bob rolls two fair six-sided dice until he rolls a sum 7 for the first time. Find the probability that the number of times Alice rolls her dice is equal to or within one of the number of times Bob rolls his dice.
An experiment consists of rolling six-sided dice twice.                                  &
An experiment consists of rolling six-sided dice twice.                                      (10) List the sample space for this experiment. Find the probability distribution for this experiment where x represents the number of even numbers in the 2 rolls. Find the mean of the probability distribution. Find the standard deviation of the probability distribution. Would it be unusual to get 2 even numbers? Why or why not? (show your work)
You are rolling a pair of balanced dice in a board game. Rolls are independent. You...
You are rolling a pair of balanced dice in a board game. Rolls are independent. You land in a danger zone that requires you to roll doubles (both faces show the same number of spots) before you are allowed to play again. 1. What is the probability of rolling doubles on a single toss of the dice? A) 25/36 B) 5/36 C) 1/6 D) 1/36 2. What is the probability that you do not roll doubles on the first toss,...
Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the...
Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the total on the dice comes up to be a given number. Ask the user for the number that you are rolling for. To have your program roll two dice, use the rand() function this way: die1 = rand() % 6 + 1; die2 = rand() % 6 + 1; Your program then computes and prints the number of rolls it takes to get the...
1. Amy rolls 15 8-sided dice. What is the probability that at least 2 of the...
1. Amy rolls 15 8-sided dice. What is the probability that at least 2 of the rolls are 5s? Answer: 0.5759 2. Amy shoots 10 arrows at a target. Each arrow hits the target (independently) with probability 0.3. What is the probability that at least 3 of the arrows hit the target? Answer: 0.6172 3. Amy shoots 64000 arrows at a target. Each arrow hits the target (independently) with probability 0.3. What is the probability that more than 2 of...
Rolling a pair of 12 sided dice and summing the numbers find the following: The probability...
Rolling a pair of 12 sided dice and summing the numbers find the following: The probability distribution of the sums. Sums= P(Sums)
Consider rolling two 6-sided dice. What is the probability that at least two of the rolls...
Consider rolling two 6-sided dice. What is the probability that at least two of the rolls have a sum that exceeds 6? at least 7 of the rolls have a sum that is even? exactly three rolls have a sum that equals 5?
Consider the game consisting of rolling a pair of fair 6-sided dice and recording the sum....
Consider the game consisting of rolling a pair of fair 6-sided dice and recording the sum. It will cost you $1 to play the game. If the sumis less than 5, then you will win $3. However, you do not get your $1 back so your profit is $2. If you roll a sum of exactly8, thenyou will win $4. However, you do not get your $1 back so your profit is $3. Otherwise, you lose your $1. A.What is...
. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum...
. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] The following table shows the 36 possible combinations of the two dice. Your program should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT