Guest User

Untitled

a guest
Feb 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. /*Robert Jarvis
  2.  * CGS3416-1
  3.  * Assignment 3 part 2
  4.  */
  5.  
  6. import java.util.Scanner; //Import Scanner class to accept user input
  7. import java.util.Random; //Import Random class to create random dice rolls
  8.  
  9. public class Dice {
  10.    
  11.     static Random r1 = new Random(); //Random object created outside of method blocks and static so both methods can use them
  12.    
  13.     public static void main (String[] args){
  14.        
  15.         Scanner input = new Scanner(System.in); //Variable to accept scanner input
  16.         int timesRolled; //Amount of times the dice are to be rolled
  17.         int snakeEyes = 0; //Counter to count the amount of times double 1s is rolled
  18.         int seven = 0; //Counter to count the amount of times 7 is rolled
  19.         int counter = 0; //Counter for amount of times dice are rolled
  20.        
  21.         System.out.print("How many times would you like to roll the two dice? "); //Displays prompt for input
  22.             timesRolled = input.nextInt();
  23.                
  24.         while(counter++ < timesRolled){  //While loop to loop and count amount of times dice are rolled/snake eyes and sevens are rolled
  25.             int dieTotal = rollDice(); //Creating variable from rollDice method
  26.                 if(dieTotal == 2){
  27.                     snakeEyes++;}
  28.                 else if(dieTotal == 7){
  29.                     seven++;}
  30.             }
  31.        
  32.        
  33.         double percentSnake = (snakeEyes/timesRolled); //Variables for the percentage of rolls these special cases make up
  34.         double percentSeven = (seven/timesRolled);
  35.        
  36.            
  37.         System.out.println("\nSnake eyes (double 1s) appeared"); //Begin output of program
  38.         System.out.printf("   " + snakeEyes + " times\n");
  39.         System.out.printf("   %.2f %% of the time\n", percentSnake);
  40.         System.out.println("\nA roll of 7 appeared");
  41.         System.out.printf("   " + seven + " times\n");
  42.         System.out.printf("   %.2f %% of the time", percentSeven);
  43.         }
  44.  
  45.     public static int rollDice(){ //Method for rolling dice
  46.         int die1 = r1.nextInt(7) + 1; //Die1 is a random number 1-6
  47.         int die2 = r1.nextInt(7) + 1; //Die2 is a random number 1-6    
  48.         int dieTotal = die1 + die2; //dieTotal is the combination of these numbers
  49.         return dieTotal;        //Returns dieTotal
  50.     }
  51. }
Add Comment
Please, Sign In to add comment