In: Computer Science
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
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 */