Advertisement
ZhenghaoZhu

DiceToss

Jan 13th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class DiceToss
  4. {
  5.   public static void main(String[] args)
  6.   {
  7.     Scanner input = new Scanner(System.in);
  8.     System.out.println("How many times will you roll the two dice?");
  9.     int toss = input.nextInt();
  10.     int sum, dieA, dieB;
  11.     //counters needed to keep track o results
  12.     int two =0;
  13.     int three =0;
  14.     int four =0;
  15.     int five =0;
  16.     int six=0;
  17.    
  18.     for (int i =1; i <= toss; i++){
  19.       dieA=(int)(Math.random()*6+1);
  20.       dieB=(int)(Math.random()*6+1);
  21.      if (dieA ==2 || dieB==2)
  22.         two++;
  23.      if (dieA ==3 || dieB==3)
  24.         three++;
  25.      if (dieA ==4 || dieB==4)
  26.         four++;
  27.      if (dieA ==5 || dieB==5)
  28.         five++;
  29.      if (dieA ==6 || dieB==6)
  30.         six++;
  31.      sum = dieA + dieB;
  32.      System.out.println(dieA + " and " + dieB);
  33.      System.out.println("The sum of the two dice is " + sum);
  34.     }
  35.     if(two>=1)
  36.       System.out.println("2==>" + two + " times " +((int)(two/ (double)toss*100))+"%");
  37.     if(three>=1)
  38.       System.out.println("3==>" + three + " times " + ((int)(three/ (double)toss*100))+"%");
  39.     if(four>=1)
  40.       System.out.println("4==>" + four + " times " +((int)(four/ (double)toss*100))+"%");
  41.     if(five>=1)
  42.       System.out.println("5==>" + five + " times " +((int)(five/ (double)toss*100))+"%");
  43.     if(six>=1)
  44.       System.out.println("6==>" + six + " times " +((int)(six/ (double)toss*100))+"%");
  45.   }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement