Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Robert Jarvis
- * CGS3416-1
- * Assignment 3 part 2
- */
- import java.util.Scanner; //Import Scanner class to accept user input
- import java.util.Random; //Import Random class to create random dice rolls
- public class Dice {
- static Random r1 = new Random(); //Random object created outside of method blocks and static so both methods can use them
- public static void main (String[] args){
- Scanner input = new Scanner(System.in); //Variable to accept scanner input
- int timesRolled; //Amount of times the dice are to be rolled
- int snakeEyes = 0; //Counter to count the amount of times double 1s is rolled
- int seven = 0; //Counter to count the amount of times 7 is rolled
- int counter = 0; //Counter for amount of times dice are rolled
- System.out.print("How many times would you like to roll the two dice? "); //Displays prompt for input
- timesRolled = input.nextInt();
- while(counter++ < timesRolled){ //While loop to loop and count amount of times dice are rolled/snake eyes and sevens are rolled
- int dieTotal = rollDice(); //Creating variable from rollDice method
- if(dieTotal == 2){
- snakeEyes++;}
- else if(dieTotal == 7){
- seven++;}
- }
- double percentSnake = (snakeEyes/timesRolled); //Variables for the percentage of rolls these special cases make up
- double percentSeven = (seven/timesRolled);
- System.out.println("\nSnake eyes (double 1s) appeared"); //Begin output of program
- System.out.printf(" " + snakeEyes + " times\n");
- System.out.printf(" %.2f %% of the time\n", percentSnake);
- System.out.println("\nA roll of 7 appeared");
- System.out.printf(" " + seven + " times\n");
- System.out.printf(" %.2f %% of the time", percentSeven);
- }
- public static int rollDice(){ //Method for rolling dice
- int die1 = r1.nextInt(7) + 1; //Die1 is a random number 1-6
- int die2 = r1.nextInt(7) + 1; //Die2 is a random number 1-6
- int dieTotal = die1 + die2; //dieTotal is the combination of these numbers
- return dieTotal; //Returns dieTotal
- }
- }
Add Comment
Please, Sign In to add comment